blob: 02a329d3ec0601e1f648e70e03730e43a59e61d6 [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
Doris Liu0084e372015-04-10 12:39:35 -070019import android.content.res.Configuration;
Yigit Boyard422dc32014-09-25 12:23:35 -070020import android.content.res.ConstantState;
Doris Liu0084e372015-04-10 12:39:35 -070021import android.content.res.Resources;
22import android.util.DisplayMetrics;
23import android.util.Log;
24import android.view.animation.AnimationUtils;
Yigit Boyard422dc32014-09-25 12:23:35 -070025
Chet Haase17fb4b02010-06-28 17:55:07 -070026import java.util.ArrayList;
27
28/**
Chet Haasea18a86b2010-09-07 13:20:00 -070029 * This is the superclass for classes which provide basic support for animations which can be
30 * started, ended, and have <code>AnimatorListeners</code> added to them.
Chet Haase17fb4b02010-06-28 17:55:07 -070031 */
Chet Haasee8cee38c2013-04-16 17:54:14 -070032public abstract class Animator implements Cloneable {
Doris Liu0084e372015-04-10 12:39:35 -070033 /**
34 * Set this hint when duration for the animation does not need to be scaled. By default, no
35 * scaling is applied to the duration.
36 */
37 public static final int HINT_NO_SCALE = 0;
38
39 /**
Doris Liu7513aab2015-04-18 00:14:14 +000040 * Set this scale hint (using {@link #setDurationScaleHint(int, Resources)} when the animation's
Doris Liu0084e372015-04-10 12:39:35 -070041 * moving distance is proportional to the screen size. (e.g. a view coming in from the bottom of
42 * the screen to top/center). With this scale hint set, the animation duration will be
43 * automatically scaled based on screen size.
44 */
45 public static final int HINT_DISTANCE_PROPORTIONAL_TO_SCREEN_SIZE = 1;
46
47 /**
Doris Liu7513aab2015-04-18 00:14:14 +000048 * Set this scale hint (using {@link #setDurationScaleHint(int, Resources)}) if the animation
Doris Liu0084e372015-04-10 12:39:35 -070049 * has pre-defined moving distance in dp that does not vary from device to device. This is
50 * extremely useful when the animation needs to run on both phones/tablets and TV, because TV
51 * has inflated dp and therefore will have a longer visual arc for the same animation than on
52 * the phone. This hint is used to calculate a scaling factor to compensate for different
53 * visual arcs while maintaining the same angular velocity for the animation.
54 */
55 public static final int HINT_DISTANCE_DEFINED_IN_DP = 2;
Chet Haase17fb4b02010-06-28 17:55:07 -070056
57 /**
Chet Haase17fb4b02010-06-28 17:55:07 -070058 * The set of listeners to be sent events through the life of an animation.
59 */
Chet Haasea18a86b2010-09-07 13:20:00 -070060 ArrayList<AnimatorListener> mListeners = null;
Chet Haase17fb4b02010-06-28 17:55:07 -070061
62 /**
Chet Haase8aa1ffb2013-08-08 14:00:00 -070063 * The set of listeners to be sent pause/resume events through the life
64 * of an animation.
65 */
66 ArrayList<AnimatorPauseListener> mPauseListeners = null;
67
68 /**
69 * Whether this animator is currently in a paused state.
70 */
71 boolean mPaused = false;
72
73 /**
Yigit Boyard422dc32014-09-25 12:23:35 -070074 * A set of flags which identify the type of configuration changes that can affect this
75 * Animator. Used by the Animator cache.
76 */
77 int mChangingConfigurations = 0;
78
79 /**
80 * If this animator is inflated from a constant state, keep a reference to it so that
81 * ConstantState will not be garbage collected until this animator is collected
82 */
83 private AnimatorConstantState mConstantState;
84
85 /**
Doris Liu0084e372015-04-10 12:39:35 -070086 * Scaling factor for an animation that moves across the whole screen.
87 */
88 float mScreenSizeBasedDurationScale = 1.0f;
89
90 /**
91 * Scaling factor for an animation that is defined to move the same amount of dp across all
92 * devices.
93 */
94 float mDpBasedDurationScale = 1.0f;
95
96 /**
97 * By default, the scaling assumes the animation moves across the entire screen.
98 */
99 int mDurationScaleHint = HINT_NO_SCALE;
100
101 private final static boolean ANIM_DEBUG = false;
102
103 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700104 * Starts this animation. If the animation has a nonzero startDelay, the animation will start
Chet Haaseb8f574a2011-08-03 14:10:06 -0700105 * running after that delay elapses. A non-delayed animation will have its initial
106 * value(s) set immediately, followed by calls to
107 * {@link AnimatorListener#onAnimationStart(Animator)} for any listeners of this animator.
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800108 *
109 * <p>The animation started by calling this method will be run on the thread that called
110 * this method. This thread should have a Looper on it (a runtime exception will be thrown if
111 * this is not the case). Also, if the animation will animate
112 * properties of objects in the view hierarchy, then the calling thread should be the UI
113 * thread for that view hierarchy.</p>
114 *
Chet Haase17fb4b02010-06-28 17:55:07 -0700115 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700116 public void start() {
Chet Haasef54a8d72010-07-22 14:44:59 -0700117 }
118
Chet Haased953d082010-08-16 17:44:28 -0700119 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700120 * Cancels the animation. Unlike {@link #end()}, <code>cancel()</code> causes the animation to
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800121 * stop in its tracks, sending an
122 * {@link android.animation.Animator.AnimatorListener#onAnimationCancel(Animator)} to
123 * its listeners, followed by an
124 * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} message.
125 *
126 * <p>This method must be called on the thread that is running the animation.</p>
Chet Haased953d082010-08-16 17:44:28 -0700127 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700128 public void cancel() {
Chet Haased953d082010-08-16 17:44:28 -0700129 }
130
Chet Haase21cd1382010-09-01 17:42:29 -0700131 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700132 * Ends the animation. This causes the animation to assign the end value of the property being
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800133 * animated, then calling the
134 * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} method on
Chet Haasea18a86b2010-09-07 13:20:00 -0700135 * its listeners.
Chet Haasee2ab7cc2010-12-06 16:10:07 -0800136 *
137 * <p>This method must be called on the thread that is running the animation.</p>
Chet Haase21cd1382010-09-01 17:42:29 -0700138 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700139 public void end() {
Chet Haase17fb4b02010-06-28 17:55:07 -0700140 }
141
142 /**
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700143 * Pauses a running animation. This method should only be called on the same thread on
144 * which the animation was started. If the animation has not yet been {@link
145 * #isStarted() started} or has since ended, then the call is ignored. Paused
146 * animations can be resumed by calling {@link #resume()}.
147 *
148 * @see #resume()
149 * @see #isPaused()
150 * @see AnimatorPauseListener
151 */
152 public void pause() {
153 if (isStarted() && !mPaused) {
154 mPaused = true;
155 if (mPauseListeners != null) {
156 ArrayList<AnimatorPauseListener> tmpListeners =
157 (ArrayList<AnimatorPauseListener>) mPauseListeners.clone();
158 int numListeners = tmpListeners.size();
159 for (int i = 0; i < numListeners; ++i) {
160 tmpListeners.get(i).onAnimationPause(this);
161 }
162 }
163 }
164 }
165
166 /**
167 * Resumes a paused animation, causing the animator to pick up where it left off
168 * when it was paused. This method should only be called on the same thread on
169 * which the animation was started. Calls to resume() on an animator that is
170 * not currently paused will be ignored.
171 *
172 * @see #pause()
173 * @see #isPaused()
174 * @see AnimatorPauseListener
175 */
176 public void resume() {
177 if (mPaused) {
178 mPaused = false;
179 if (mPauseListeners != null) {
180 ArrayList<AnimatorPauseListener> tmpListeners =
181 (ArrayList<AnimatorPauseListener>) mPauseListeners.clone();
182 int numListeners = tmpListeners.size();
183 for (int i = 0; i < numListeners; ++i) {
184 tmpListeners.get(i).onAnimationResume(this);
185 }
186 }
187 }
188 }
189
190 /**
191 * Returns whether this animator is currently in a paused state.
192 *
193 * @return True if the animator is currently paused, false otherwise.
194 *
195 * @see #pause()
196 * @see #resume()
197 */
198 public boolean isPaused() {
199 return mPaused;
200 }
201
202 /**
Chet Haasee8cee38c2013-04-16 17:54:14 -0700203 * The amount of time, in milliseconds, to delay processing the animation
204 * after {@link #start()} is called.
205 *
206 * @return the number of milliseconds to delay running the animation
207 */
208 public abstract long getStartDelay();
209
210 /**
211 * The amount of time, in milliseconds, to delay processing the animation
212 * after {@link #start()} is called.
213
214 * @param startDelay The amount of the delay, in milliseconds
215 */
216 public abstract void setStartDelay(long startDelay);
217
218 /**
219 * Sets the duration of the animation.
220 *
221 * @param duration The length of the animation, in milliseconds.
222 */
223 public abstract Animator setDuration(long duration);
224
225 /**
226 * Gets the duration of the animation.
227 *
228 * @return The length of the animation, in milliseconds.
229 */
230 public abstract long getDuration();
231
232 /**
Doris Liu0084e372015-04-10 12:39:35 -0700233 * Hints how duration scaling factor should be calculated. The duration will not be scaled when
234 * hint is set to {@link #HINT_NO_SCALE}. Otherwise, the duration will be automatically scaled
235 * per device to achieve the same look and feel across different devices. In order to do
236 * that, the same angular velocity of the animation will be needed on different devices in
237 * users' field of view. Therefore, the duration scale factor is determined by the ratio of the
238 * angular movement on current devices to that on the baseline device (i.e. Nexus 5).
239 *
240 * @param hint an indicator on how the animation is defined. The hint could be
241 * {@link #HINT_NO_SCALE}, {@link #HINT_DISTANCE_PROPORTIONAL_TO_SCREEN_SIZE} or
242 * {@link #HINT_DISTANCE_DEFINED_IN_DP}.
Doris Liu7513aab2015-04-18 00:14:14 +0000243 * @param res The resources {@see android.content.res.Resources} for getting display metrics
Doris Liu0084e372015-04-10 12:39:35 -0700244 */
Doris Liu7513aab2015-04-18 00:14:14 +0000245 public void setDurationScaleHint(int hint, Resources res) {
Doris Liu0084e372015-04-10 12:39:35 -0700246 if (ANIM_DEBUG) {
247 Log.d("ANIM_DEBUG", "distance based duration hint: " + hint);
248 }
249 if (hint == mDurationScaleHint) {
250 return;
251 }
252 mDurationScaleHint = hint;
253 if (hint != HINT_NO_SCALE) {
254 int uiMode = res.getConfiguration().uiMode & Configuration.UI_MODE_TYPE_MASK;
Doris Liu7513aab2015-04-18 00:14:14 +0000255 DisplayMetrics metrics = res.getDisplayMetrics();
256 float width = metrics.widthPixels / metrics.xdpi;
257 float height = metrics.heightPixels / metrics.ydpi;
Doris Liu0084e372015-04-10 12:39:35 -0700258 float viewingDistance = AnimationUtils.getViewingDistance(width, height, uiMode);
259 if (ANIM_DEBUG) {
260 Log.d("ANIM_DEBUG", "width, height, viewing distance, uimode: "
261 + width + ", " + height + ", " + viewingDistance + ", " + uiMode);
262 }
263 mScreenSizeBasedDurationScale = AnimationUtils
264 .getScreenSizeBasedDurationScale(width, height, viewingDistance);
265 mDpBasedDurationScale = AnimationUtils.getDpBasedDurationScale(
Doris Liu7513aab2015-04-18 00:14:14 +0000266 metrics.density, metrics.xdpi, viewingDistance);
Doris Liu0084e372015-04-10 12:39:35 -0700267 if (ANIM_DEBUG) {
268 Log.d("ANIM_DEBUG", "screen based scale, dp based scale: " +
269 mScreenSizeBasedDurationScale + ", " + mDpBasedDurationScale);
270 }
271 }
272 }
273
274 // Copies duration scale hint and scaling factors to the new animation.
275 void copyDurationScaleInfoTo(Animator anim) {
276 anim.mDurationScaleHint = mDurationScaleHint;
277 anim.mScreenSizeBasedDurationScale = mScreenSizeBasedDurationScale;
278 anim.mDpBasedDurationScale = mDpBasedDurationScale;
279 }
280
281 /**
282 * @return The scaled duration calculated based on distance of movement (as defined by the
283 * animation) and perceived velocity (derived from the duration set on the animation for
284 * baseline device)
285 */
286 public long getDistanceBasedDuration() {
287 return (long) (getDuration() * getDistanceBasedDurationScale());
288 }
289
290 /**
291 * @return scaling factor of duration based on the duration scale hint. A scaling factor of 1
292 * means no scaling will be applied to the duration.
293 */
294 float getDistanceBasedDurationScale() {
295 if (mDurationScaleHint == HINT_DISTANCE_PROPORTIONAL_TO_SCREEN_SIZE) {
296 return mScreenSizeBasedDurationScale;
297 } else if (mDurationScaleHint == HINT_DISTANCE_DEFINED_IN_DP) {
298 return mDpBasedDurationScale;
299 } else {
300 return 1f;
301 }
302 }
303
304 /**
Chet Haasee8cee38c2013-04-16 17:54:14 -0700305 * The time interpolator used in calculating the elapsed fraction of the
306 * animation. The interpolator determines whether the animation runs with
307 * linear or non-linear motion, such as acceleration and deceleration. The
308 * default value is {@link android.view.animation.AccelerateDecelerateInterpolator}.
309 *
310 * @param value the interpolator to be used by this animation
311 */
312 public abstract void setInterpolator(TimeInterpolator value);
313
314 /**
315 * Returns the timing interpolator that this animation uses.
316 *
317 * @return The timing interpolator for this animation.
318 */
319 public TimeInterpolator getInterpolator() {
320 return null;
321 }
322
323 /**
Chet Haase8b699792011-08-05 15:20:19 -0700324 * Returns whether this Animator is currently running (having been started and gone past any
325 * initial startDelay period and not yet ended).
326 *
Chet Haasea18a86b2010-09-07 13:20:00 -0700327 * @return Whether the Animator is running.
328 */
329 public abstract boolean isRunning();
330
331 /**
Chet Haase8b699792011-08-05 15:20:19 -0700332 * Returns whether this Animator has been started and not yet ended. This state is a superset
333 * of the state of {@link #isRunning()}, because an Animator with a nonzero
334 * {@link #getStartDelay() startDelay} will return true for {@link #isStarted()} during the
335 * delay phase, whereas {@link #isRunning()} will return true only after the delay phase
336 * is complete.
337 *
338 * @return Whether the Animator has been started and not yet ended.
339 */
340 public boolean isStarted() {
341 // Default method returns value for isRunning(). Subclasses should override to return a
342 // real value.
343 return isRunning();
344 }
345
346 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700347 * Adds a listener to the set of listeners that are sent events through the life of an
348 * animation, such as start, repeat, and end.
349 *
350 * @param listener the listener to be added to the current set of listeners for this animation.
351 */
352 public void addListener(AnimatorListener listener) {
353 if (mListeners == null) {
354 mListeners = new ArrayList<AnimatorListener>();
355 }
356 mListeners.add(listener);
357 }
358
359 /**
360 * Removes a listener from the set listening to this animation.
361 *
362 * @param listener the listener to be removed from the current set of listeners for this
363 * animation.
364 */
365 public void removeListener(AnimatorListener listener) {
366 if (mListeners == null) {
367 return;
368 }
369 mListeners.remove(listener);
370 if (mListeners.size() == 0) {
371 mListeners = null;
Chet Haase17fb4b02010-06-28 17:55:07 -0700372 }
373 }
374
375 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700376 * Gets the set of {@link android.animation.Animator.AnimatorListener} objects that are currently
377 * listening for events on this <code>Animator</code> object.
Chet Haase602e4d32010-08-16 08:57:23 -0700378 *
Chet Haasea18a86b2010-09-07 13:20:00 -0700379 * @return ArrayList<AnimatorListener> The set of listeners.
Chet Haase602e4d32010-08-16 08:57:23 -0700380 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700381 public ArrayList<AnimatorListener> getListeners() {
382 return mListeners;
Chet Haase602e4d32010-08-16 08:57:23 -0700383 }
384
385 /**
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700386 * Adds a pause listener to this animator.
387 *
388 * @param listener the listener to be added to the current set of pause listeners
389 * for this animation.
390 */
391 public void addPauseListener(AnimatorPauseListener listener) {
392 if (mPauseListeners == null) {
393 mPauseListeners = new ArrayList<AnimatorPauseListener>();
394 }
395 mPauseListeners.add(listener);
396 }
397
398 /**
399 * Removes a pause listener from the set listening to this animation.
400 *
401 * @param listener the listener to be removed from the current set of pause
402 * listeners for this animation.
403 */
404 public void removePauseListener(AnimatorPauseListener listener) {
405 if (mPauseListeners == null) {
406 return;
407 }
408 mPauseListeners.remove(listener);
409 if (mPauseListeners.size() == 0) {
410 mPauseListeners = null;
411 }
412 }
413
414 /**
Chet Haased82c8ac2013-08-26 14:20:16 -0700415 * Removes all {@link #addListener(android.animation.Animator.AnimatorListener) listeners}
416 * and {@link #addPauseListener(android.animation.Animator.AnimatorPauseListener)
417 * pauseListeners} from this object.
Chet Haase17fb4b02010-06-28 17:55:07 -0700418 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700419 public void removeAllListeners() {
Chet Haase17fb4b02010-06-28 17:55:07 -0700420 if (mListeners != null) {
Chet Haasea18a86b2010-09-07 13:20:00 -0700421 mListeners.clear();
422 mListeners = null;
Chet Haase17fb4b02010-06-28 17:55:07 -0700423 }
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700424 if (mPauseListeners != null) {
425 mPauseListeners.clear();
426 mPauseListeners = null;
427 }
Chet Haase17fb4b02010-06-28 17:55:07 -0700428 }
429
Yigit Boyard422dc32014-09-25 12:23:35 -0700430 /**
431 * Return a mask of the configuration parameters for which this animator may change, requiring
432 * that it should be re-created from Resources. The default implementation returns whatever
433 * value was provided through setChangingConfigurations(int) or 0 by default.
434 *
435 * @return Returns a mask of the changing configuration parameters, as defined by
436 * {@link android.content.pm.ActivityInfo}.
437 * @see android.content.pm.ActivityInfo
438 * @hide
439 */
440 public int getChangingConfigurations() {
441 return mChangingConfigurations;
442 }
443
444 /**
445 * Set a mask of the configuration parameters for which this animator may change, requiring
446 * that it be re-created from resource.
447 *
448 * @param configs A mask of the changing configuration parameters, as
449 * defined by {@link android.content.pm.ActivityInfo}.
450 *
451 * @see android.content.pm.ActivityInfo
452 * @hide
453 */
454 public void setChangingConfigurations(int configs) {
455 mChangingConfigurations = configs;
456 }
457
458 /**
459 * Sets the changing configurations value to the union of the current changing configurations
460 * and the provided configs.
461 * This method is called while loading the animator.
462 * @hide
463 */
464 public void appendChangingConfigurations(int configs) {
465 mChangingConfigurations |= configs;
466 }
467
468 /**
469 * Return a {@link android.content.res.ConstantState} instance that holds the shared state of
470 * this Animator.
471 * <p>
472 * This constant state is used to create new instances of this animator when needed, instead
473 * of re-loading it from resources. Default implementation creates a new
474 * {@link AnimatorConstantState}. You can override this method to provide your custom logic or
475 * return null if you don't want this animator to be cached.
476 *
477 * @return The ConfigurationBoundResourceCache.BaseConstantState associated to this Animator.
478 * @see android.content.res.ConstantState
479 * @see #clone()
480 * @hide
481 */
482 public ConstantState<Animator> createConstantState() {
483 return new AnimatorConstantState(this);
484 }
485
Chet Haase49afa5b2010-08-23 11:39:53 -0700486 @Override
Chet Haase21cd1382010-09-01 17:42:29 -0700487 public Animator clone() {
Chet Haasea18a86b2010-09-07 13:20:00 -0700488 try {
489 final Animator anim = (Animator) super.clone();
490 if (mListeners != null) {
Yigit Boyard422dc32014-09-25 12:23:35 -0700491 anim.mListeners = new ArrayList<AnimatorListener>(mListeners);
Chet Haase49afa5b2010-08-23 11:39:53 -0700492 }
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700493 if (mPauseListeners != null) {
Yigit Boyard422dc32014-09-25 12:23:35 -0700494 anim.mPauseListeners = new ArrayList<AnimatorPauseListener>(mPauseListeners);
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700495 }
Chet Haasea18a86b2010-09-07 13:20:00 -0700496 return anim;
497 } catch (CloneNotSupportedException e) {
498 throw new AssertionError();
Chet Haase49afa5b2010-08-23 11:39:53 -0700499 }
Chet Haase49afa5b2010-08-23 11:39:53 -0700500 }
501
Chet Haase17fb4b02010-06-28 17:55:07 -0700502 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700503 * This method tells the object to use appropriate information to extract
504 * starting values for the animation. For example, a AnimatorSet object will pass
505 * this call to its child objects to tell them to set up the values. A
506 * ObjectAnimator object will use the information it has about its target object
507 * and PropertyValuesHolder objects to get the start values for its properties.
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900508 * A ValueAnimator object will ignore the request since it does not have enough
Chet Haasea18a86b2010-09-07 13:20:00 -0700509 * information (such as a target object) to gather these values.
Chet Haase17fb4b02010-06-28 17:55:07 -0700510 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700511 public void setupStartValues() {
512 }
513
514 /**
515 * This method tells the object to use appropriate information to extract
516 * ending values for the animation. For example, a AnimatorSet object will pass
517 * this call to its child objects to tell them to set up the values. A
518 * ObjectAnimator object will use the information it has about its target object
519 * and PropertyValuesHolder objects to get the start values for its properties.
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900520 * A ValueAnimator object will ignore the request since it does not have enough
Chet Haasea18a86b2010-09-07 13:20:00 -0700521 * information (such as a target object) to gather these values.
522 */
523 public void setupEndValues() {
524 }
525
526 /**
527 * Sets the target object whose property will be animated by this animation. Not all subclasses
528 * operate on target objects (for example, {@link ValueAnimator}, but this method
529 * is on the superclass for the convenience of dealing generically with those subclasses
530 * that do handle targets.
531 *
532 * @param target The object being animated
533 */
534 public void setTarget(Object target) {
535 }
536
ztenghui7bc6a3f2014-07-15 15:12:12 -0700537 // Hide reverse() and canReverse() for now since reverse() only work for simple
538 // cases, like we don't support sequential, neither startDelay.
539 // TODO: make reverse() works for all the Animators.
540 /**
541 * @hide
542 */
543 public boolean canReverse() {
544 return false;
545 }
546
547 /**
548 * @hide
549 */
550 public void reverse() {
John Reck291161a2014-07-22 07:31:09 -0700551 throw new IllegalStateException("Reverse is not supported");
ztenghui7bc6a3f2014-07-15 15:12:12 -0700552 }
553
Chet Haasea18a86b2010-09-07 13:20:00 -0700554 /**
555 * <p>An animation listener receives notifications from an animation.
556 * Notifications indicate animation related events, such as the end or the
557 * repetition of the animation.</p>
558 */
559 public static interface AnimatorListener {
Chet Haase17fb4b02010-06-28 17:55:07 -0700560 /**
Chet Haasea18a86b2010-09-07 13:20:00 -0700561 * <p>Notifies the start of the animation.</p>
562 *
563 * @param animation The started animation.
564 */
565 void onAnimationStart(Animator animation);
566
567 /**
568 * <p>Notifies the end of the animation. This callback is not invoked
569 * for animations with repeat count set to INFINITE.</p>
570 *
571 * @param animation The animation which reached its end.
572 */
573 void onAnimationEnd(Animator animation);
574
575 /**
576 * <p>Notifies the cancellation of the animation. This callback is not invoked
577 * for animations with repeat count set to INFINITE.</p>
578 *
579 * @param animation The animation which was canceled.
580 */
581 void onAnimationCancel(Animator animation);
582
583 /**
584 * <p>Notifies the repetition of the animation.</p>
Chet Haase17fb4b02010-06-28 17:55:07 -0700585 *
586 * @param animation The animation which was repeated.
587 */
Chet Haasea18a86b2010-09-07 13:20:00 -0700588 void onAnimationRepeat(Animator animation);
Chet Haase17fb4b02010-06-28 17:55:07 -0700589 }
Chet Haase8aa1ffb2013-08-08 14:00:00 -0700590
591 /**
592 * A pause listener receives notifications from an animation when the
593 * animation is {@link #pause() paused} or {@link #resume() resumed}.
594 *
595 * @see #addPauseListener(AnimatorPauseListener)
596 */
597 public static interface AnimatorPauseListener {
598 /**
599 * <p>Notifies that the animation was paused.</p>
600 *
601 * @param animation The animaton being paused.
602 * @see #pause()
603 */
604 void onAnimationPause(Animator animation);
605
606 /**
607 * <p>Notifies that the animation was resumed, after being
608 * previously paused.</p>
609 *
610 * @param animation The animation being resumed.
611 * @see #resume()
612 */
613 void onAnimationResume(Animator animation);
614 }
John Reckc01bd112014-07-18 16:22:09 -0700615
616 /**
617 * <p>Whether or not the Animator is allowed to run asynchronously off of
618 * the UI thread. This is a hint that informs the Animator that it is
619 * OK to run the animation off-thread, however the Animator may decide
620 * that it must run the animation on the UI thread anyway.
621 *
622 * <p>Regardless of whether or not the animation runs asynchronously, all
623 * listener callbacks will be called on the UI thread.</p>
624 *
625 * <p>To be able to use this hint the following must be true:</p>
626 * <ol>
627 * <li>The animator is immutable while {@link #isStarted()} is true. Requests
628 * to change duration, delay, etc... may be ignored.</li>
629 * <li>Lifecycle callback events may be asynchronous. Events such as
630 * {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or
631 * {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed
632 * as they must be posted back to the UI thread, and any actions performed
633 * by those callbacks (such as starting new animations) will not happen
634 * in the same frame.</li>
635 * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...)
636 * may be asynchronous. It is guaranteed that all state changes that are
637 * performed on the UI thread in the same frame will be applied as a single
638 * atomic update, however that frame may be the current frame,
639 * the next frame, or some future frame. This will also impact the observed
640 * state of the Animator. For example, {@link #isStarted()} may still return true
641 * after a call to {@link #end()}. Using the lifecycle callbacks is preferred over
642 * queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()}
643 * for this reason.</li>
644 * </ol>
645 * @hide
646 */
647 public void setAllowRunningAsynchronously(boolean mayRunAsync) {
648 // It is up to subclasses to support this, if they can.
649 }
Yigit Boyard422dc32014-09-25 12:23:35 -0700650
651 /**
652 * Creates a {@link ConstantState} which holds changing configurations information associated
653 * with the given Animator.
654 * <p>
655 * When {@link #newInstance()} is called, default implementation clones the Animator.
656 */
657 private static class AnimatorConstantState extends ConstantState<Animator> {
658
659 final Animator mAnimator;
660 int mChangingConf;
661
662 public AnimatorConstantState(Animator animator) {
663 mAnimator = animator;
664 // ensure a reference back to here so that constante state is not gc'ed.
665 mAnimator.mConstantState = this;
666 mChangingConf = mAnimator.getChangingConfigurations();
667 }
668
669 @Override
670 public int getChangingConfigurations() {
671 return mChangingConf;
672 }
673
674 @Override
675 public Animator newInstance() {
676 final Animator clone = mAnimator.clone();
677 clone.mConstantState = this;
678 return clone;
679 }
680 }
Chet Haasea18a86b2010-09-07 13:20:00 -0700681}