summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/folder/FolderPreviewItemAnim.java
blob: be075bc9a518e2a185dd18ae79d7fd86adb81afc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.folder;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;

import com.android.launcher3.LauncherAnimUtils;

/**
 * Animates a Folder preview item.
 */
class FolderPreviewItemAnim {

    private static PreviewItemDrawingParams sTmpParams = new PreviewItemDrawingParams(0, 0, 0, 0);

    private ValueAnimator mValueAnimator;

    float finalScale;
    float finalTransX;
    float finalTransY;

    /**
     * @param params layout params to animate
     * @param index0 original index of the item to be animated
     * @param items0 original number of items in the preview
     * @param index1 new index of the item to be animated
     * @param items1 new number of items in the preview
     * @param duration duration in ms of the animation
     * @param onCompleteRunnable runnable to execute upon animation completion
     */
    FolderPreviewItemAnim(final PreviewItemManager previewItemManager,
            final PreviewItemDrawingParams params, int index0, int items0, int index1, int items1,
            int duration, final Runnable onCompleteRunnable) {
        previewItemManager.computePreviewItemDrawingParams(index1, items1, sTmpParams);

        finalScale = sTmpParams.scale;
        finalTransX = sTmpParams.transX;
        finalTransY = sTmpParams.transY;

        previewItemManager.computePreviewItemDrawingParams(index0, items0, sTmpParams);

        final float scale0 = sTmpParams.scale;
        final float transX0 = sTmpParams.transX;
        final float transY0 = sTmpParams.transY;

        mValueAnimator = LauncherAnimUtils.ofFloat(0f, 1.0f);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
            public void onAnimationUpdate(ValueAnimator animation) {
                float progress = animation.getAnimatedFraction();

                params.transX = transX0 + progress * (finalTransX - transX0);
                params.transY = transY0 + progress * (finalTransY - transY0);
                params.scale = scale0 + progress * (finalScale - scale0);
                previewItemManager.onParamsChanged();
            }
        });
        mValueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (onCompleteRunnable != null) {
                    onCompleteRunnable.run();
                }
                params.anim = null;
            }
        });
        mValueAnimator.setDuration(duration);
    }

    public void start() {
        mValueAnimator.start();
    }

    public void cancel() {
        mValueAnimator.cancel();
    }

    public boolean hasEqualFinalState(FolderPreviewItemAnim anim) {
        return finalTransY == anim.finalTransY && finalTransX == anim.finalTransX &&
                finalScale == anim.finalScale;

    }
}