1 /*
2  * Copyright (C) 2018 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.quickstep.util;
17 
18 import android.animation.ValueAnimator;
19 import android.view.animation.Interpolator;
20 
21 import com.android.launcher3.Utilities;
22 
23 import java.util.ArrayList;
24 
25 /**
26  * Utility class to update multiple values with different interpolators and durations during
27  * the same animation.
28  */
29 public abstract class MultiValueUpdateListener implements ValueAnimator.AnimatorUpdateListener {
30 
31     private final ArrayList<FloatProp> mAllProperties = new ArrayList<>();
32 
33     @Override
onAnimationUpdate(ValueAnimator animator)34     public final void onAnimationUpdate(ValueAnimator animator) {
35         final float percent = animator.getAnimatedFraction();
36 
37         for (int i = mAllProperties.size() - 1; i >= 0; i--) {
38             FloatProp prop = mAllProperties.get(i);
39             float interpolatedPercent = prop.mInterpolator.getInterpolation(percent);
40             prop.value = Utilities.mapRange(interpolatedPercent, prop.mStart, prop.mEnd);
41         }
42         onUpdate(percent, false /* initOnly */);
43     }
44 
45     /**
46      * @param percent The total animation progress.
47      * @param initOnly When true, only does enough work to initialize the animation.
48      */
onUpdate(float percent, boolean initOnly)49     public abstract void onUpdate(float percent, boolean initOnly);
50 
51     public final class FloatProp {
52 
53         public float value;
54 
55         private final float mStart;
56         private final float mEnd;
57         private final Interpolator mInterpolator;
58 
FloatProp(float start, float end, Interpolator i)59         public FloatProp(float start, float end, Interpolator i) {
60             value = mStart = start;
61             mEnd = end;
62             mInterpolator = i;
63             mAllProperties.add(this);
64         }
65 
66         /**
67          * Gets the start value.
68          */
getStartValue()69         public float getStartValue() {
70             return mStart;
71         }
72     }
73 }
74