blob: 9bf1634c5a55cc34059b42112050f187761eef7b [file] [log] [blame]
Chet Haasea18a86b2010-09-07 13:20:00 -07001/*
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.os.Handler;
20import android.os.Looper;
21import android.os.Message;
Chet Haase2970c492010-11-09 13:58:04 -080022import android.util.AndroidRuntimeException;
Jeff Brown96e942d2011-11-30 19:55:01 -080023import android.view.Choreographer;
Chet Haasea18a86b2010-09-07 13:20:00 -070024import android.view.animation.AccelerateDecelerateInterpolator;
25import android.view.animation.AnimationUtils;
Chet Haase27c1d4d2010-12-16 07:58:28 -080026import android.view.animation.LinearInterpolator;
Chet Haasea18a86b2010-09-07 13:20:00 -070027
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
Chet Haasee0ee2e92010-10-07 09:06:18 -070041 * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p>
Chet Haasea18a86b2010-09-07 13:20:00 -070042 */
Chet Haase2794eb32010-10-12 16:29:28 -070043public class ValueAnimator extends Animator {
Chet Haasea18a86b2010-09-07 13:20:00 -070044
45 /**
46 * Internal constants
47 */
48
Chet Haasea18a86b2010-09-07 13:20:00 -070049 /**
Jeff Brown96e942d2011-11-30 19:55:01 -080050 * Messages sent to timing handler: START is sent when an animation first begins.
Chet Haasea18a86b2010-09-07 13:20:00 -070051 */
Xavier Ducrohet7f9f99ea2011-08-11 10:16:17 -070052 static final int ANIMATION_START = 0;
Chet Haasea18a86b2010-09-07 13:20:00 -070053
54 /**
55 * Values used with internal variable mPlayingState to indicate the current state of an
56 * animation.
57 */
Chet Haase051d35e2010-12-14 07:20:58 -080058 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 Haasea18a86b2010-09-07 13:20:00 -070061
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 Haase051d35e2010-12-14 07:20:58 -080072 long mStartTime;
Chet Haasea18a86b2010-09-07 13:20:00 -070073
74 /**
75 * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked
76 * to a value.
77 */
Chet Haase051d35e2010-12-14 07:20:58 -080078 long mSeekTime = -1;
Chet Haasea18a86b2010-09-07 13:20:00 -070079
80 // The static sAnimationHandler processes the internal timing loop on which all animations
81 // are based
Chet Haasee3bc4e62010-10-27 13:18:54 -070082 private static ThreadLocal<AnimationHandler> sAnimationHandler =
83 new ThreadLocal<AnimationHandler>();
Chet Haasea18a86b2010-09-07 13:20:00 -070084
Chet Haasea18a86b2010-09-07 13:20:00 -070085 // The time interpolator to be used if none is set on the animation
Chet Haasee0ee2e92010-10-07 09:06:18 -070086 private static final TimeInterpolator sDefaultInterpolator =
87 new AccelerateDecelerateInterpolator();
Chet Haasea18a86b2010-09-07 13:20:00 -070088
Chet Haasea18a86b2010-09-07 13:20:00 -070089 /**
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 Haasea00f3862011-02-22 06:34:40 -0800102 * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction().
103 */
104 private float mCurrentFraction = 0f;
105
106 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700107 * 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 Haasee2ab7cc2010-12-06 16:10:07 -0800123 * STOPPED, RUNNING, SEEKED.
Chet Haasea18a86b2010-09-07 13:20:00 -0700124 */
Chet Haase051d35e2010-12-14 07:20:58 -0800125 int mPlayingState = STOPPED;
Chet Haasea18a86b2010-09-07 13:20:00 -0700126
127 /**
Chet Haaseb8f574a2011-08-03 14:10:06 -0700128 * 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 Haase8b699792011-08-05 15:20:19 -0700135 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 Haaseb8f574a2011-08-03 14:10:06 -0700141 private boolean mStarted = false;
142
143 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700144 * 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 Haase2794eb32010-10-12 16:29:28 -0700154 private long mDuration = 300;
Chet Haasea18a86b2010-09-07 13:20:00 -0700155
156 // The amount of time in ms to delay starting the animation after start() is called
157 private long mStartDelay = 0;
158
Chet Haasea18a86b2010-09-07 13:20:00 -0700159 // 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 Haasee0ee2e92010-10-07 09:06:18 -0700175 private TimeInterpolator mInterpolator = sDefaultInterpolator;
Chet Haasea18a86b2010-09-07 13:20:00 -0700176
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 Haase2794eb32010-10-12 16:29:28 -0700215 * use internally; the factory methods which take parameters are more generally
Chet Haasea18a86b2010-09-07 13:20:00 -0700216 * useful.
217 */
218 public ValueAnimator() {
219 }
220
221 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700222 * 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 Haasea18a86b2010-09-07 13:20:00 -0700228 *
Chet Haase2794eb32010-10-12 16:29:28 -0700229 * @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 Haasea18a86b2010-09-07 13:20:00 -0700231 */
Chet Haase2794eb32010-10-12 16:29:28 -0700232 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 Haase2794eb32010-10-12 16:29:28 -0700256 * 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 Haasea18a86b2010-09-07 13:20:00 -0700310 }
Chet Haase2794eb32010-10-12 16:29:28 -0700311 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 Haase2794eb32010-10-12 16:29:28 -0700350 * 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 Haasea18a86b2010-09-07 13:20:00 -0700380 }
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 Haase0e0590b2010-09-26 11:57:28 -0700398 // New property/values/target should cause re-initialization prior to starting
399 mInitialized = false;
Chet Haasea18a86b2010-09-07 13:20:00 -0700400 }
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 Haasea18a86b2010-09-07 13:20:00 -0700415 * 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 Haasea18a86b2010-09-07 13:20:00 -0700430 mInitialized = true;
431 }
432 }
433
434
435 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700436 * Sets the length of the animation. The default duration is 300 milliseconds.
Chet Haasea18a86b2010-09-07 13:20:00 -0700437 *
Chet Haase27c1d4d2010-12-16 07:58:28 -0800438 * @param duration The length of the animation, in milliseconds. This value cannot
439 * be negative.
Chet Haase2794eb32010-10-12 16:29:28 -0700440 * @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 Haasea18a86b2010-09-07 13:20:00 -0700443 */
Chet Haase2794eb32010-10-12 16:29:28 -0700444 public ValueAnimator setDuration(long duration) {
Chet Haase27c1d4d2010-12-16 07:58:28 -0800445 if (duration < 0) {
446 throw new IllegalArgumentException("Animators cannot have negative duration: " +
447 duration);
448 }
Chet Haasea18a86b2010-09-07 13:20:00 -0700449 mDuration = duration;
Chet Haase2794eb32010-10-12 16:29:28 -0700450 return this;
Chet Haasea18a86b2010-09-07 13:20:00 -0700451 }
452
453 /**
Chet Haase2794eb32010-10-12 16:29:28 -0700454 * Gets the length of the animation. The default duration is 300 milliseconds.
Chet Haasea18a86b2010-09-07 13:20:00 -0700455 *
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 Brown96e942d2011-11-30 19:55:01 -0800505 private static class AnimationHandler extends Handler
506 implements Choreographer.OnAnimateListener {
Jeff Brown9c38dbe2011-12-02 16:22:46 -0800507 // 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 Brown96e942d2011-11-30 19:55:01 -0800521 private final Choreographer mChoreographer;
522 private boolean mIsChoreographed;
523
524 private AnimationHandler() {
525 mChoreographer = Choreographer.getInstance();
526 }
527
Chet Haasea18a86b2010-09-07 13:20:00 -0700528 /**
Jeff Brown96e942d2011-11-30 19:55:01 -0800529 * The START message is sent when an animation's start() method is called.
530 * It cannot start synchronously when start() is called
Chet Haasea18a86b2010-09-07 13:20:00 -0700531 * 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 Haasea18a86b2010-09-07 13:20:00 -0700536 */
537 @Override
538 public void handleMessage(Message msg) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700539 switch (msg.what) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700540 case ANIMATION_START:
Jeff Brown96e942d2011-11-30 19:55:01 -0800541 doAnimationStart();
Chet Haasea18a86b2010-09-07 13:20:00 -0700542 break;
543 }
544 }
Jeff Brown96e942d2011-11-30 19:55:01 -0800545
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 Haasea18a86b2010-09-07 13:20:00 -0700645 }
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 Brown96e942d2011-11-30 19:55:01 -0800674 * 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 Haasea18a86b2010-09-07 13:20:00 -0700677 * @return the requested time between frames, in milliseconds
678 */
679 public static long getFrameDelay() {
Jeff Brown96e942d2011-11-30 19:55:01 -0800680 return Choreographer.getFrameDelay();
Chet Haasea18a86b2010-09-07 13:20:00 -0700681 }
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 Brown96e942d2011-11-30 19:55:01 -0800690 * 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 Haasea18a86b2010-09-07 13:20:00 -0700693 * @param frameDelay the requested time between frames, in milliseconds
694 */
695 public static void setFrameDelay(long frameDelay) {
Jeff Brown96e942d2011-11-30 19:55:01 -0800696 Choreographer.setFrameDelay(frameDelay);
Chet Haasea18a86b2010-09-07 13:20:00 -0700697 }
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 Miller30604212010-09-22 19:56:23 -0700795 * 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 Haasea18a86b2010-09-07 13:20:00 -0700806 * 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 Haase27c1d4d2010-12-16 07:58:28 -0800828 * @param value the interpolator to be used by this animation. A value of <code>null</code>
829 * will result in linear interpolation.
Chet Haasea18a86b2010-09-07 13:20:00 -0700830 */
831 @Override
Chet Haasee0ee2e92010-10-07 09:06:18 -0700832 public void setInterpolator(TimeInterpolator value) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700833 if (value != null) {
834 mInterpolator = value;
Chet Haase27c1d4d2010-12-16 07:58:28 -0800835 } else {
836 mInterpolator = new LinearInterpolator();
Chet Haasea18a86b2010-09-07 13:20:00 -0700837 }
838 }
839
840 /**
841 * Returns the timing interpolator that this ValueAnimator uses.
842 *
843 * @return The timing interpolator for this ValueAnimator.
844 */
Chet Haasee0ee2e92010-10-07 09:06:18 -0700845 public TimeInterpolator getInterpolator() {
Chet Haasea18a86b2010-09-07 13:20:00 -0700846 return mInterpolator;
847 }
848
849 /**
850 * The type evaluator to be used when calculating the animated values of this animation.
Chet Haaseb2ab04f2011-01-16 11:03:22 -0800851 * The system will automatically assign a float or int evaluator based on the type
Chet Haasea18a86b2010-09-07 13:20:00 -0700852 * 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 Haase53ee3312011-01-10 15:56:56 -0800855 * For example, when running an animation on color values, the {@link ArgbEvaluator}
Chet Haasea18a86b2010-09-07 13:20:00 -0700856 * 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 Haase2970c492010-11-09 13:58:04 -0800874 * 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 Haasea18a86b2010-09-07 13:20:00 -0700881 *
882 * @param playBackwards Whether the ValueAnimator should start playing in reverse.
883 */
884 private void start(boolean playBackwards) {
Chet Haase2970c492010-11-09 13:58:04 -0800885 if (Looper.myLooper() == null) {
886 throw new AndroidRuntimeException("Animators may only be run on Looper threads");
Jim Miller30604212010-09-22 19:56:23 -0700887 }
Chet Haase2970c492010-11-09 13:58:04 -0800888 mPlayingBackwards = playBackwards;
Chet Haaseadd65772011-02-09 16:47:29 -0800889 mCurrentIteration = 0;
890 mPlayingState = STOPPED;
Chet Haase8b699792011-08-05 15:20:19 -0700891 mStarted = true;
Chet Haaseadd65772011-02-09 16:47:29 -0800892 mStartedDelay = false;
Jeff Brown9c38dbe2011-12-02 16:22:46 -0800893 AnimationHandler animationHandler = getOrCreateAnimationHandler();
894 animationHandler.mPendingAnimations.add(this);
Chet Haase2970c492010-11-09 13:58:04 -0800895 if (mStartDelay == 0) {
Chet Haaseadd65772011-02-09 16:47:29 -0800896 // This sets the initial value of the animation, prior to actually starting it running
897 setCurrentPlayTime(getCurrentPlayTime());
Chet Haase154f1452011-02-23 16:53:18 -0800898 mPlayingState = STOPPED;
Chet Haase8b699792011-08-05 15:20:19 -0700899 mRunning = true;
Chet Haaseadd65772011-02-09 16:47:29 -0800900
Chet Haaseb20db3e2010-09-10 13:07:30 -0700901 if (mListeners != null) {
902 ArrayList<AnimatorListener> tmpListeners =
903 (ArrayList<AnimatorListener>) mListeners.clone();
Chet Haase7c608f22010-10-22 17:54:04 -0700904 int numListeners = tmpListeners.size();
905 for (int i = 0; i < numListeners; ++i) {
906 tmpListeners.get(i).onAnimationStart(this);
Chet Haaseb20db3e2010-09-10 13:07:30 -0700907 }
908 }
Chet Haasea18a86b2010-09-07 13:20:00 -0700909 }
Chet Haasee3bc4e62010-10-27 13:18:54 -0700910 animationHandler.sendEmptyMessage(ANIMATION_START);
Chet Haasea18a86b2010-09-07 13:20:00 -0700911 }
912
913 @Override
914 public void start() {
915 start(false);
916 }
917
918 @Override
919 public void cancel() {
Chet Haase2970c492010-11-09 13:58:04 -0800920 // Only cancel if the animation is actually running or has been started and is about
921 // to run
Jeff Brown9c38dbe2011-12-02 16:22:46 -0800922 AnimationHandler handler = getOrCreateAnimationHandler();
923 if (mPlayingState != STOPPED
924 || handler.mPendingAnimations.contains(this)
925 || handler.mDelayedAnims.contains(this)) {
Chet Haaseb8f574a2011-08-03 14:10:06 -0700926 // Only notify listeners if the animator has actually started
Chet Haase8b699792011-08-05 15:20:19 -0700927 if (mRunning && mListeners != null) {
Chet Haase7dfacdb2011-07-11 17:01:56 -0700928 ArrayList<AnimatorListener> tmpListeners =
929 (ArrayList<AnimatorListener>) mListeners.clone();
930 for (AnimatorListener listener : tmpListeners) {
931 listener.onAnimationCancel(this);
932 }
933 }
Jeff Brown9c38dbe2011-12-02 16:22:46 -0800934 endAnimation(handler);
Chet Haase2970c492010-11-09 13:58:04 -0800935 }
Chet Haasea18a86b2010-09-07 13:20:00 -0700936 }
937
938 @Override
939 public void end() {
Jeff Brown9c38dbe2011-12-02 16:22:46 -0800940 AnimationHandler handler = getOrCreateAnimationHandler();
941 if (!handler.mAnimations.contains(this) && !handler.mPendingAnimations.contains(this)) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700942 // Special case if the animation has not yet started; get it ready for ending
943 mStartedDelay = false;
Jeff Brown9c38dbe2011-12-02 16:22:46 -0800944 startAnimation(handler);
Chet Haaseadd65772011-02-09 16:47:29 -0800945 } else if (!mInitialized) {
946 initAnimation();
Chet Haasea18a86b2010-09-07 13:20:00 -0700947 }
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800948 // 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 Brown9c38dbe2011-12-02 16:22:46 -0800955 endAnimation(handler);
Chet Haasea18a86b2010-09-07 13:20:00 -0700956 }
957
958 @Override
959 public boolean isRunning() {
Chet Haase8b699792011-08-05 15:20:19 -0700960 return (mPlayingState == RUNNING || mRunning);
961 }
962
963 @Override
964 public boolean isStarted() {
965 return mStarted;
Chet Haasea18a86b2010-09-07 13:20:00 -0700966 }
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 Brown9c38dbe2011-12-02 16:22:46 -0800991 private void endAnimation(AnimationHandler handler) {
992 handler.mAnimations.remove(this);
993 handler.mPendingAnimations.remove(this);
994 handler.mDelayedAnims.remove(this);
Chet Haasea18a86b2010-09-07 13:20:00 -0700995 mPlayingState = STOPPED;
Chet Haase8b699792011-08-05 15:20:19 -0700996 if (mRunning && mListeners != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700997 ArrayList<AnimatorListener> tmpListeners =
998 (ArrayList<AnimatorListener>) mListeners.clone();
Chet Haase7c608f22010-10-22 17:54:04 -0700999 int numListeners = tmpListeners.size();
1000 for (int i = 0; i < numListeners; ++i) {
1001 tmpListeners.get(i).onAnimationEnd(this);
Chet Haasea18a86b2010-09-07 13:20:00 -07001002 }
1003 }
Chet Haase8b699792011-08-05 15:20:19 -07001004 mRunning = false;
Chet Haaseb8f574a2011-08-03 14:10:06 -07001005 mStarted = false;
Chet Haasea18a86b2010-09-07 13:20:00 -07001006 }
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 Brown9c38dbe2011-12-02 16:22:46 -08001012 private void startAnimation(AnimationHandler handler) {
Chet Haasea18a86b2010-09-07 13:20:00 -07001013 initAnimation();
Jeff Brown9c38dbe2011-12-02 16:22:46 -08001014 handler.mAnimations.add(this);
Chet Haaseb20db3e2010-09-10 13:07:30 -07001015 if (mStartDelay > 0 && mListeners != null) {
1016 // Listeners were already notified in start() if startDelay is 0; this is
1017 // just for delayed animations
Chet Haasea18a86b2010-09-07 13:20:00 -07001018 ArrayList<AnimatorListener> tmpListeners =
1019 (ArrayList<AnimatorListener>) mListeners.clone();
Chet Haase7c608f22010-10-22 17:54:04 -07001020 int numListeners = tmpListeners.size();
1021 for (int i = 0; i < numListeners; ++i) {
1022 tmpListeners.get(i).onAnimationStart(this);
Chet Haasea18a86b2010-09-07 13:20:00 -07001023 }
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 Haasea18a86b2010-09-07 13:20:00 -07001038 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 Haase051d35e2010-12-14 07:20:58 -08001066 boolean animationFrame(long currentTime) {
Chet Haasea18a86b2010-09-07 13:20:00 -07001067 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 Haase70d4ba12010-10-06 09:46:45 -07001082 float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f;
Chet Haasea18a86b2010-09-07 13:20:00 -07001083 if (fraction >= 1f) {
1084 if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) {
1085 // Time to repeat
1086 if (mListeners != null) {
Chet Haase7c608f22010-10-22 17:54:04 -07001087 int numListeners = mListeners.size();
1088 for (int i = 0; i < numListeners; ++i) {
1089 mListeners.get(i).onAnimationRepeat(this);
Chet Haasea18a86b2010-09-07 13:20:00 -07001090 }
1091 }
Chet Haasea18a86b2010-09-07 13:20:00 -07001092 if (mRepeatMode == REVERSE) {
1093 mPlayingBackwards = mPlayingBackwards ? false : true;
1094 }
Chet Haase73066682010-11-29 15:55:32 -08001095 mCurrentIteration += (int)fraction;
1096 fraction = fraction % 1f;
Chet Haasea18a86b2010-09-07 13:20:00 -07001097 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 Haasea18a86b2010-09-07 13:20:00 -07001108 }
1109
1110 return done;
1111 }
1112
1113 /**
Chet Haasea00f3862011-02-22 06:34:40 -08001114 * 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 Haasea18a86b2010-09-07 13:20:00 -07001124 * 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 Haasea00f3862011-02-22 06:34:40 -08001137 mCurrentFraction = fraction;
Chet Haasea18a86b2010-09-07 13:20:00 -07001138 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 Haasea18a86b2010-09-07 13:20:00 -07001171 anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
1172 for (int i = 0; i < numValues; ++i) {
Chet Haased4dd7022011-04-04 15:25:09 -07001173 PropertyValuesHolder newValuesHolder = oldValues[i].clone();
1174 anim.mValues[i] = newValuesHolder;
1175 anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder);
Chet Haasea18a86b2010-09-07 13:20:00 -07001176 }
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 Fitzpatrick599ca292010-10-22 14:47:03 -07001196
1197 /**
1198 * Return the number of animations currently running.
1199 *
Jeff Brown9c38dbe2011-12-02 16:22:46 -08001200 * Used by StrictMode internally to annotate violations.
1201 * May be called on arbitrary threads!
Brad Fitzpatrick599ca292010-10-22 14:47:03 -07001202 *
1203 * @hide
1204 */
1205 public static int getCurrentAnimationsCount() {
Jeff Brown9c38dbe2011-12-02 16:22:46 -08001206 AnimationHandler handler = sAnimationHandler.get();
1207 return handler != null ? handler.mAnimations.size() : 0;
Brad Fitzpatrick599ca292010-10-22 14:47:03 -07001208 }
Patrick Dubroy8901ffa2011-01-16 17:13:55 -08001209
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 Brown9c38dbe2011-12-02 16:22:46 -08001217 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 Dubroy8901ffa2011-01-16 17:13:55 -08001232 }
Chet Haasee9140a72011-02-16 16:23:29 -08001233
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 Fitzpatrick599ca292010-10-22 14:47:03 -07001244}