Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 android.animation; |
| 18 | |
| 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
| 21 | import android.os.Message; |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 22 | import android.util.AndroidRuntimeException; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 23 | import android.view.Choreographer; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 24 | import android.view.animation.AccelerateDecelerateInterpolator; |
| 25 | import android.view.animation.AnimationUtils; |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 26 | import android.view.animation.LinearInterpolator; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.HashMap; |
| 30 | |
| 31 | /** |
| 32 | * This class provides a simple timing engine for running animations |
| 33 | * which calculate animated values and set them on target objects. |
| 34 | * |
| 35 | * <p>There is a single timing pulse that all animations use. It runs in a |
| 36 | * custom handler to ensure that property changes happen on the UI thread.</p> |
| 37 | * |
| 38 | * <p>By default, ValueAnimator uses non-linear time interpolation, via the |
| 39 | * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates |
| 40 | * out of an animation. This behavior can be changed by calling |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 41 | * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 42 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 43 | public class ValueAnimator extends Animator { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Internal constants |
| 47 | */ |
| 48 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 49 | /** |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 50 | * Messages sent to timing handler: START is sent when an animation first begins. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 51 | */ |
Xavier Ducrohet | 7f9f99ea | 2011-08-11 10:16:17 -0700 | [diff] [blame] | 52 | static final int ANIMATION_START = 0; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Values used with internal variable mPlayingState to indicate the current state of an |
| 56 | * animation. |
| 57 | */ |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 58 | static final int STOPPED = 0; // Not yet playing |
| 59 | static final int RUNNING = 1; // Playing normally |
| 60 | static final int SEEKED = 2; // Seeked to some time value |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Internal variables |
| 64 | * NOTE: This object implements the clone() method, making a deep copy of any referenced |
| 65 | * objects. As other non-trivial fields are added to this class, make sure to add logic |
| 66 | * to clone() to make deep copies of them. |
| 67 | */ |
| 68 | |
| 69 | // The first time that the animation's animateFrame() method is called. This time is used to |
| 70 | // determine elapsed time (and therefore the elapsed fraction) in subsequent calls |
| 71 | // to animateFrame() |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 72 | long mStartTime; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked |
| 76 | * to a value. |
| 77 | */ |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 78 | long mSeekTime = -1; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 79 | |
| 80 | // The static sAnimationHandler processes the internal timing loop on which all animations |
| 81 | // are based |
Chet Haase | e3bc4e6 | 2010-10-27 13:18:54 -0700 | [diff] [blame] | 82 | private static ThreadLocal<AnimationHandler> sAnimationHandler = |
| 83 | new ThreadLocal<AnimationHandler>(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 84 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 85 | // The time interpolator to be used if none is set on the animation |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 86 | private static final TimeInterpolator sDefaultInterpolator = |
| 87 | new AccelerateDecelerateInterpolator(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 88 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Used to indicate whether the animation is currently playing in reverse. This causes the |
| 91 | * elapsed fraction to be inverted to calculate the appropriate values. |
| 92 | */ |
| 93 | private boolean mPlayingBackwards = false; |
| 94 | |
| 95 | /** |
| 96 | * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the |
| 97 | * repeatCount (if repeatCount!=INFINITE), the animation ends |
| 98 | */ |
| 99 | private int mCurrentIteration = 0; |
| 100 | |
| 101 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 102 | * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction(). |
| 103 | */ |
| 104 | private float mCurrentFraction = 0f; |
| 105 | |
| 106 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 107 | * Tracks whether a startDelay'd animation has begun playing through the startDelay. |
| 108 | */ |
| 109 | private boolean mStartedDelay = false; |
| 110 | |
| 111 | /** |
| 112 | * Tracks the time at which the animation began playing through its startDelay. This is |
| 113 | * different from the mStartTime variable, which is used to track when the animation became |
| 114 | * active (which is when the startDelay expired and the animation was added to the active |
| 115 | * animations list). |
| 116 | */ |
| 117 | private long mDelayStartTime; |
| 118 | |
| 119 | /** |
| 120 | * Flag that represents the current state of the animation. Used to figure out when to start |
| 121 | * an animation (if state == STOPPED). Also used to end an animation that |
| 122 | * has been cancel()'d or end()'d since the last animation frame. Possible values are |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 123 | * STOPPED, RUNNING, SEEKED. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 124 | */ |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 125 | int mPlayingState = STOPPED; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 128 | * Additional playing state to indicate whether an animator has been start()'d. There is |
| 129 | * some lag between a call to start() and the first animation frame. We should still note |
| 130 | * that the animation has been started, even if it's first animation frame has not yet |
| 131 | * happened, and reflect that state in isRunning(). |
| 132 | * Note that delayed animations are different: they are not started until their first |
| 133 | * animation frame, which occurs after their delay elapses. |
| 134 | */ |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 135 | private boolean mRunning = false; |
| 136 | |
| 137 | /** |
| 138 | * Additional playing state to indicate whether an animator has been start()'d, whether or |
| 139 | * not there is a nonzero startDelay. |
| 140 | */ |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 141 | private boolean mStarted = false; |
| 142 | |
| 143 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 144 | * Flag that denotes whether the animation is set up and ready to go. Used to |
| 145 | * set up animation that has not yet been started. |
| 146 | */ |
| 147 | boolean mInitialized = false; |
| 148 | |
| 149 | // |
| 150 | // Backing variables |
| 151 | // |
| 152 | |
| 153 | // How long the animation should last in ms |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 154 | private long mDuration = 300; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 155 | |
| 156 | // The amount of time in ms to delay starting the animation after start() is called |
| 157 | private long mStartDelay = 0; |
| 158 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 159 | // The number of times the animation will repeat. The default is 0, which means the animation |
| 160 | // will play only once |
| 161 | private int mRepeatCount = 0; |
| 162 | |
| 163 | /** |
| 164 | * The type of repetition that will occur when repeatMode is nonzero. RESTART means the |
| 165 | * animation will start from the beginning on every new cycle. REVERSE means the animation |
| 166 | * will reverse directions on each iteration. |
| 167 | */ |
| 168 | private int mRepeatMode = RESTART; |
| 169 | |
| 170 | /** |
| 171 | * The time interpolator to be used. The elapsed fraction of the animation will be passed |
| 172 | * through this interpolator to calculate the interpolated fraction, which is then used to |
| 173 | * calculate the animated values. |
| 174 | */ |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 175 | private TimeInterpolator mInterpolator = sDefaultInterpolator; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * The set of listeners to be sent events through the life of an animation. |
| 179 | */ |
| 180 | private ArrayList<AnimatorUpdateListener> mUpdateListeners = null; |
| 181 | |
| 182 | /** |
| 183 | * The property/value sets being animated. |
| 184 | */ |
| 185 | PropertyValuesHolder[] mValues; |
| 186 | |
| 187 | /** |
| 188 | * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values |
| 189 | * by property name during calls to getAnimatedValue(String). |
| 190 | */ |
| 191 | HashMap<String, PropertyValuesHolder> mValuesMap; |
| 192 | |
| 193 | /** |
| 194 | * Public constants |
| 195 | */ |
| 196 | |
| 197 | /** |
| 198 | * When the animation reaches the end and <code>repeatCount</code> is INFINITE |
| 199 | * or a positive value, the animation restarts from the beginning. |
| 200 | */ |
| 201 | public static final int RESTART = 1; |
| 202 | /** |
| 203 | * When the animation reaches the end and <code>repeatCount</code> is INFINITE |
| 204 | * or a positive value, the animation reverses direction on every iteration. |
| 205 | */ |
| 206 | public static final int REVERSE = 2; |
| 207 | /** |
| 208 | * This value used used with the {@link #setRepeatCount(int)} property to repeat |
| 209 | * the animation indefinitely. |
| 210 | */ |
| 211 | public static final int INFINITE = -1; |
| 212 | |
| 213 | /** |
| 214 | * Creates a new ValueAnimator object. This default constructor is primarily for |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 215 | * use internally; the factory methods which take parameters are more generally |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 216 | * useful. |
| 217 | */ |
| 218 | public ValueAnimator() { |
| 219 | } |
| 220 | |
| 221 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 222 | * Constructs and returns a ValueAnimator that animates between int values. A single |
| 223 | * value implies that that value is the one being animated to. However, this is not typically |
| 224 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 225 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 226 | * from the target object and property being animated). Therefore, there should typically |
| 227 | * be two or more values. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 228 | * |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 229 | * @param values A set of values that the animation will animate between over time. |
| 230 | * @return A ValueAnimator object that is set up to animate between the given values. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 231 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 232 | public static ValueAnimator ofInt(int... values) { |
| 233 | ValueAnimator anim = new ValueAnimator(); |
| 234 | anim.setIntValues(values); |
| 235 | return anim; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Constructs and returns a ValueAnimator that animates between float values. A single |
| 240 | * value implies that that value is the one being animated to. However, this is not typically |
| 241 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 242 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 243 | * from the target object and property being animated). Therefore, there should typically |
| 244 | * be two or more values. |
| 245 | * |
| 246 | * @param values A set of values that the animation will animate between over time. |
| 247 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 248 | */ |
| 249 | public static ValueAnimator ofFloat(float... values) { |
| 250 | ValueAnimator anim = new ValueAnimator(); |
| 251 | anim.setFloatValues(values); |
| 252 | return anim; |
| 253 | } |
| 254 | |
| 255 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 256 | * Constructs and returns a ValueAnimator that animates between the values |
| 257 | * specified in the PropertyValuesHolder objects. |
| 258 | * |
| 259 | * @param values A set of PropertyValuesHolder objects whose values will be animated |
| 260 | * between over time. |
| 261 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 262 | */ |
| 263 | public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) { |
| 264 | ValueAnimator anim = new ValueAnimator(); |
| 265 | anim.setValues(values); |
| 266 | return anim; |
| 267 | } |
| 268 | /** |
| 269 | * Constructs and returns a ValueAnimator that animates between Object values. A single |
| 270 | * value implies that that value is the one being animated to. However, this is not typically |
| 271 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 272 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 273 | * from the target object and property being animated). Therefore, there should typically |
| 274 | * be two or more values. |
| 275 | * |
| 276 | * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this |
| 277 | * factory method also takes a TypeEvaluator object that the ValueAnimator will use |
| 278 | * to perform that interpolation. |
| 279 | * |
| 280 | * @param evaluator A TypeEvaluator that will be called on each animation frame to |
| 281 | * provide the ncessry interpolation between the Object values to derive the animated |
| 282 | * value. |
| 283 | * @param values A set of values that the animation will animate between over time. |
| 284 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 285 | */ |
| 286 | public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) { |
| 287 | ValueAnimator anim = new ValueAnimator(); |
| 288 | anim.setObjectValues(values); |
| 289 | anim.setEvaluator(evaluator); |
| 290 | return anim; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Sets int values that will be animated between. A single |
| 295 | * value implies that that value is the one being animated to. However, this is not typically |
| 296 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 297 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 298 | * from the target object and property being animated). Therefore, there should typically |
| 299 | * be two or more values. |
| 300 | * |
| 301 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 302 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 303 | * of those objects.</p> |
| 304 | * |
| 305 | * @param values A set of values that the animation will animate between over time. |
| 306 | */ |
| 307 | public void setIntValues(int... values) { |
| 308 | if (values == null || values.length == 0) { |
| 309 | return; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 310 | } |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 311 | if (mValues == null || mValues.length == 0) { |
| 312 | setValues(new PropertyValuesHolder[]{PropertyValuesHolder.ofInt("", values)}); |
| 313 | } else { |
| 314 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 315 | valuesHolder.setIntValues(values); |
| 316 | } |
| 317 | // New property/values/target should cause re-initialization prior to starting |
| 318 | mInitialized = false; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Sets float values that will be animated between. A single |
| 323 | * value implies that that value is the one being animated to. However, this is not typically |
| 324 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 325 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 326 | * from the target object and property being animated). Therefore, there should typically |
| 327 | * be two or more values. |
| 328 | * |
| 329 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 330 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 331 | * of those objects.</p> |
| 332 | * |
| 333 | * @param values A set of values that the animation will animate between over time. |
| 334 | */ |
| 335 | public void setFloatValues(float... values) { |
| 336 | if (values == null || values.length == 0) { |
| 337 | return; |
| 338 | } |
| 339 | if (mValues == null || mValues.length == 0) { |
| 340 | setValues(new PropertyValuesHolder[]{PropertyValuesHolder.ofFloat("", values)}); |
| 341 | } else { |
| 342 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 343 | valuesHolder.setFloatValues(values); |
| 344 | } |
| 345 | // New property/values/target should cause re-initialization prior to starting |
| 346 | mInitialized = false; |
| 347 | } |
| 348 | |
| 349 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 350 | * Sets the values to animate between for this animation. A single |
| 351 | * value implies that that value is the one being animated to. However, this is not typically |
| 352 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 353 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 354 | * from the target object and property being animated). Therefore, there should typically |
| 355 | * be two or more values. |
| 356 | * |
| 357 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 358 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 359 | * of those objects.</p> |
| 360 | * |
| 361 | * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate |
| 362 | * between these value objects. ValueAnimator only knows how to interpolate between the |
| 363 | * primitive types specified in the other setValues() methods.</p> |
| 364 | * |
| 365 | * @param values The set of values to animate between. |
| 366 | */ |
| 367 | public void setObjectValues(Object... values) { |
| 368 | if (values == null || values.length == 0) { |
| 369 | return; |
| 370 | } |
| 371 | if (mValues == null || mValues.length == 0) { |
| 372 | setValues(new PropertyValuesHolder[]{PropertyValuesHolder.ofObject("", |
| 373 | (TypeEvaluator)null, values)}); |
| 374 | } else { |
| 375 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 376 | valuesHolder.setObjectValues(values); |
| 377 | } |
| 378 | // New property/values/target should cause re-initialization prior to starting |
| 379 | mInitialized = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Sets the values, per property, being animated between. This function is called internally |
| 384 | * by the constructors of ValueAnimator that take a list of values. But an ValueAnimator can |
| 385 | * be constructed without values and this method can be called to set the values manually |
| 386 | * instead. |
| 387 | * |
| 388 | * @param values The set of values, per property, being animated between. |
| 389 | */ |
| 390 | public void setValues(PropertyValuesHolder... values) { |
| 391 | int numValues = values.length; |
| 392 | mValues = values; |
| 393 | mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); |
| 394 | for (int i = 0; i < numValues; ++i) { |
| 395 | PropertyValuesHolder valuesHolder = (PropertyValuesHolder) values[i]; |
| 396 | mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder); |
| 397 | } |
Chet Haase | 0e0590b | 2010-09-26 11:57:28 -0700 | [diff] [blame] | 398 | // New property/values/target should cause re-initialization prior to starting |
| 399 | mInitialized = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Returns the values that this ValueAnimator animates between. These values are stored in |
| 404 | * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list |
| 405 | * of value objects instead. |
| 406 | * |
| 407 | * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the |
| 408 | * values, per property, that define the animation. |
| 409 | */ |
| 410 | public PropertyValuesHolder[] getValues() { |
| 411 | return mValues; |
| 412 | } |
| 413 | |
| 414 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 415 | * This function is called immediately before processing the first animation |
| 416 | * frame of an animation. If there is a nonzero <code>startDelay</code>, the |
| 417 | * function is called after that delay ends. |
| 418 | * It takes care of the final initialization steps for the |
| 419 | * animation. |
| 420 | * |
| 421 | * <p>Overrides of this method should call the superclass method to ensure |
| 422 | * that internal mechanisms for the animation are set up correctly.</p> |
| 423 | */ |
| 424 | void initAnimation() { |
| 425 | if (!mInitialized) { |
| 426 | int numValues = mValues.length; |
| 427 | for (int i = 0; i < numValues; ++i) { |
| 428 | mValues[i].init(); |
| 429 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 430 | mInitialized = true; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| 435 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 436 | * Sets the length of the animation. The default duration is 300 milliseconds. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 437 | * |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 438 | * @param duration The length of the animation, in milliseconds. This value cannot |
| 439 | * be negative. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 440 | * @return ValueAnimator The object called with setDuration(). This return |
| 441 | * value makes it easier to compose statements together that construct and then set the |
| 442 | * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 443 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 444 | public ValueAnimator setDuration(long duration) { |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 445 | if (duration < 0) { |
| 446 | throw new IllegalArgumentException("Animators cannot have negative duration: " + |
| 447 | duration); |
| 448 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 449 | mDuration = duration; |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 450 | return this; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 454 | * Gets the length of the animation. The default duration is 300 milliseconds. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 455 | * |
| 456 | * @return The length of the animation, in milliseconds. |
| 457 | */ |
| 458 | public long getDuration() { |
| 459 | return mDuration; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Sets the position of the animation to the specified point in time. This time should |
| 464 | * be between 0 and the total duration of the animation, including any repetition. If |
| 465 | * the animation has not yet been started, then it will not advance forward after it is |
| 466 | * set to this time; it will simply set the time to this value and perform any appropriate |
| 467 | * actions based on that time. If the animation is already running, then setCurrentPlayTime() |
| 468 | * will set the current playing time to this value and continue playing from that point. |
| 469 | * |
| 470 | * @param playTime The time, in milliseconds, to which the animation is advanced or rewound. |
| 471 | */ |
| 472 | public void setCurrentPlayTime(long playTime) { |
| 473 | initAnimation(); |
| 474 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 475 | if (mPlayingState != RUNNING) { |
| 476 | mSeekTime = playTime; |
| 477 | mPlayingState = SEEKED; |
| 478 | } |
| 479 | mStartTime = currentTime - playTime; |
| 480 | animationFrame(currentTime); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Gets the current position of the animation in time, which is equal to the current |
| 485 | * time minus the time that the animation started. An animation that is not yet started will |
| 486 | * return a value of zero. |
| 487 | * |
| 488 | * @return The current position in time of the animation. |
| 489 | */ |
| 490 | public long getCurrentPlayTime() { |
| 491 | if (!mInitialized || mPlayingState == STOPPED) { |
| 492 | return 0; |
| 493 | } |
| 494 | return AnimationUtils.currentAnimationTimeMillis() - mStartTime; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * This custom, static handler handles the timing pulse that is shared by |
| 499 | * all active animations. This approach ensures that the setting of animation |
| 500 | * values will happen on the UI thread and that all animations will share |
| 501 | * the same times for calculating their values, which makes synchronizing |
| 502 | * animations possible. |
| 503 | * |
| 504 | */ |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 505 | private static class AnimationHandler extends Handler |
| 506 | implements Choreographer.OnAnimateListener { |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 507 | // The per-thread list of all active animations |
| 508 | private final ArrayList<ValueAnimator> mAnimations = new ArrayList<ValueAnimator>(); |
| 509 | |
| 510 | // The per-thread set of animations to be started on the next animation frame |
| 511 | private final ArrayList<ValueAnimator> mPendingAnimations = new ArrayList<ValueAnimator>(); |
| 512 | |
| 513 | /** |
| 514 | * Internal per-thread collections used to avoid set collisions as animations start and end |
| 515 | * while being processed. |
| 516 | */ |
| 517 | private final ArrayList<ValueAnimator> mDelayedAnims = new ArrayList<ValueAnimator>(); |
| 518 | private final ArrayList<ValueAnimator> mEndingAnims = new ArrayList<ValueAnimator>(); |
| 519 | private final ArrayList<ValueAnimator> mReadyAnims = new ArrayList<ValueAnimator>(); |
| 520 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 521 | private final Choreographer mChoreographer; |
| 522 | private boolean mIsChoreographed; |
| 523 | |
| 524 | private AnimationHandler() { |
| 525 | mChoreographer = Choreographer.getInstance(); |
| 526 | } |
| 527 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 528 | /** |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 529 | * The START message is sent when an animation's start() method is called. |
| 530 | * It cannot start synchronously when start() is called |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 531 | * because the call may be on the wrong thread, and it would also not be |
| 532 | * synchronized with other animations because it would not start on a common |
| 533 | * timing pulse. So each animation sends a START message to the handler, which |
| 534 | * causes the handler to place the animation on the active animations queue and |
| 535 | * start processing frames for that animation. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 536 | */ |
| 537 | @Override |
| 538 | public void handleMessage(Message msg) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 539 | switch (msg.what) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 540 | case ANIMATION_START: |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 541 | doAnimationStart(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 542 | break; |
| 543 | } |
| 544 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 545 | |
| 546 | private void doAnimationStart() { |
| 547 | // mPendingAnimations holds any animations that have requested to be started |
| 548 | // We're going to clear mPendingAnimations, but starting animation may |
| 549 | // cause more to be added to the pending list (for example, if one animation |
| 550 | // starting triggers another starting). So we loop until mPendingAnimations |
| 551 | // is empty. |
| 552 | while (mPendingAnimations.size() > 0) { |
| 553 | ArrayList<ValueAnimator> pendingCopy = |
| 554 | (ArrayList<ValueAnimator>) mPendingAnimations.clone(); |
| 555 | mPendingAnimations.clear(); |
| 556 | int count = pendingCopy.size(); |
| 557 | for (int i = 0; i < count; ++i) { |
| 558 | ValueAnimator anim = pendingCopy.get(i); |
| 559 | // If the animation has a startDelay, place it on the delayed list |
| 560 | if (anim.mStartDelay == 0) { |
| 561 | anim.startAnimation(this); |
| 562 | } else { |
| 563 | mDelayedAnims.add(anim); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | doAnimationFrame(); |
| 568 | } |
| 569 | |
| 570 | private void doAnimationFrame() { |
| 571 | // currentTime holds the common time for all animations processed |
| 572 | // during this frame |
| 573 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 574 | |
| 575 | // First, process animations currently sitting on the delayed queue, adding |
| 576 | // them to the active animations if they are ready |
| 577 | int numDelayedAnims = mDelayedAnims.size(); |
| 578 | for (int i = 0; i < numDelayedAnims; ++i) { |
| 579 | ValueAnimator anim = mDelayedAnims.get(i); |
| 580 | if (anim.delayedAnimationFrame(currentTime)) { |
| 581 | mReadyAnims.add(anim); |
| 582 | } |
| 583 | } |
| 584 | int numReadyAnims = mReadyAnims.size(); |
| 585 | if (numReadyAnims > 0) { |
| 586 | for (int i = 0; i < numReadyAnims; ++i) { |
| 587 | ValueAnimator anim = mReadyAnims.get(i); |
| 588 | anim.startAnimation(this); |
| 589 | anim.mRunning = true; |
| 590 | mDelayedAnims.remove(anim); |
| 591 | } |
| 592 | mReadyAnims.clear(); |
| 593 | } |
| 594 | |
| 595 | // Now process all active animations. The return value from animationFrame() |
| 596 | // tells the handler whether it should now be ended |
| 597 | int numAnims = mAnimations.size(); |
| 598 | int i = 0; |
| 599 | while (i < numAnims) { |
| 600 | ValueAnimator anim = mAnimations.get(i); |
| 601 | if (anim.animationFrame(currentTime)) { |
| 602 | mEndingAnims.add(anim); |
| 603 | } |
| 604 | if (mAnimations.size() == numAnims) { |
| 605 | ++i; |
| 606 | } else { |
| 607 | // An animation might be canceled or ended by client code |
| 608 | // during the animation frame. Check to see if this happened by |
| 609 | // seeing whether the current index is the same as it was before |
| 610 | // calling animationFrame(). Another approach would be to copy |
| 611 | // animations to a temporary list and process that list instead, |
| 612 | // but that entails garbage and processing overhead that would |
| 613 | // be nice to avoid. |
| 614 | --numAnims; |
| 615 | mEndingAnims.remove(anim); |
| 616 | } |
| 617 | } |
| 618 | if (mEndingAnims.size() > 0) { |
| 619 | for (i = 0; i < mEndingAnims.size(); ++i) { |
| 620 | mEndingAnims.get(i).endAnimation(this); |
| 621 | } |
| 622 | mEndingAnims.clear(); |
| 623 | } |
| 624 | |
| 625 | // If there are still active or delayed animations, schedule a future call to |
| 626 | // onAnimate to process the next frame of the animations. |
| 627 | if (!mAnimations.isEmpty() || !mDelayedAnims.isEmpty()) { |
| 628 | if (!mIsChoreographed) { |
| 629 | mIsChoreographed = true; |
| 630 | mChoreographer.addOnAnimateListener(this); |
| 631 | } |
| 632 | mChoreographer.scheduleAnimation(); |
| 633 | } else { |
| 634 | if (mIsChoreographed) { |
| 635 | mIsChoreographed = false; |
| 636 | mChoreographer.removeOnAnimateListener(this); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public void onAnimate() { |
| 643 | doAnimationFrame(); |
| 644 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
| 648 | * The amount of time, in milliseconds, to delay starting the animation after |
| 649 | * {@link #start()} is called. |
| 650 | * |
| 651 | * @return the number of milliseconds to delay running the animation |
| 652 | */ |
| 653 | public long getStartDelay() { |
| 654 | return mStartDelay; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * The amount of time, in milliseconds, to delay starting the animation after |
| 659 | * {@link #start()} is called. |
| 660 | |
| 661 | * @param startDelay The amount of the delay, in milliseconds |
| 662 | */ |
| 663 | public void setStartDelay(long startDelay) { |
| 664 | this.mStartDelay = startDelay; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 669 | * requested time that the animation will attempt to honor, but the actual delay between |
| 670 | * frames may be different, depending on system load and capabilities. This is a static |
| 671 | * function because the same delay will be applied to all animations, since they are all |
| 672 | * run off of a single timing loop. |
| 673 | * |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 674 | * The frame delay may be ignored when the animation system uses an external timing |
| 675 | * source, such as the display refresh rate (vsync), to govern animations. |
| 676 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 677 | * @return the requested time between frames, in milliseconds |
| 678 | */ |
| 679 | public static long getFrameDelay() { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 680 | return Choreographer.getFrameDelay(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /** |
| 684 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 685 | * requested time that the animation will attempt to honor, but the actual delay between |
| 686 | * frames may be different, depending on system load and capabilities. This is a static |
| 687 | * function because the same delay will be applied to all animations, since they are all |
| 688 | * run off of a single timing loop. |
| 689 | * |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 690 | * The frame delay may be ignored when the animation system uses an external timing |
| 691 | * source, such as the display refresh rate (vsync), to govern animations. |
| 692 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 693 | * @param frameDelay the requested time between frames, in milliseconds |
| 694 | */ |
| 695 | public static void setFrameDelay(long frameDelay) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 696 | Choreographer.setFrameDelay(frameDelay); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /** |
| 700 | * The most recent value calculated by this <code>ValueAnimator</code> when there is just one |
| 701 | * property being animated. This value is only sensible while the animation is running. The main |
| 702 | * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code> |
| 703 | * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which |
| 704 | * is called during each animation frame, immediately after the value is calculated. |
| 705 | * |
| 706 | * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for |
| 707 | * the single property being animated. If there are several properties being animated |
| 708 | * (specified by several PropertyValuesHolder objects in the constructor), this function |
| 709 | * returns the animated value for the first of those objects. |
| 710 | */ |
| 711 | public Object getAnimatedValue() { |
| 712 | if (mValues != null && mValues.length > 0) { |
| 713 | return mValues[0].getAnimatedValue(); |
| 714 | } |
| 715 | // Shouldn't get here; should always have values unless ValueAnimator was set up wrong |
| 716 | return null; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>. |
| 721 | * The main purpose for this read-only property is to retrieve the value from the |
| 722 | * <code>ValueAnimator</code> during a call to |
| 723 | * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which |
| 724 | * is called during each animation frame, immediately after the value is calculated. |
| 725 | * |
| 726 | * @return animatedValue The value most recently calculated for the named property |
| 727 | * by this <code>ValueAnimator</code>. |
| 728 | */ |
| 729 | public Object getAnimatedValue(String propertyName) { |
| 730 | PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName); |
| 731 | if (valuesHolder != null) { |
| 732 | return valuesHolder.getAnimatedValue(); |
| 733 | } else { |
| 734 | // At least avoid crashing if called with bogus propertyName |
| 735 | return null; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Sets how many times the animation should be repeated. If the repeat |
| 741 | * count is 0, the animation is never repeated. If the repeat count is |
| 742 | * greater than 0 or {@link #INFINITE}, the repeat mode will be taken |
| 743 | * into account. The repeat count is 0 by default. |
| 744 | * |
| 745 | * @param value the number of times the animation should be repeated |
| 746 | */ |
| 747 | public void setRepeatCount(int value) { |
| 748 | mRepeatCount = value; |
| 749 | } |
| 750 | /** |
| 751 | * Defines how many times the animation should repeat. The default value |
| 752 | * is 0. |
| 753 | * |
| 754 | * @return the number of times the animation should repeat, or {@link #INFINITE} |
| 755 | */ |
| 756 | public int getRepeatCount() { |
| 757 | return mRepeatCount; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Defines what this animation should do when it reaches the end. This |
| 762 | * setting is applied only when the repeat count is either greater than |
| 763 | * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. |
| 764 | * |
| 765 | * @param value {@link #RESTART} or {@link #REVERSE} |
| 766 | */ |
| 767 | public void setRepeatMode(int value) { |
| 768 | mRepeatMode = value; |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * Defines what this animation should do when it reaches the end. |
| 773 | * |
| 774 | * @return either one of {@link #REVERSE} or {@link #RESTART} |
| 775 | */ |
| 776 | public int getRepeatMode() { |
| 777 | return mRepeatMode; |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Adds a listener to the set of listeners that are sent update events through the life of |
| 782 | * an animation. This method is called on all listeners for every frame of the animation, |
| 783 | * after the values for the animation have been calculated. |
| 784 | * |
| 785 | * @param listener the listener to be added to the current set of listeners for this animation. |
| 786 | */ |
| 787 | public void addUpdateListener(AnimatorUpdateListener listener) { |
| 788 | if (mUpdateListeners == null) { |
| 789 | mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); |
| 790 | } |
| 791 | mUpdateListeners.add(listener); |
| 792 | } |
| 793 | |
| 794 | /** |
Jim Miller | 3060421 | 2010-09-22 19:56:23 -0700 | [diff] [blame] | 795 | * Removes all listeners from the set listening to frame updates for this animation. |
| 796 | */ |
| 797 | public void removeAllUpdateListeners() { |
| 798 | if (mUpdateListeners == null) { |
| 799 | return; |
| 800 | } |
| 801 | mUpdateListeners.clear(); |
| 802 | mUpdateListeners = null; |
| 803 | } |
| 804 | |
| 805 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 806 | * Removes a listener from the set listening to frame updates for this animation. |
| 807 | * |
| 808 | * @param listener the listener to be removed from the current set of update listeners |
| 809 | * for this animation. |
| 810 | */ |
| 811 | public void removeUpdateListener(AnimatorUpdateListener listener) { |
| 812 | if (mUpdateListeners == null) { |
| 813 | return; |
| 814 | } |
| 815 | mUpdateListeners.remove(listener); |
| 816 | if (mUpdateListeners.size() == 0) { |
| 817 | mUpdateListeners = null; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | |
| 822 | /** |
| 823 | * The time interpolator used in calculating the elapsed fraction of this animation. The |
| 824 | * interpolator determines whether the animation runs with linear or non-linear motion, |
| 825 | * such as acceleration and deceleration. The default value is |
| 826 | * {@link android.view.animation.AccelerateDecelerateInterpolator} |
| 827 | * |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 828 | * @param value the interpolator to be used by this animation. A value of <code>null</code> |
| 829 | * will result in linear interpolation. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 830 | */ |
| 831 | @Override |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 832 | public void setInterpolator(TimeInterpolator value) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 833 | if (value != null) { |
| 834 | mInterpolator = value; |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 835 | } else { |
| 836 | mInterpolator = new LinearInterpolator(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Returns the timing interpolator that this ValueAnimator uses. |
| 842 | * |
| 843 | * @return The timing interpolator for this ValueAnimator. |
| 844 | */ |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 845 | public TimeInterpolator getInterpolator() { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 846 | return mInterpolator; |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * The type evaluator to be used when calculating the animated values of this animation. |
Chet Haase | b2ab04f | 2011-01-16 11:03:22 -0800 | [diff] [blame] | 851 | * The system will automatically assign a float or int evaluator based on the type |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 852 | * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values |
| 853 | * are not one of these primitive types, or if different evaluation is desired (such as is |
| 854 | * necessary with int values that represent colors), a custom evaluator needs to be assigned. |
Chet Haase | 53ee331 | 2011-01-10 15:56:56 -0800 | [diff] [blame] | 855 | * For example, when running an animation on color values, the {@link ArgbEvaluator} |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 856 | * should be used to get correct RGB color interpolation. |
| 857 | * |
| 858 | * <p>If this ValueAnimator has only one set of values being animated between, this evaluator |
| 859 | * will be used for that set. If there are several sets of values being animated, which is |
| 860 | * the case if PropertyValuesHOlder objects were set on the ValueAnimator, then the evaluator |
| 861 | * is assigned just to the first PropertyValuesHolder object.</p> |
| 862 | * |
| 863 | * @param value the evaluator to be used this animation |
| 864 | */ |
| 865 | public void setEvaluator(TypeEvaluator value) { |
| 866 | if (value != null && mValues != null && mValues.length > 0) { |
| 867 | mValues[0].setEvaluator(value); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * Start the animation playing. This version of start() takes a boolean flag that indicates |
| 873 | * whether the animation should play in reverse. The flag is usually false, but may be set |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 874 | * to true if called from the reverse() method. |
| 875 | * |
| 876 | * <p>The animation started by calling this method will be run on the thread that called |
| 877 | * this method. This thread should have a Looper on it (a runtime exception will be thrown if |
| 878 | * this is not the case). Also, if the animation will animate |
| 879 | * properties of objects in the view hierarchy, then the calling thread should be the UI |
| 880 | * thread for that view hierarchy.</p> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 881 | * |
| 882 | * @param playBackwards Whether the ValueAnimator should start playing in reverse. |
| 883 | */ |
| 884 | private void start(boolean playBackwards) { |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 885 | if (Looper.myLooper() == null) { |
| 886 | throw new AndroidRuntimeException("Animators may only be run on Looper threads"); |
Jim Miller | 3060421 | 2010-09-22 19:56:23 -0700 | [diff] [blame] | 887 | } |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 888 | mPlayingBackwards = playBackwards; |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 889 | mCurrentIteration = 0; |
| 890 | mPlayingState = STOPPED; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 891 | mStarted = true; |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 892 | mStartedDelay = false; |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 893 | AnimationHandler animationHandler = getOrCreateAnimationHandler(); |
| 894 | animationHandler.mPendingAnimations.add(this); |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 895 | if (mStartDelay == 0) { |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 896 | // This sets the initial value of the animation, prior to actually starting it running |
| 897 | setCurrentPlayTime(getCurrentPlayTime()); |
Chet Haase | 154f145 | 2011-02-23 16:53:18 -0800 | [diff] [blame] | 898 | mPlayingState = STOPPED; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 899 | mRunning = true; |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 900 | |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 901 | if (mListeners != null) { |
| 902 | ArrayList<AnimatorListener> tmpListeners = |
| 903 | (ArrayList<AnimatorListener>) mListeners.clone(); |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 904 | int numListeners = tmpListeners.size(); |
| 905 | for (int i = 0; i < numListeners; ++i) { |
| 906 | tmpListeners.get(i).onAnimationStart(this); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 909 | } |
Chet Haase | e3bc4e6 | 2010-10-27 13:18:54 -0700 | [diff] [blame] | 910 | animationHandler.sendEmptyMessage(ANIMATION_START); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | @Override |
| 914 | public void start() { |
| 915 | start(false); |
| 916 | } |
| 917 | |
| 918 | @Override |
| 919 | public void cancel() { |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 920 | // Only cancel if the animation is actually running or has been started and is about |
| 921 | // to run |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 922 | AnimationHandler handler = getOrCreateAnimationHandler(); |
| 923 | if (mPlayingState != STOPPED |
| 924 | || handler.mPendingAnimations.contains(this) |
| 925 | || handler.mDelayedAnims.contains(this)) { |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 926 | // Only notify listeners if the animator has actually started |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 927 | if (mRunning && mListeners != null) { |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 928 | ArrayList<AnimatorListener> tmpListeners = |
| 929 | (ArrayList<AnimatorListener>) mListeners.clone(); |
| 930 | for (AnimatorListener listener : tmpListeners) { |
| 931 | listener.onAnimationCancel(this); |
| 932 | } |
| 933 | } |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 934 | endAnimation(handler); |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 935 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | @Override |
| 939 | public void end() { |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 940 | AnimationHandler handler = getOrCreateAnimationHandler(); |
| 941 | if (!handler.mAnimations.contains(this) && !handler.mPendingAnimations.contains(this)) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 942 | // Special case if the animation has not yet started; get it ready for ending |
| 943 | mStartedDelay = false; |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 944 | startAnimation(handler); |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 945 | } else if (!mInitialized) { |
| 946 | initAnimation(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 947 | } |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 948 | // The final value set on the target varies, depending on whether the animation |
| 949 | // was supposed to repeat an odd number of times |
| 950 | if (mRepeatCount > 0 && (mRepeatCount & 0x01) == 1) { |
| 951 | animateValue(0f); |
| 952 | } else { |
| 953 | animateValue(1f); |
| 954 | } |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 955 | endAnimation(handler); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | @Override |
| 959 | public boolean isRunning() { |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 960 | return (mPlayingState == RUNNING || mRunning); |
| 961 | } |
| 962 | |
| 963 | @Override |
| 964 | public boolean isStarted() { |
| 965 | return mStarted; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /** |
| 969 | * Plays the ValueAnimator in reverse. If the animation is already running, |
| 970 | * it will stop itself and play backwards from the point reached when reverse was called. |
| 971 | * If the animation is not currently running, then it will start from the end and |
| 972 | * play backwards. This behavior is only set for the current animation; future playing |
| 973 | * of the animation will use the default behavior of playing forward. |
| 974 | */ |
| 975 | public void reverse() { |
| 976 | mPlayingBackwards = !mPlayingBackwards; |
| 977 | if (mPlayingState == RUNNING) { |
| 978 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 979 | long currentPlayTime = currentTime - mStartTime; |
| 980 | long timeLeft = mDuration - currentPlayTime; |
| 981 | mStartTime = currentTime - timeLeft; |
| 982 | } else { |
| 983 | start(true); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * Called internally to end an animation by removing it from the animations list. Must be |
| 989 | * called on the UI thread. |
| 990 | */ |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 991 | private void endAnimation(AnimationHandler handler) { |
| 992 | handler.mAnimations.remove(this); |
| 993 | handler.mPendingAnimations.remove(this); |
| 994 | handler.mDelayedAnims.remove(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 995 | mPlayingState = STOPPED; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 996 | if (mRunning && mListeners != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 997 | ArrayList<AnimatorListener> tmpListeners = |
| 998 | (ArrayList<AnimatorListener>) mListeners.clone(); |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 999 | int numListeners = tmpListeners.size(); |
| 1000 | for (int i = 0; i < numListeners; ++i) { |
| 1001 | tmpListeners.get(i).onAnimationEnd(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1002 | } |
| 1003 | } |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 1004 | mRunning = false; |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 1005 | mStarted = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Called internally to start an animation by adding it to the active animations list. Must be |
| 1010 | * called on the UI thread. |
| 1011 | */ |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1012 | private void startAnimation(AnimationHandler handler) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1013 | initAnimation(); |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1014 | handler.mAnimations.add(this); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1015 | if (mStartDelay > 0 && mListeners != null) { |
| 1016 | // Listeners were already notified in start() if startDelay is 0; this is |
| 1017 | // just for delayed animations |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1018 | ArrayList<AnimatorListener> tmpListeners = |
| 1019 | (ArrayList<AnimatorListener>) mListeners.clone(); |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 1020 | int numListeners = tmpListeners.size(); |
| 1021 | for (int i = 0; i < numListeners; ++i) { |
| 1022 | tmpListeners.get(i).onAnimationStart(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * Internal function called to process an animation frame on an animation that is currently |
| 1029 | * sleeping through its <code>startDelay</code> phase. The return value indicates whether it |
| 1030 | * should be woken up and put on the active animations queue. |
| 1031 | * |
| 1032 | * @param currentTime The current animation time, used to calculate whether the animation |
| 1033 | * has exceeded its <code>startDelay</code> and should be started. |
| 1034 | * @return True if the animation's <code>startDelay</code> has been exceeded and the animation |
| 1035 | * should be added to the set of active animations. |
| 1036 | */ |
| 1037 | private boolean delayedAnimationFrame(long currentTime) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1038 | if (!mStartedDelay) { |
| 1039 | mStartedDelay = true; |
| 1040 | mDelayStartTime = currentTime; |
| 1041 | } else { |
| 1042 | long deltaTime = currentTime - mDelayStartTime; |
| 1043 | if (deltaTime > mStartDelay) { |
| 1044 | // startDelay ended - start the anim and record the |
| 1045 | // mStartTime appropriately |
| 1046 | mStartTime = currentTime - (deltaTime - mStartDelay); |
| 1047 | mPlayingState = RUNNING; |
| 1048 | return true; |
| 1049 | } |
| 1050 | } |
| 1051 | return false; |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * This internal function processes a single animation frame for a given animation. The |
| 1056 | * currentTime parameter is the timing pulse sent by the handler, used to calculate the |
| 1057 | * elapsed duration, and therefore |
| 1058 | * the elapsed fraction, of the animation. The return value indicates whether the animation |
| 1059 | * should be ended (which happens when the elapsed time of the animation exceeds the |
| 1060 | * animation's duration, including the repeatCount). |
| 1061 | * |
| 1062 | * @param currentTime The current time, as tracked by the static timing handler |
| 1063 | * @return true if the animation's duration, including any repetitions due to |
| 1064 | * <code>repeatCount</code> has been exceeded and the animation should be ended. |
| 1065 | */ |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 1066 | boolean animationFrame(long currentTime) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1067 | boolean done = false; |
| 1068 | |
| 1069 | if (mPlayingState == STOPPED) { |
| 1070 | mPlayingState = RUNNING; |
| 1071 | if (mSeekTime < 0) { |
| 1072 | mStartTime = currentTime; |
| 1073 | } else { |
| 1074 | mStartTime = currentTime - mSeekTime; |
| 1075 | // Now that we're playing, reset the seek time |
| 1076 | mSeekTime = -1; |
| 1077 | } |
| 1078 | } |
| 1079 | switch (mPlayingState) { |
| 1080 | case RUNNING: |
| 1081 | case SEEKED: |
Chet Haase | 70d4ba1 | 2010-10-06 09:46:45 -0700 | [diff] [blame] | 1082 | float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1083 | if (fraction >= 1f) { |
| 1084 | if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) { |
| 1085 | // Time to repeat |
| 1086 | if (mListeners != null) { |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 1087 | int numListeners = mListeners.size(); |
| 1088 | for (int i = 0; i < numListeners; ++i) { |
| 1089 | mListeners.get(i).onAnimationRepeat(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1092 | if (mRepeatMode == REVERSE) { |
| 1093 | mPlayingBackwards = mPlayingBackwards ? false : true; |
| 1094 | } |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 1095 | mCurrentIteration += (int)fraction; |
| 1096 | fraction = fraction % 1f; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1097 | mStartTime += mDuration; |
| 1098 | } else { |
| 1099 | done = true; |
| 1100 | fraction = Math.min(fraction, 1.0f); |
| 1101 | } |
| 1102 | } |
| 1103 | if (mPlayingBackwards) { |
| 1104 | fraction = 1f - fraction; |
| 1105 | } |
| 1106 | animateValue(fraction); |
| 1107 | break; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | return done; |
| 1111 | } |
| 1112 | |
| 1113 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 1114 | * Returns the current animation fraction, which is the elapsed/interpolated fraction used in |
| 1115 | * the most recent frame update on the animation. |
| 1116 | * |
| 1117 | * @return Elapsed/interpolated fraction of the animation. |
| 1118 | */ |
| 1119 | public float getAnimatedFraction() { |
| 1120 | return mCurrentFraction; |
| 1121 | } |
| 1122 | |
| 1123 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1124 | * This method is called with the elapsed fraction of the animation during every |
| 1125 | * animation frame. This function turns the elapsed fraction into an interpolated fraction |
| 1126 | * and then into an animated value (from the evaluator. The function is called mostly during |
| 1127 | * animation updates, but it is also called when the <code>end()</code> |
| 1128 | * function is called, to set the final value on the property. |
| 1129 | * |
| 1130 | * <p>Overrides of this method must call the superclass to perform the calculation |
| 1131 | * of the animated value.</p> |
| 1132 | * |
| 1133 | * @param fraction The elapsed fraction of the animation. |
| 1134 | */ |
| 1135 | void animateValue(float fraction) { |
| 1136 | fraction = mInterpolator.getInterpolation(fraction); |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 1137 | mCurrentFraction = fraction; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1138 | int numValues = mValues.length; |
| 1139 | for (int i = 0; i < numValues; ++i) { |
| 1140 | mValues[i].calculateValue(fraction); |
| 1141 | } |
| 1142 | if (mUpdateListeners != null) { |
| 1143 | int numListeners = mUpdateListeners.size(); |
| 1144 | for (int i = 0; i < numListeners; ++i) { |
| 1145 | mUpdateListeners.get(i).onAnimationUpdate(this); |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | @Override |
| 1151 | public ValueAnimator clone() { |
| 1152 | final ValueAnimator anim = (ValueAnimator) super.clone(); |
| 1153 | if (mUpdateListeners != null) { |
| 1154 | ArrayList<AnimatorUpdateListener> oldListeners = mUpdateListeners; |
| 1155 | anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); |
| 1156 | int numListeners = oldListeners.size(); |
| 1157 | for (int i = 0; i < numListeners; ++i) { |
| 1158 | anim.mUpdateListeners.add(oldListeners.get(i)); |
| 1159 | } |
| 1160 | } |
| 1161 | anim.mSeekTime = -1; |
| 1162 | anim.mPlayingBackwards = false; |
| 1163 | anim.mCurrentIteration = 0; |
| 1164 | anim.mInitialized = false; |
| 1165 | anim.mPlayingState = STOPPED; |
| 1166 | anim.mStartedDelay = false; |
| 1167 | PropertyValuesHolder[] oldValues = mValues; |
| 1168 | if (oldValues != null) { |
| 1169 | int numValues = oldValues.length; |
| 1170 | anim.mValues = new PropertyValuesHolder[numValues]; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1171 | anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); |
| 1172 | for (int i = 0; i < numValues; ++i) { |
Chet Haase | d4dd702 | 2011-04-04 15:25:09 -0700 | [diff] [blame] | 1173 | PropertyValuesHolder newValuesHolder = oldValues[i].clone(); |
| 1174 | anim.mValues[i] = newValuesHolder; |
| 1175 | anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | return anim; |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * Implementors of this interface can add themselves as update listeners |
| 1183 | * to an <code>ValueAnimator</code> instance to receive callbacks on every animation |
| 1184 | * frame, after the current frame's values have been calculated for that |
| 1185 | * <code>ValueAnimator</code>. |
| 1186 | */ |
| 1187 | public static interface AnimatorUpdateListener { |
| 1188 | /** |
| 1189 | * <p>Notifies the occurrence of another frame of the animation.</p> |
| 1190 | * |
| 1191 | * @param animation The animation which was repeated. |
| 1192 | */ |
| 1193 | void onAnimationUpdate(ValueAnimator animation); |
| 1194 | |
| 1195 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1196 | |
| 1197 | /** |
| 1198 | * Return the number of animations currently running. |
| 1199 | * |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1200 | * Used by StrictMode internally to annotate violations. |
| 1201 | * May be called on arbitrary threads! |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1202 | * |
| 1203 | * @hide |
| 1204 | */ |
| 1205 | public static int getCurrentAnimationsCount() { |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1206 | AnimationHandler handler = sAnimationHandler.get(); |
| 1207 | return handler != null ? handler.mAnimations.size() : 0; |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1208 | } |
Patrick Dubroy | 8901ffa | 2011-01-16 17:13:55 -0800 | [diff] [blame] | 1209 | |
| 1210 | /** |
| 1211 | * Clear all animations on this thread, without canceling or ending them. |
| 1212 | * This should be used with caution. |
| 1213 | * |
| 1214 | * @hide |
| 1215 | */ |
| 1216 | public static void clearAllAnimations() { |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1217 | AnimationHandler handler = sAnimationHandler.get(); |
| 1218 | if (handler != null) { |
| 1219 | handler.mAnimations.clear(); |
| 1220 | handler.mPendingAnimations.clear(); |
| 1221 | handler.mDelayedAnims.clear(); |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | private AnimationHandler getOrCreateAnimationHandler() { |
| 1226 | AnimationHandler handler = sAnimationHandler.get(); |
| 1227 | if (handler == null) { |
| 1228 | handler = new AnimationHandler(); |
| 1229 | sAnimationHandler.set(handler); |
| 1230 | } |
| 1231 | return handler; |
Patrick Dubroy | 8901ffa | 2011-01-16 17:13:55 -0800 | [diff] [blame] | 1232 | } |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 1233 | |
| 1234 | @Override |
| 1235 | public String toString() { |
| 1236 | String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode()); |
| 1237 | if (mValues != null) { |
| 1238 | for (int i = 0; i < mValues.length; ++i) { |
| 1239 | returnVal += "\n " + mValues[i].toString(); |
| 1240 | } |
| 1241 | } |
| 1242 | return returnVal; |
| 1243 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1244 | } |