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 java.util.ArrayList;
22 
23 /**
24  * Utility class to update multiple values with different interpolators and durations during
25  * the same animation.
26  */
27 public abstract class MultiValueUpdateListener implements ValueAnimator.AnimatorUpdateListener {
28 
29     private final ArrayList<FloatProp> mAllProperties = new ArrayList<>();
30 
31     @Override
onAnimationUpdate(ValueAnimator animator)32     public final void onAnimationUpdate(ValueAnimator animator) {
33         final float percent = animator.getAnimatedFraction();
34         final float currentPlayTime = percent * animator.getDuration();
35 
36         for (int i = mAllProperties.size() - 1; i >= 0; i--) {
37             FloatProp prop = mAllProperties.get(i);
38             float time = Math.max(0, currentPlayTime - prop.mDelay);
39             float newPercent = Math.min(1f, time / prop.mDuration);
40             newPercent = prop.mInterpolator.getInterpolation(newPercent);
41             prop.value = prop.mEnd * newPercent + prop.mStart * (1 - newPercent);
42         }
43         onUpdate(percent);
44     }
45 
onUpdate(float percent)46     public abstract void onUpdate(float percent);
47 
48     public final class FloatProp {
49 
50         public float value;
51 
52         private final float mStart;
53         private final float mEnd;
54         private final float mDelay;
55         private final float mDuration;
56         private final Interpolator mInterpolator;
57 
FloatProp(float start, float end, float delay, float duration, Interpolator i)58         public FloatProp(float start, float end, float delay, float duration, Interpolator i) {
59             value = mStart = start;
60             mEnd = end;
61             mDelay = delay;
62             mDuration = duration;
63             mInterpolator = i;
64 
65             mAllProperties.add(this);
66         }
67     }
68 }
69