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, false /* initOnly */); 44 } 45 46 /** 47 * @param percent The total animation progress. 48 * @param initOnly When true, only does enough work to initialize the animation. 49 */ onUpdate(float percent, boolean initOnly)50 public abstract void onUpdate(float percent, boolean initOnly); 51 52 public final class FloatProp { 53 54 public float value; 55 56 private final float mStart; 57 private final float mEnd; 58 private final float mDelay; 59 private final float mDuration; 60 private final Interpolator mInterpolator; 61 FloatProp(float start, float end, float delay, float duration, Interpolator i)62 public FloatProp(float start, float end, float delay, float duration, Interpolator i) { 63 value = mStart = start; 64 mEnd = end; 65 mDelay = delay; 66 mDuration = duration; 67 mInterpolator = i; 68 69 mAllProperties.add(this); 70 } 71 72 /** 73 * Gets the start value. 74 */ getStartValue()75 public float getStartValue() { 76 return mStart; 77 } 78 } 79 } 80