1 /* 2 * Copyright (C) 2016 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 17 package com.android.systemui.recents.views; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorSet; 21 import android.animation.ValueAnimator; 22 import android.annotation.IntDef; 23 import android.util.SparseArray; 24 import android.util.SparseLongArray; 25 import android.view.View; 26 import android.view.animation.Interpolator; 27 28 import com.android.systemui.Interpolators; 29 30 import java.lang.annotation.Retention; 31 import java.lang.annotation.RetentionPolicy; 32 import java.util.List; 33 34 /** 35 * The generic set of animation properties to animate a {@link View}. The animation can have 36 * different interpolators, start delays and durations for each of the different properties. 37 */ 38 public class AnimationProps { 39 40 public static final AnimationProps IMMEDIATE = new AnimationProps(0, Interpolators.LINEAR); 41 42 @Retention(RetentionPolicy.SOURCE) 43 @IntDef({ALL, TRANSLATION_X, TRANSLATION_Y, TRANSLATION_Z, ALPHA, SCALE, BOUNDS}) 44 public @interface PropType {} 45 46 public static final int ALL = 0; 47 public static final int TRANSLATION_X = 1; 48 public static final int TRANSLATION_Y = 2; 49 public static final int TRANSLATION_Z = 3; 50 public static final int ALPHA = 4; 51 public static final int SCALE = 5; 52 public static final int BOUNDS = 6; 53 public static final int DIM_ALPHA = 7; 54 public static final int FOCUS_STATE = 8; 55 56 private SparseLongArray mPropStartDelay; 57 private SparseLongArray mPropDuration; 58 private SparseArray<Interpolator> mPropInterpolators; 59 private Animator.AnimatorListener mListener; 60 61 /** 62 * The builder constructor. 63 */ 64 public AnimationProps() {} 65 66 /** 67 * Creates an animation with a default {@param duration} and {@param interpolator} for all 68 * properties in this animation. 69 */ 70 public AnimationProps(int duration, Interpolator interpolator) { 71 this(0, duration, interpolator, null); 72 } 73 74 /** 75 * Creates an animation with a default {@param duration} and {@param interpolator} for all 76 * properties in this animation. 77 */ 78 public AnimationProps(int duration, Interpolator interpolator, 79 Animator.AnimatorListener listener) { 80 this(0, duration, interpolator, listener); 81 } 82 83 /** 84 * Creates an animation with a default {@param startDelay}, {@param duration} and 85 * {@param interpolator} for all properties in this animation. 86 */ 87 public AnimationProps(int startDelay, int duration, Interpolator interpolator) { 88 this(startDelay, duration, interpolator, null); 89 } 90 91 /** 92 * Creates an animation with a default {@param startDelay}, {@param duration} and 93 * {@param interpolator} for all properties in this animation. 94 */ 95 public AnimationProps(int startDelay, int duration, Interpolator interpolator, 96 Animator.AnimatorListener listener) { 97 setStartDelay(ALL, startDelay); 98 setDuration(ALL, duration); 99 setInterpolator(ALL, interpolator); 100 setListener(listener); 101 } 102 103 /** 104 * Creates a new {@link AnimatorSet} that will animate the given animators. Callers need to 105 * manually apply the individual animation properties for each of the animators respectively. 106 */ 107 public AnimatorSet createAnimator(List<Animator> animators) { 108 AnimatorSet anim = new AnimatorSet(); 109 if (mListener != null) { 110 anim.addListener(mListener); 111 } 112 anim.playTogether(animators); 113 return anim; 114 } 115 116 /** 117 * Applies the specific start delay, duration and interpolator to the given {@param animator} 118 * for the specified {@param propertyType}. 119 */ 120 public <T extends ValueAnimator> T apply(@PropType int propertyType, T animator) { 121 animator.setStartDelay(getStartDelay(propertyType)); 122 animator.setDuration(getDuration(propertyType)); 123 animator.setInterpolator(getInterpolator(propertyType)); 124 return animator; 125 } 126 127 /** 128 * Sets a start delay for a specific property. 129 */ 130 public AnimationProps setStartDelay(@PropType int propertyType, int startDelay) { 131 if (mPropStartDelay == null) { 132 mPropStartDelay = new SparseLongArray(); 133 } 134 mPropStartDelay.append(propertyType, startDelay); 135 return this; 136 } 137 138 /** 139 * Returns the start delay for a specific property. 140 */ 141 public long getStartDelay(@PropType int propertyType) { 142 if (mPropStartDelay != null) { 143 long startDelay = mPropStartDelay.get(propertyType, -1); 144 if (startDelay != -1) { 145 return startDelay; 146 } 147 return mPropStartDelay.get(ALL, 0); 148 } 149 return 0; 150 } 151 152 /** 153 * Sets a duration for a specific property. 154 */ 155 public AnimationProps setDuration(@PropType int propertyType, int duration) { 156 if (mPropDuration == null) { 157 mPropDuration = new SparseLongArray(); 158 } 159 mPropDuration.append(propertyType, duration); 160 return this; 161 } 162 163 /** 164 * Returns the duration for a specific property. 165 */ 166 public long getDuration(@PropType int propertyType) { 167 if (mPropDuration != null) { 168 long duration = mPropDuration.get(propertyType, -1); 169 if (duration != -1) { 170 return duration; 171 } 172 return mPropDuration.get(ALL, 0); 173 } 174 return 0; 175 } 176 177 /** 178 * Sets an interpolator for a specific property. 179 */ 180 public AnimationProps setInterpolator(@PropType int propertyType, Interpolator interpolator) { 181 if (mPropInterpolators == null) { 182 mPropInterpolators = new SparseArray<>(); 183 } 184 mPropInterpolators.append(propertyType, interpolator); 185 return this; 186 } 187 188 /** 189 * Returns the interpolator for a specific property, falling back to the general interpolator 190 * if there is no specific property interpolator. 191 */ 192 public Interpolator getInterpolator(@PropType int propertyType) { 193 if (mPropInterpolators != null) { 194 Interpolator interp = mPropInterpolators.get(propertyType); 195 if (interp != null) { 196 return interp; 197 } 198 return mPropInterpolators.get(ALL, Interpolators.LINEAR); 199 } 200 return Interpolators.LINEAR; 201 } 202 203 /** 204 * Sets an animator listener for this animation. 205 */ 206 public AnimationProps setListener(Animator.AnimatorListener listener) { 207 mListener = listener; 208 return this; 209 } 210 211 /** 212 * Returns the animator listener for this animation. 213 */ 214 public Animator.AnimatorListener getListener() { 215 return mListener; 216 } 217 218 /** 219 * Returns whether this animation has any duration. 220 */ 221 public boolean isImmediate() { 222 int count = mPropDuration.size(); 223 for (int i = 0; i < count; i++) { 224 if (mPropDuration.valueAt(i) > 0) { 225 return false; 226 } 227 } 228 return true; 229 } 230 } 231