blob: 17d54d2455fe38d855509bfe39d2f3a52f424cef [file] [log] [blame]
Chet Haase17fb4b02010-06-28 17:55:07 -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
Chet Haasee60a6932016-03-17 06:59:16 -070019import android.annotation.Nullable;
Mathew Inwood61beb332018-08-01 14:42:25 +010020import android.annotation.UnsupportedAppUsage;
Alan Viveretteac85f902016-03-11 15:15:51 -050021import android.content.pm.ActivityInfo.Config;
Yigit Boyard422dc32014-09-25 12:23:35 -070022import android.content.res.ConstantState;
23
Chet Haase17fb4b02010-06-28 17:55:07 -070024import java.util.ArrayList;
25
26/**
Chet Haasea18a86b2010-09-07 13:20:00 -070027 * This is the superclass for classes which provide basic support for animations which can be
28 * started, ended, and have <code>AnimatorListeners</code> added to them.
Chet Haase17fb4b02010-06-28 17:55:07 -070029 */
Doris Liu5c71b8c2017-01-31 14:38:21 -080030public abstract class Animator implements Cloneable {
Chet Haase17fb4b02010-06-28 17:55:07 -070031
32 /**
Doris Liu13099142015-07-10 17:32:41 -070033 * The value used to indicate infinite duration (e.g. when Animators repeat infinitely).
Doris Liu13099142015-07-10 17:32:41 -070034 */
35 public static final long DURATION_INFINITE = -1;
36 /**
Chet Haase17fb4b02010-06-28 17:55:07 -070037 * The set of listeners to be sent events through the life of an animation.
38 */
Chet Haasea18a86b2010-09-07 13:20:00 -070039 ArrayList<AnimatorListener> mListeners = null;
Chet Haase17fb4b02010-06-28 17:55:07 -070040
41 /**
Chet Haase8aa1ffb2013-08-08 14:00:00 -070042 * The set of listeners to be sent pause/resume events through the life
43 * of an animation.
44 */
45 ArrayList<AnimatorPauseListener> mPauseListeners = null;
46
47 /**
48 * Whether this animator is currently in a paused state.
49 */
50 boolean mPaused = false;
51
52 /**
Yigit Boyard422dc32014-09-25 12:23:35 -070053 * A set of flags which identify the type of configuration changes that can affect this
54 * Animator. Used by the Animator cache.
55 */
Alan Viveretteac85f902016-03-11 15:15:51 -050056 @Config int mChangingConfigurations = 0;
Yigit Boyard422dc32014-09-25 12:23:35 -070057
58 /**
59 * If this animator is inflated from a constant state, keep a reference to it so that
60 * ConstantState will not be garbage collected until this animator is collected
61 */
62 private AnimatorConstantState mConstantState;
63
64 /**
Chet Haasea18a86b2010-09-07 13:20:00 -070065 * Starts this animation. If the animation has a nonzero startDelay, the animation will start
Chet Haaseb8f574a2011-08-03 14:10:06 -070066 * running after that delay elapses. A non-delayed animation will have its initial
67 * value(s) set immediately, followed by calls to
68 * {@link AnimatorListener#onAnimationStart(Animator)} for any listeners of this animator.
Chet Haasee2ab7cc2010-12-06 16:10:07 -080069 *
70 * <p>The animation started by calling this method will be run on the thread that called
71 * this method. This thread should have a Looper on it (a runtime exception will be thrown if
72 * this is not the case). Also, if the animation will animate
73 * properties of objects in the view hierarchy, then the calling thread should be the UI
74 * thread for that view hierarchy.</p>
75 *
Chet Haase17fb4b02010-06-28 17:55:07 -070076 */
Chet Haasea18a86b2010-09-07 13:20:00 -070077 public void start() {
Chet Haasef54a8d72010-07-22 14:44:59 -070078 }
79
Chet Haased953d082010-08-16 17:44:28 -070080 /**
Chet Haasea18a86b2010-09-07 13:20:00 -070081 * Cancels the animation. Unlike {@link #end()}, <code>cancel()</code> causes the animation to
Chet Haasee2ab7cc2010-12-06 16:10:07 -080082 * stop in its tracks, sending an
83 * {@link android.animation.Animator.AnimatorListener#onAnimationCancel(Animator)} to
84 * its listeners, followed by an
85 * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} message.
86 *
87 * <p>This method must be called on the thread that is running the animation.</p>
Chet Haased953d082010-08-16 17:44:28 -070088 */
Chet Haasea18a86b2010-09-07 13:20:00 -070089 public void cancel() {
Chet Haased953d082010-08-16 17:44:28 -070090 }
91
Chet Haase21cd1382010-09-01 17:42:29 -070092 /**
Chet Haasea18a86b2010-09-07 13:20:00 -070093 * Ends the animation. This causes the animation to assign the end value of the property being
Chet Haasee2ab7cc2010-12-06 16:10:07 -080094 * animated, then calling the
95 * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} method on
Chet Haasea18a86b2010-09-07 13:20:00 -070096 * its listeners.
Chet Haasee2ab7cc2010-12-06 16:10:07 -080097 *
98 * <p>This method must be called on the thread that is running the animation.</p>
Chet Haase21cd1382010-09-01 17:42:29 -070099 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700100 public void end() {
Chet Haase17fb4b02010-06-28 17:55:07 -0700101 }
102
103 /**
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700104 * Pauses a running animation. This method should only be called on the same thread on
105 * which the animation was started. If the animation has not yet been {@link
106 * #isStarted() started} or has since ended, then the call is ignored. Paused
107 * animations can be resumed by calling {@link #resume()}.
108 *
109 * @see #resume()
110 * @see #isPaused()
111 * @see AnimatorPauseListener
112 */
113 public void pause() {
114 if (isStarted() && !mPaused) {
115 mPaused = true;
116 if (mPauseListeners != null) {
117 ArrayList<AnimatorPauseListener> tmpListeners =
118 (ArrayList<AnimatorPauseListener>) mPauseListeners.clone();
119 int numListeners = tmpListeners.size();
120 for (int i = 0; i < numListeners; ++i) {
121 tmpListeners.get(i).onAnimationPause(this);
122 }
123 }
124 }
125 }
126
127 /**
128 * Resumes a paused animation, causing the animator to pick up where it left off
129 * when it was paused. This method should only be called on the same thread on
130 * which the animation was started. Calls to resume() on an animator that is
131 * not currently paused will be ignored.
132 *
133 * @see #pause()
134 * @see #isPaused()
135 * @see AnimatorPauseListener
136 */
137 public void resume() {
138 if (mPaused) {
139 mPaused = false;
140 if (mPauseListeners != null) {
141 ArrayList<AnimatorPauseListener> tmpListeners =
142 (ArrayList<AnimatorPauseListener>) mPauseListeners.clone();
143 int numListeners = tmpListeners.size();
144 for (int i = 0; i < numListeners; ++i) {
145 tmpListeners.get(i).onAnimationResume(this);
146 }
147 }
148 }
149 }
150
151 /**
152 * Returns whether this animator is currently in a paused state.
153 *
154 * @return True if the animator is currently paused, false otherwise.
155 *
156 * @see #pause()
157 * @see #resume()
158 */
159 public boolean isPaused() {
160 return mPaused;
161 }
162
163 /**
Chet Haasee8cee38c2013-04-16 17:54:14 -0700164 * The amount of time, in milliseconds, to delay processing the animation
165 * after {@link #start()} is called.
166 *
167 * @return the number of milliseconds to delay running the animation
168 */
169 public abstract long getStartDelay();
170
171 /**
172 * The amount of time, in milliseconds, to delay processing the animation
173 * after {@link #start()} is called.
174
175 * @param startDelay The amount of the delay, in milliseconds
176 */
177 public abstract void setStartDelay(long startDelay);
178
179 /**
180 * Sets the duration of the animation.
181 *
182 * @param duration The length of the animation, in milliseconds.
183 */
184 public abstract Animator setDuration(long duration);
185
186 /**
187 * Gets the duration of the animation.
188 *
189 * @return The length of the animation, in milliseconds.
190 */
191 public abstract long getDuration();
192
193 /**
Doris Liu13099142015-07-10 17:32:41 -0700194 * Gets the total duration of the animation, accounting for animation sequences, start delay,
195 * and repeating. Return {@link #DURATION_INFINITE} if the duration is infinite.
Doris Liu8b7c99c2015-10-01 17:27:20 -0700196 *
197 * @return Total time an animation takes to finish, starting from the time {@link #start()}
198 * is called. {@link #DURATION_INFINITE} will be returned if the animation or any
199 * child animation repeats infinite times.
Doris Liu13099142015-07-10 17:32:41 -0700200 */
201 public long getTotalDuration() {
Doris Liu8b7c99c2015-10-01 17:27:20 -0700202 long duration = getDuration();
203 if (duration == DURATION_INFINITE) {
204 return DURATION_INFINITE;
205 } else {
206 return getStartDelay() + duration;
207 }
Doris Liu13099142015-07-10 17:32:41 -0700208 }
209
210 /**
Chet Haasee8cee38c2013-04-16 17:54:14 -0700211 * The time interpolator used in calculating the elapsed fraction of the
212 * animation. The interpolator determines whether the animation runs with
213 * linear or non-linear motion, such as acceleration and deceleration. The
214 * default value is {@link android.view.animation.AccelerateDecelerateInterpolator}.
215 *
216 * @param value the interpolator to be used by this animation
217 */
218 public abstract void setInterpolator(TimeInterpolator value);
219
220 /**
221 * Returns the timing interpolator that this animation uses.
222 *
223 * @return The timing interpolator for this animation.
224 */
225 public TimeInterpolator getInterpolator() {
226 return null;
227 }
228
229 /**
Chet Haase8b699792011-08-05 15:20:19 -0700230 * Returns whether this Animator is currently running (having been started and gone past any
231 * initial startDelay period and not yet ended).
232 *
Chet Haasea18a86b2010-09-07 13:20:00 -0700233 * @return Whether the Animator is running.
234 */
235 public abstract boolean isRunning();
236
237 /**
Chet Haase4ddd9252015-06-10 09:25:16 +0100238 * Returns whether this Animator has been started and not yet ended. For reusable
239 * Animators (which most Animators are, apart from the one-shot animator produced by
240 * {@link android.view.ViewAnimationUtils#createCircularReveal(
241 * android.view.View, int, int, float, float) createCircularReveal()}),
242 * this state is a superset of {@link #isRunning()}, because an Animator with a
243 * nonzero {@link #getStartDelay() startDelay} will return true for {@link #isStarted()} during
244 * the delay phase, whereas {@link #isRunning()} will return true only after the delay phase
245 * is complete. Non-reusable animators will always return true after they have been
246 * started, because they cannot return to a non-started state.
Chet Haase8b699792011-08-05 15:20:19 -0700247 *
248 * @return Whether the Animator has been started and not yet ended.
249 */
250 public boolean isStarted() {
251 // Default method returns value for isRunning(). Subclasses should override to return a
252 // real value.
253 return isRunning();
254 }
255
256 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700257 * Adds a listener to the set of listeners that are sent events through the life of an
258 * animation, such as start, repeat, and end.
259 *
260 * @param listener the listener to be added to the current set of listeners for this animation.
261 */
262 public void addListener(AnimatorListener listener) {
263 if (mListeners == null) {
264 mListeners = new ArrayList<AnimatorListener>();
265 }
266 mListeners.add(listener);
267 }
268
269 /**
270 * Removes a listener from the set listening to this animation.
271 *
272 * @param listener the listener to be removed from the current set of listeners for this
273 * animation.
274 */
275 public void removeListener(AnimatorListener listener) {
276 if (mListeners == null) {
277 return;
278 }
279 mListeners.remove(listener);
280 if (mListeners.size() == 0) {
281 mListeners = null;
Chet Haase17fb4b02010-06-28 17:55:07 -0700282 }
283 }
284
285 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700286 * Gets the set of {@link android.animation.Animator.AnimatorListener} objects that are currently
287 * listening for events on this <code>Animator</code> object.
Chet Haase602e4d32010-08-16 08:57:23 -0700288 *
Chet Haasea18a86b2010-09-07 13:20:00 -0700289 * @return ArrayList<AnimatorListener> The set of listeners.
Chet Haase602e4d32010-08-16 08:57:23 -0700290 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700291 public ArrayList<AnimatorListener> getListeners() {
292 return mListeners;
Chet Haase602e4d32010-08-16 08:57:23 -0700293 }
294
295 /**
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700296 * Adds a pause listener to this animator.
297 *
298 * @param listener the listener to be added to the current set of pause listeners
299 * for this animation.
300 */
301 public void addPauseListener(AnimatorPauseListener listener) {
302 if (mPauseListeners == null) {
303 mPauseListeners = new ArrayList<AnimatorPauseListener>();
304 }
305 mPauseListeners.add(listener);
306 }
307
308 /**
309 * Removes a pause listener from the set listening to this animation.
310 *
311 * @param listener the listener to be removed from the current set of pause
312 * listeners for this animation.
313 */
314 public void removePauseListener(AnimatorPauseListener listener) {
315 if (mPauseListeners == null) {
316 return;
317 }
318 mPauseListeners.remove(listener);
319 if (mPauseListeners.size() == 0) {
320 mPauseListeners = null;
321 }
322 }
323
324 /**
Chet Haased82c8ac2013-08-26 14:20:16 -0700325 * Removes all {@link #addListener(android.animation.Animator.AnimatorListener) listeners}
326 * and {@link #addPauseListener(android.animation.Animator.AnimatorPauseListener)
327 * pauseListeners} from this object.
Chet Haase17fb4b02010-06-28 17:55:07 -0700328 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700329 public void removeAllListeners() {
Chet Haase17fb4b02010-06-28 17:55:07 -0700330 if (mListeners != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700331 mListeners.clear();
332 mListeners = null;
Chet Haase17fb4b02010-06-28 17:55:07 -0700333 }
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700334 if (mPauseListeners != null) {
335 mPauseListeners.clear();
336 mPauseListeners = null;
337 }
Chet Haase17fb4b02010-06-28 17:55:07 -0700338 }
339
Yigit Boyard422dc32014-09-25 12:23:35 -0700340 /**
341 * Return a mask of the configuration parameters for which this animator may change, requiring
342 * that it should be re-created from Resources. The default implementation returns whatever
343 * value was provided through setChangingConfigurations(int) or 0 by default.
344 *
345 * @return Returns a mask of the changing configuration parameters, as defined by
346 * {@link android.content.pm.ActivityInfo}.
347 * @see android.content.pm.ActivityInfo
348 * @hide
349 */
Alan Viveretteac85f902016-03-11 15:15:51 -0500350 public @Config int getChangingConfigurations() {
Yigit Boyard422dc32014-09-25 12:23:35 -0700351 return mChangingConfigurations;
352 }
353
354 /**
355 * Set a mask of the configuration parameters for which this animator may change, requiring
356 * that it be re-created from resource.
357 *
358 * @param configs A mask of the changing configuration parameters, as
359 * defined by {@link android.content.pm.ActivityInfo}.
360 *
361 * @see android.content.pm.ActivityInfo
362 * @hide
363 */
Alan Viveretteac85f902016-03-11 15:15:51 -0500364 public void setChangingConfigurations(@Config int configs) {
Yigit Boyard422dc32014-09-25 12:23:35 -0700365 mChangingConfigurations = configs;
366 }
367
368 /**
369 * Sets the changing configurations value to the union of the current changing configurations
370 * and the provided configs.
371 * This method is called while loading the animator.
372 * @hide
373 */
Alan Viveretteac85f902016-03-11 15:15:51 -0500374 public void appendChangingConfigurations(@Config int configs) {
Yigit Boyard422dc32014-09-25 12:23:35 -0700375 mChangingConfigurations |= configs;
376 }
377
378 /**
379 * Return a {@link android.content.res.ConstantState} instance that holds the shared state of
380 * this Animator.
381 * <p>
382 * This constant state is used to create new instances of this animator when needed, instead
383 * of re-loading it from resources. Default implementation creates a new
384 * {@link AnimatorConstantState}. You can override this method to provide your custom logic or
385 * return null if you don't want this animator to be cached.
386 *
387 * @return The ConfigurationBoundResourceCache.BaseConstantState associated to this Animator.
388 * @see android.content.res.ConstantState
389 * @see #clone()
390 * @hide
391 */
392 public ConstantState<Animator> createConstantState() {
393 return new AnimatorConstantState(this);
394 }
395
Chet Haase49afa5b2010-08-23 11:39:53 -0700396 @Override
Chet Haase21cd1382010-09-01 17:42:29 -0700397 public Animator clone() {
Chet Haasea18a86b2010-09-07 13:20:00 -0700398 try {
399 final Animator anim = (Animator) super.clone();
400 if (mListeners != null) {
Yigit Boyard422dc32014-09-25 12:23:35 -0700401 anim.mListeners = new ArrayList<AnimatorListener>(mListeners);
Chet Haase49afa5b2010-08-23 11:39:53 -0700402 }
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700403 if (mPauseListeners != null) {
Yigit Boyard422dc32014-09-25 12:23:35 -0700404 anim.mPauseListeners = new ArrayList<AnimatorPauseListener>(mPauseListeners);
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700405 }
Chet Haasea18a86b2010-09-07 13:20:00 -0700406 return anim;
407 } catch (CloneNotSupportedException e) {
408 throw new AssertionError();
Chet Haase49afa5b2010-08-23 11:39:53 -0700409 }
Chet Haase49afa5b2010-08-23 11:39:53 -0700410 }
411
Chet Haase17fb4b02010-06-28 17:55:07 -0700412 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700413 * This method tells the object to use appropriate information to extract
414 * starting values for the animation. For example, a AnimatorSet object will pass
415 * this call to its child objects to tell them to set up the values. A
416 * ObjectAnimator object will use the information it has about its target object
417 * and PropertyValuesHolder objects to get the start values for its properties.
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900418 * A ValueAnimator object will ignore the request since it does not have enough
Chet Haasea18a86b2010-09-07 13:20:00 -0700419 * information (such as a target object) to gather these values.
Chet Haase17fb4b02010-06-28 17:55:07 -0700420 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700421 public void setupStartValues() {
422 }
423
424 /**
425 * This method tells the object to use appropriate information to extract
426 * ending values for the animation. For example, a AnimatorSet object will pass
427 * this call to its child objects to tell them to set up the values. A
428 * ObjectAnimator object will use the information it has about its target object
429 * and PropertyValuesHolder objects to get the start values for its properties.
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900430 * A ValueAnimator object will ignore the request since it does not have enough
Chet Haasea18a86b2010-09-07 13:20:00 -0700431 * information (such as a target object) to gather these values.
432 */
433 public void setupEndValues() {
434 }
435
436 /**
437 * Sets the target object whose property will be animated by this animation. Not all subclasses
438 * operate on target objects (for example, {@link ValueAnimator}, but this method
439 * is on the superclass for the convenience of dealing generically with those subclasses
440 * that do handle targets.
Chet Haasee60a6932016-03-17 06:59:16 -0700441 * <p>
442 * <strong>Note:</strong> The target is stored as a weak reference internally to avoid leaking
443 * resources by having animators directly reference old targets. Therefore, you should
444 * ensure that animator targets always have a hard reference elsewhere.
Chet Haasea18a86b2010-09-07 13:20:00 -0700445 *
446 * @param target The object being animated
447 */
Chet Haasee60a6932016-03-17 06:59:16 -0700448 public void setTarget(@Nullable Object target) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700449 }
450
ztenghui7bc6a3f2014-07-15 15:12:12 -0700451 // Hide reverse() and canReverse() for now since reverse() only work for simple
452 // cases, like we don't support sequential, neither startDelay.
453 // TODO: make reverse() works for all the Animators.
454 /**
455 * @hide
456 */
457 public boolean canReverse() {
458 return false;
459 }
460
461 /**
462 * @hide
463 */
Mathew Inwood61beb332018-08-01 14:42:25 +0100464 @UnsupportedAppUsage
ztenghui7bc6a3f2014-07-15 15:12:12 -0700465 public void reverse() {
John Reck291161a2014-07-22 07:31:09 -0700466 throw new IllegalStateException("Reverse is not supported");
ztenghui7bc6a3f2014-07-15 15:12:12 -0700467 }
468
Doris Liu5c71b8c2017-01-31 14:38:21 -0800469 // Pulse an animation frame into the animation.
470 boolean pulseAnimationFrame(long frameTime) {
Doris Liu6ba5ed32017-01-26 18:50:04 -0800471 // TODO: Need to find a better signal than this. There's a bug in SystemUI that's preventing
472 // returning !isStarted() from working.
473 return false;
Doris Liu13351992017-01-17 17:10:42 -0800474 }
475
476 /**
Doris Liu13351992017-01-17 17:10:42 -0800477 * Internal use only.
478 * This call starts the animation in regular or reverse direction without requiring them to
479 * register frame callbacks. The caller will be responsible for all the subsequent animation
480 * pulses. Specifically, the caller needs to call doAnimationFrame(...) for the animation on
481 * every frame.
482 *
483 * @param inReverse whether the animation should play in reverse direction
484 */
485 void startWithoutPulsing(boolean inReverse) {
486 if (inReverse) {
487 reverse();
488 } else {
489 start();
490 }
491 }
492
493 /**
494 * Internal use only.
495 * Skips the animation value to end/start, depending on whether the play direction is forward
496 * or backward.
497 *
498 * @param inReverse whether the end value is based on a reverse direction. If yes, this is
499 * equivalent to skip to start value in a forward playing direction.
500 */
501 void skipToEndValue(boolean inReverse) {}
502
503
504 /**
505 * Internal use only.
506 *
507 * Returns whether the animation has start/end values setup. For most of the animations, this
508 * should always be true. For ObjectAnimators, the start values are setup in the initialization
509 * of the animation.
510 */
511 boolean isInitialized() {
512 return true;
513 }
514
515 /**
516 * Internal use only.
517 */
518 void animateBasedOnPlayTime(long currentPlayTime, long lastPlayTime, boolean inReverse) {}
519
520 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700521 * <p>An animation listener receives notifications from an animation.
522 * Notifications indicate animation related events, such as the end or the
523 * repetition of the animation.</p>
524 */
525 public static interface AnimatorListener {
Doris Liu13351992017-01-17 17:10:42 -0800526
527 /**
528 * <p>Notifies the start of the animation as well as the animation's overall play direction.
529 * This method's default behavior is to call {@link #onAnimationStart(Animator)}. This
530 * method can be overridden, though not required, to get the additional play direction info
531 * when an animation starts. Skipping calling super when overriding this method results in
532 * {@link #onAnimationStart(Animator)} not getting called.
533 *
534 * @param animation The started animation.
535 * @param isReverse Whether the animation is playing in reverse.
536 */
537 default void onAnimationStart(Animator animation, boolean isReverse) {
538 onAnimationStart(animation);
539 }
540
541 /**
542 * <p>Notifies the end of the animation. This callback is not invoked
543 * for animations with repeat count set to INFINITE.</p>
544 *
545 * <p>This method's default behavior is to call {@link #onAnimationEnd(Animator)}. This
546 * method can be overridden, though not required, to get the additional play direction info
547 * when an animation ends. Skipping calling super when overriding this method results in
548 * {@link #onAnimationEnd(Animator)} not getting called.
549 *
550 * @param animation The animation which reached its end.
551 * @param isReverse Whether the animation is playing in reverse.
552 */
553 default void onAnimationEnd(Animator animation, boolean isReverse) {
554 onAnimationEnd(animation);
555 }
556
Chet Haase17fb4b02010-06-28 17:55:07 -0700557 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700558 * <p>Notifies the start of the animation.</p>
559 *
560 * @param animation The started animation.
561 */
562 void onAnimationStart(Animator animation);
563
564 /**
565 * <p>Notifies the end of the animation. This callback is not invoked
566 * for animations with repeat count set to INFINITE.</p>
567 *
568 * @param animation The animation which reached its end.
569 */
570 void onAnimationEnd(Animator animation);
571
572 /**
573 * <p>Notifies the cancellation of the animation. This callback is not invoked
574 * for animations with repeat count set to INFINITE.</p>
575 *
576 * @param animation The animation which was canceled.
577 */
578 void onAnimationCancel(Animator animation);
579
580 /**
581 * <p>Notifies the repetition of the animation.</p>
Chet Haase17fb4b02010-06-28 17:55:07 -0700582 *
583 * @param animation The animation which was repeated.
584 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700585 void onAnimationRepeat(Animator animation);
Chet Haase17fb4b02010-06-28 17:55:07 -0700586 }
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700587
588 /**
589 * A pause listener receives notifications from an animation when the
590 * animation is {@link #pause() paused} or {@link #resume() resumed}.
591 *
592 * @see #addPauseListener(AnimatorPauseListener)
593 */
594 public static interface AnimatorPauseListener {
595 /**
596 * <p>Notifies that the animation was paused.</p>
597 *
598 * @param animation The animaton being paused.
599 * @see #pause()
600 */
601 void onAnimationPause(Animator animation);
602
603 /**
604 * <p>Notifies that the animation was resumed, after being
605 * previously paused.</p>
606 *
607 * @param animation The animation being resumed.
608 * @see #resume()
609 */
610 void onAnimationResume(Animator animation);
611 }
John Reckc01bd112014-07-18 16:22:09 -0700612
613 /**
614 * <p>Whether or not the Animator is allowed to run asynchronously off of
615 * the UI thread. This is a hint that informs the Animator that it is
616 * OK to run the animation off-thread, however the Animator may decide
617 * that it must run the animation on the UI thread anyway.
618 *
619 * <p>Regardless of whether or not the animation runs asynchronously, all
620 * listener callbacks will be called on the UI thread.</p>
621 *
622 * <p>To be able to use this hint the following must be true:</p>
623 * <ol>
624 * <li>The animator is immutable while {@link #isStarted()} is true. Requests
625 * to change duration, delay, etc... may be ignored.</li>
626 * <li>Lifecycle callback events may be asynchronous. Events such as
627 * {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or
628 * {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed
629 * as they must be posted back to the UI thread, and any actions performed
630 * by those callbacks (such as starting new animations) will not happen
631 * in the same frame.</li>
632 * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...)
633 * may be asynchronous. It is guaranteed that all state changes that are
634 * performed on the UI thread in the same frame will be applied as a single
635 * atomic update, however that frame may be the current frame,
636 * the next frame, or some future frame. This will also impact the observed
637 * state of the Animator. For example, {@link #isStarted()} may still return true
638 * after a call to {@link #end()}. Using the lifecycle callbacks is preferred over
639 * queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()}
640 * for this reason.</li>
641 * </ol>
642 * @hide
643 */
644 public void setAllowRunningAsynchronously(boolean mayRunAsync) {
645 // It is up to subclasses to support this, if they can.
646 }
Yigit Boyard422dc32014-09-25 12:23:35 -0700647
648 /**
649 * Creates a {@link ConstantState} which holds changing configurations information associated
650 * with the given Animator.
651 * <p>
652 * When {@link #newInstance()} is called, default implementation clones the Animator.
653 */
654 private static class AnimatorConstantState extends ConstantState<Animator> {
655
656 final Animator mAnimator;
Alan Viveretteac85f902016-03-11 15:15:51 -0500657 @Config int mChangingConf;
Yigit Boyard422dc32014-09-25 12:23:35 -0700658
659 public AnimatorConstantState(Animator animator) {
660 mAnimator = animator;
661 // ensure a reference back to here so that constante state is not gc'ed.
662 mAnimator.mConstantState = this;
663 mChangingConf = mAnimator.getChangingConfigurations();
664 }
665
666 @Override
Alan Viveretteac85f902016-03-11 15:15:51 -0500667 public @Config int getChangingConfigurations() {
Yigit Boyard422dc32014-09-25 12:23:35 -0700668 return mChangingConf;
669 }
670
671 @Override
672 public Animator newInstance() {
673 final Animator clone = mAnimator.clone();
674 clone.mConstantState = this;
675 return clone;
676 }
677 }
Chet Haasea18a86b2010-09-07 13:20:00 -0700678}