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