1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.launcher3.folder;
17 
18 import android.animation.Animator;
19 import android.animation.AnimatorListenerAdapter;
20 import android.animation.FloatArrayEvaluator;
21 import android.animation.ObjectAnimator;
22 import android.util.Property;
23 
24 import java.util.Arrays;
25 
26 /**
27  * Animates a Folder preview item.
28  */
29 class FolderPreviewItemAnim {
30 
31     private static final Property<FolderPreviewItemAnim, float[]> PARAMS =
32             new Property<FolderPreviewItemAnim, float[]>(float[].class, "params") {
33                 @Override
34                 public float[] get(FolderPreviewItemAnim anim) {
35                     sTempParamsArray[0] = anim.mParams.scale;
36                     sTempParamsArray[1] = anim.mParams.transX;
37                     sTempParamsArray[2] = anim.mParams.transY;
38                     return sTempParamsArray;
39                 }
40 
41                 @Override
42                 public void set(FolderPreviewItemAnim anim, float[] value) {
43                     anim.setParams(value);
44                 }
45             };
46 
47     private static final PreviewItemDrawingParams sTmpParams =
48             new PreviewItemDrawingParams(0, 0, 0);
49     private static final float[] sTempParamsArray = new float[3];
50 
51     private final ObjectAnimator mAnimator;
52     private final PreviewItemManager mItemManager;
53     private final PreviewItemDrawingParams mParams;
54 
55     public final float[] finalState;
56 
57     /**
58      * @param params layout params to animate
59      * @param index0 original index of the item to be animated
60      * @param items0 original number of items in the preview
61      * @param index1 new index of the item to be animated
62      * @param items1 new number of items in the preview
63      * @param duration duration in ms of the animation
64      * @param onCompleteRunnable runnable to execute upon animation completion
65      */
FolderPreviewItemAnim(PreviewItemManager itemManager, PreviewItemDrawingParams params, int index0, int items0, int index1, int items1, int duration, final Runnable onCompleteRunnable)66     FolderPreviewItemAnim(PreviewItemManager itemManager,
67             PreviewItemDrawingParams params, int index0, int items0, int index1, int items1,
68             int duration, final Runnable onCompleteRunnable) {
69         mItemManager = itemManager;
70         mParams = params;
71         mParams.index = index1;
72 
73         mItemManager.computePreviewItemDrawingParams(index1, items1, sTmpParams);
74         finalState = new float[] {sTmpParams.scale, sTmpParams.transX, sTmpParams.transY};
75 
76         mItemManager.computePreviewItemDrawingParams(index0, items0, sTmpParams);
77         float[] startState = new float[] {sTmpParams.scale, sTmpParams.transX, sTmpParams.transY};
78 
79         mAnimator = ObjectAnimator.ofObject(this, PARAMS, new FloatArrayEvaluator(),
80                 startState, finalState);
81         mAnimator.addListener(new AnimatorListenerAdapter() {
82             @Override
83             public void onAnimationEnd(Animator animation) {
84                 if (onCompleteRunnable != null) {
85                     onCompleteRunnable.run();
86                 }
87                 params.anim = null;
88             }
89         });
90         mAnimator.setDuration(duration);
91     }
92 
setParams(float[] values)93     private void setParams(float[] values) {
94         mParams.scale = values[0];
95         mParams.transX = values[1];
96         mParams.transY = values[2];
97         mItemManager.onParamsChanged();
98     }
99 
start()100     public void start() {
101         mAnimator.start();
102     }
103 
cancel()104     public void cancel() {
105         mAnimator.cancel();
106     }
107 
hasEqualFinalState(FolderPreviewItemAnim anim)108     public boolean hasEqualFinalState(FolderPreviewItemAnim anim) {
109         return Arrays.equals(finalState, anim.finalState);
110 
111     }
112 }
113