Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.animation; |
| 18 | |
Chet Haase | e60a693 | 2016-03-17 06:59:16 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 20 | import android.content.pm.ActivityInfo.Config; |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 21 | import android.content.res.ConstantState; |
| 22 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 23 | import java.util.ArrayList; |
| 24 | |
| 25 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 26 | * This is the superclass for classes which provide basic support for animations which can be |
| 27 | * started, ended, and have <code>AnimatorListeners</code> added to them. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 28 | */ |
Chet Haase | e8cee38c | 2013-04-16 17:54:14 -0700 | [diff] [blame] | 29 | public abstract class Animator implements Cloneable { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 32 | * The value used to indicate infinite duration (e.g. when Animators repeat infinitely). |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 33 | */ |
| 34 | public static final long DURATION_INFINITE = -1; |
| 35 | /** |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 36 | * The set of listeners to be sent events through the life of an animation. |
| 37 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 38 | ArrayList<AnimatorListener> mListeners = null; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 41 | * The set of listeners to be sent pause/resume events through the life |
| 42 | * of an animation. |
| 43 | */ |
| 44 | ArrayList<AnimatorPauseListener> mPauseListeners = null; |
| 45 | |
| 46 | /** |
| 47 | * Whether this animator is currently in a paused state. |
| 48 | */ |
| 49 | boolean mPaused = false; |
| 50 | |
| 51 | /** |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 52 | * A set of flags which identify the type of configuration changes that can affect this |
| 53 | * Animator. Used by the Animator cache. |
| 54 | */ |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 55 | @Config int mChangingConfigurations = 0; |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * If this animator is inflated from a constant state, keep a reference to it so that |
| 59 | * ConstantState will not be garbage collected until this animator is collected |
| 60 | */ |
| 61 | private AnimatorConstantState mConstantState; |
| 62 | |
| 63 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 64 | * Starts this animation. If the animation has a nonzero startDelay, the animation will start |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 65 | * running after that delay elapses. A non-delayed animation will have its initial |
| 66 | * value(s) set immediately, followed by calls to |
| 67 | * {@link AnimatorListener#onAnimationStart(Animator)} for any listeners of this animator. |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 68 | * |
| 69 | * <p>The animation started by calling this method will be run on the thread that called |
| 70 | * this method. This thread should have a Looper on it (a runtime exception will be thrown if |
| 71 | * this is not the case). Also, if the animation will animate |
| 72 | * properties of objects in the view hierarchy, then the calling thread should be the UI |
| 73 | * thread for that view hierarchy.</p> |
| 74 | * |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 75 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 76 | public void start() { |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 79 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 80 | * Cancels the animation. Unlike {@link #end()}, <code>cancel()</code> causes the animation to |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 81 | * stop in its tracks, sending an |
| 82 | * {@link android.animation.Animator.AnimatorListener#onAnimationCancel(Animator)} to |
| 83 | * its listeners, followed by an |
| 84 | * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} message. |
| 85 | * |
| 86 | * <p>This method must be called on the thread that is running the animation.</p> |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 87 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 88 | public void cancel() { |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 91 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 92 | * Ends the animation. This causes the animation to assign the end value of the property being |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 93 | * animated, then calling the |
| 94 | * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} method on |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 95 | * its listeners. |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 96 | * |
| 97 | * <p>This method must be called on the thread that is running the animation.</p> |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 98 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 99 | public void end() { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 103 | * Pauses a running animation. This method should only be called on the same thread on |
| 104 | * which the animation was started. If the animation has not yet been {@link |
| 105 | * #isStarted() started} or has since ended, then the call is ignored. Paused |
| 106 | * animations can be resumed by calling {@link #resume()}. |
| 107 | * |
| 108 | * @see #resume() |
| 109 | * @see #isPaused() |
| 110 | * @see AnimatorPauseListener |
| 111 | */ |
| 112 | public void pause() { |
| 113 | if (isStarted() && !mPaused) { |
| 114 | mPaused = true; |
| 115 | if (mPauseListeners != null) { |
| 116 | ArrayList<AnimatorPauseListener> tmpListeners = |
| 117 | (ArrayList<AnimatorPauseListener>) mPauseListeners.clone(); |
| 118 | int numListeners = tmpListeners.size(); |
| 119 | for (int i = 0; i < numListeners; ++i) { |
| 120 | tmpListeners.get(i).onAnimationPause(this); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Resumes a paused animation, causing the animator to pick up where it left off |
| 128 | * when it was paused. This method should only be called on the same thread on |
| 129 | * which the animation was started. Calls to resume() on an animator that is |
| 130 | * not currently paused will be ignored. |
| 131 | * |
| 132 | * @see #pause() |
| 133 | * @see #isPaused() |
| 134 | * @see AnimatorPauseListener |
| 135 | */ |
| 136 | public void resume() { |
| 137 | if (mPaused) { |
| 138 | mPaused = false; |
| 139 | if (mPauseListeners != null) { |
| 140 | ArrayList<AnimatorPauseListener> tmpListeners = |
| 141 | (ArrayList<AnimatorPauseListener>) mPauseListeners.clone(); |
| 142 | int numListeners = tmpListeners.size(); |
| 143 | for (int i = 0; i < numListeners; ++i) { |
| 144 | tmpListeners.get(i).onAnimationResume(this); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns whether this animator is currently in a paused state. |
| 152 | * |
| 153 | * @return True if the animator is currently paused, false otherwise. |
| 154 | * |
| 155 | * @see #pause() |
| 156 | * @see #resume() |
| 157 | */ |
| 158 | public boolean isPaused() { |
| 159 | return mPaused; |
| 160 | } |
| 161 | |
| 162 | /** |
Chet Haase | e8cee38c | 2013-04-16 17:54:14 -0700 | [diff] [blame] | 163 | * The amount of time, in milliseconds, to delay processing the animation |
| 164 | * after {@link #start()} is called. |
| 165 | * |
| 166 | * @return the number of milliseconds to delay running the animation |
| 167 | */ |
| 168 | public abstract long getStartDelay(); |
| 169 | |
| 170 | /** |
| 171 | * The amount of time, in milliseconds, to delay processing the animation |
| 172 | * after {@link #start()} is called. |
| 173 | |
| 174 | * @param startDelay The amount of the delay, in milliseconds |
| 175 | */ |
| 176 | public abstract void setStartDelay(long startDelay); |
| 177 | |
| 178 | /** |
| 179 | * Sets the duration of the animation. |
| 180 | * |
| 181 | * @param duration The length of the animation, in milliseconds. |
| 182 | */ |
| 183 | public abstract Animator setDuration(long duration); |
| 184 | |
| 185 | /** |
| 186 | * Gets the duration of the animation. |
| 187 | * |
| 188 | * @return The length of the animation, in milliseconds. |
| 189 | */ |
| 190 | public abstract long getDuration(); |
| 191 | |
| 192 | /** |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 193 | * Gets the total duration of the animation, accounting for animation sequences, start delay, |
| 194 | * and repeating. Return {@link #DURATION_INFINITE} if the duration is infinite. |
Doris Liu | 8b7c99c | 2015-10-01 17:27:20 -0700 | [diff] [blame] | 195 | * |
| 196 | * @return Total time an animation takes to finish, starting from the time {@link #start()} |
| 197 | * is called. {@link #DURATION_INFINITE} will be returned if the animation or any |
| 198 | * child animation repeats infinite times. |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 199 | */ |
| 200 | public long getTotalDuration() { |
Doris Liu | 8b7c99c | 2015-10-01 17:27:20 -0700 | [diff] [blame] | 201 | long duration = getDuration(); |
| 202 | if (duration == DURATION_INFINITE) { |
| 203 | return DURATION_INFINITE; |
| 204 | } else { |
| 205 | return getStartDelay() + duration; |
| 206 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Chet Haase | e8cee38c | 2013-04-16 17:54:14 -0700 | [diff] [blame] | 210 | * The time interpolator used in calculating the elapsed fraction of the |
| 211 | * animation. The interpolator determines whether the animation runs with |
| 212 | * linear or non-linear motion, such as acceleration and deceleration. The |
| 213 | * default value is {@link android.view.animation.AccelerateDecelerateInterpolator}. |
| 214 | * |
| 215 | * @param value the interpolator to be used by this animation |
| 216 | */ |
| 217 | public abstract void setInterpolator(TimeInterpolator value); |
| 218 | |
| 219 | /** |
| 220 | * Returns the timing interpolator that this animation uses. |
| 221 | * |
| 222 | * @return The timing interpolator for this animation. |
| 223 | */ |
| 224 | public TimeInterpolator getInterpolator() { |
| 225 | return null; |
| 226 | } |
| 227 | |
| 228 | /** |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 229 | * Returns whether this Animator is currently running (having been started and gone past any |
| 230 | * initial startDelay period and not yet ended). |
| 231 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 232 | * @return Whether the Animator is running. |
| 233 | */ |
| 234 | public abstract boolean isRunning(); |
| 235 | |
| 236 | /** |
Chet Haase | 4ddd925 | 2015-06-10 09:25:16 +0100 | [diff] [blame] | 237 | * Returns whether this Animator has been started and not yet ended. For reusable |
| 238 | * Animators (which most Animators are, apart from the one-shot animator produced by |
| 239 | * {@link android.view.ViewAnimationUtils#createCircularReveal( |
| 240 | * android.view.View, int, int, float, float) createCircularReveal()}), |
| 241 | * this state is a superset of {@link #isRunning()}, because an Animator with a |
| 242 | * nonzero {@link #getStartDelay() startDelay} will return true for {@link #isStarted()} during |
| 243 | * the delay phase, whereas {@link #isRunning()} will return true only after the delay phase |
| 244 | * is complete. Non-reusable animators will always return true after they have been |
| 245 | * started, because they cannot return to a non-started state. |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 246 | * |
| 247 | * @return Whether the Animator has been started and not yet ended. |
| 248 | */ |
| 249 | public boolean isStarted() { |
| 250 | // Default method returns value for isRunning(). Subclasses should override to return a |
| 251 | // real value. |
| 252 | return isRunning(); |
| 253 | } |
| 254 | |
| 255 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 256 | * Adds a listener to the set of listeners that are sent events through the life of an |
| 257 | * animation, such as start, repeat, and end. |
| 258 | * |
| 259 | * @param listener the listener to be added to the current set of listeners for this animation. |
| 260 | */ |
| 261 | public void addListener(AnimatorListener listener) { |
| 262 | if (mListeners == null) { |
| 263 | mListeners = new ArrayList<AnimatorListener>(); |
| 264 | } |
| 265 | mListeners.add(listener); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Removes a listener from the set listening to this animation. |
| 270 | * |
| 271 | * @param listener the listener to be removed from the current set of listeners for this |
| 272 | * animation. |
| 273 | */ |
| 274 | public void removeListener(AnimatorListener listener) { |
| 275 | if (mListeners == null) { |
| 276 | return; |
| 277 | } |
| 278 | mListeners.remove(listener); |
| 279 | if (mListeners.size() == 0) { |
| 280 | mListeners = null; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 285 | * Gets the set of {@link android.animation.Animator.AnimatorListener} objects that are currently |
| 286 | * listening for events on this <code>Animator</code> object. |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 287 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 288 | * @return ArrayList<AnimatorListener> The set of listeners. |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 289 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 290 | public ArrayList<AnimatorListener> getListeners() { |
| 291 | return mListeners; |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 295 | * Adds a pause listener to this animator. |
| 296 | * |
| 297 | * @param listener the listener to be added to the current set of pause listeners |
| 298 | * for this animation. |
| 299 | */ |
| 300 | public void addPauseListener(AnimatorPauseListener listener) { |
| 301 | if (mPauseListeners == null) { |
| 302 | mPauseListeners = new ArrayList<AnimatorPauseListener>(); |
| 303 | } |
| 304 | mPauseListeners.add(listener); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Removes a pause listener from the set listening to this animation. |
| 309 | * |
| 310 | * @param listener the listener to be removed from the current set of pause |
| 311 | * listeners for this animation. |
| 312 | */ |
| 313 | public void removePauseListener(AnimatorPauseListener listener) { |
| 314 | if (mPauseListeners == null) { |
| 315 | return; |
| 316 | } |
| 317 | mPauseListeners.remove(listener); |
| 318 | if (mPauseListeners.size() == 0) { |
| 319 | mPauseListeners = null; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 324 | * Removes all {@link #addListener(android.animation.Animator.AnimatorListener) listeners} |
| 325 | * and {@link #addPauseListener(android.animation.Animator.AnimatorPauseListener) |
| 326 | * pauseListeners} from this object. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 327 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 328 | public void removeAllListeners() { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 329 | if (mListeners != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 330 | mListeners.clear(); |
| 331 | mListeners = null; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 332 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 333 | if (mPauseListeners != null) { |
| 334 | mPauseListeners.clear(); |
| 335 | mPauseListeners = null; |
| 336 | } |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 339 | /** |
| 340 | * Return a mask of the configuration parameters for which this animator may change, requiring |
| 341 | * that it should be re-created from Resources. The default implementation returns whatever |
| 342 | * value was provided through setChangingConfigurations(int) or 0 by default. |
| 343 | * |
| 344 | * @return Returns a mask of the changing configuration parameters, as defined by |
| 345 | * {@link android.content.pm.ActivityInfo}. |
| 346 | * @see android.content.pm.ActivityInfo |
| 347 | * @hide |
| 348 | */ |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 349 | public @Config int getChangingConfigurations() { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 350 | return mChangingConfigurations; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Set a mask of the configuration parameters for which this animator may change, requiring |
| 355 | * that it be re-created from resource. |
| 356 | * |
| 357 | * @param configs A mask of the changing configuration parameters, as |
| 358 | * defined by {@link android.content.pm.ActivityInfo}. |
| 359 | * |
| 360 | * @see android.content.pm.ActivityInfo |
| 361 | * @hide |
| 362 | */ |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 363 | public void setChangingConfigurations(@Config int configs) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 364 | mChangingConfigurations = configs; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Sets the changing configurations value to the union of the current changing configurations |
| 369 | * and the provided configs. |
| 370 | * This method is called while loading the animator. |
| 371 | * @hide |
| 372 | */ |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 373 | public void appendChangingConfigurations(@Config int configs) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 374 | mChangingConfigurations |= configs; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Return a {@link android.content.res.ConstantState} instance that holds the shared state of |
| 379 | * this Animator. |
| 380 | * <p> |
| 381 | * This constant state is used to create new instances of this animator when needed, instead |
| 382 | * of re-loading it from resources. Default implementation creates a new |
| 383 | * {@link AnimatorConstantState}. You can override this method to provide your custom logic or |
| 384 | * return null if you don't want this animator to be cached. |
| 385 | * |
| 386 | * @return The ConfigurationBoundResourceCache.BaseConstantState associated to this Animator. |
| 387 | * @see android.content.res.ConstantState |
| 388 | * @see #clone() |
| 389 | * @hide |
| 390 | */ |
| 391 | public ConstantState<Animator> createConstantState() { |
| 392 | return new AnimatorConstantState(this); |
| 393 | } |
| 394 | |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 395 | @Override |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 396 | public Animator clone() { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 397 | try { |
| 398 | final Animator anim = (Animator) super.clone(); |
| 399 | if (mListeners != null) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 400 | anim.mListeners = new ArrayList<AnimatorListener>(mListeners); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 401 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 402 | if (mPauseListeners != null) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 403 | anim.mPauseListeners = new ArrayList<AnimatorPauseListener>(mPauseListeners); |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 404 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 405 | return anim; |
| 406 | } catch (CloneNotSupportedException e) { |
| 407 | throw new AssertionError(); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 408 | } |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 411 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 412 | * This method tells the object to use appropriate information to extract |
| 413 | * starting values for the animation. For example, a AnimatorSet object will pass |
| 414 | * this call to its child objects to tell them to set up the values. A |
| 415 | * ObjectAnimator object will use the information it has about its target object |
| 416 | * and PropertyValuesHolder objects to get the start values for its properties. |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 417 | * A ValueAnimator object will ignore the request since it does not have enough |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 418 | * information (such as a target object) to gather these values. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 419 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 420 | public void setupStartValues() { |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * This method tells the object to use appropriate information to extract |
| 425 | * ending values for the animation. For example, a AnimatorSet object will pass |
| 426 | * this call to its child objects to tell them to set up the values. A |
| 427 | * ObjectAnimator object will use the information it has about its target object |
| 428 | * and PropertyValuesHolder objects to get the start values for its properties. |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 429 | * A ValueAnimator object will ignore the request since it does not have enough |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 430 | * information (such as a target object) to gather these values. |
| 431 | */ |
| 432 | public void setupEndValues() { |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Sets the target object whose property will be animated by this animation. Not all subclasses |
| 437 | * operate on target objects (for example, {@link ValueAnimator}, but this method |
| 438 | * is on the superclass for the convenience of dealing generically with those subclasses |
| 439 | * that do handle targets. |
Chet Haase | e60a693 | 2016-03-17 06:59:16 -0700 | [diff] [blame] | 440 | * <p> |
| 441 | * <strong>Note:</strong> The target is stored as a weak reference internally to avoid leaking |
| 442 | * resources by having animators directly reference old targets. Therefore, you should |
| 443 | * ensure that animator targets always have a hard reference elsewhere. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 444 | * |
| 445 | * @param target The object being animated |
| 446 | */ |
Chet Haase | e60a693 | 2016-03-17 06:59:16 -0700 | [diff] [blame] | 447 | public void setTarget(@Nullable Object target) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 448 | } |
| 449 | |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 450 | // Hide reverse() and canReverse() for now since reverse() only work for simple |
| 451 | // cases, like we don't support sequential, neither startDelay. |
| 452 | // TODO: make reverse() works for all the Animators. |
| 453 | /** |
| 454 | * @hide |
| 455 | */ |
| 456 | public boolean canReverse() { |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * @hide |
| 462 | */ |
| 463 | public void reverse() { |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 464 | throw new IllegalStateException("Reverse is not supported"); |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 467 | /** |
| 468 | * <p>An animation listener receives notifications from an animation. |
| 469 | * Notifications indicate animation related events, such as the end or the |
| 470 | * repetition of the animation.</p> |
| 471 | */ |
| 472 | public static interface AnimatorListener { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 473 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 474 | * <p>Notifies the start of the animation.</p> |
| 475 | * |
| 476 | * @param animation The started animation. |
| 477 | */ |
| 478 | void onAnimationStart(Animator animation); |
| 479 | |
| 480 | /** |
| 481 | * <p>Notifies the end of the animation. This callback is not invoked |
| 482 | * for animations with repeat count set to INFINITE.</p> |
| 483 | * |
| 484 | * @param animation The animation which reached its end. |
| 485 | */ |
| 486 | void onAnimationEnd(Animator animation); |
| 487 | |
| 488 | /** |
| 489 | * <p>Notifies the cancellation of the animation. This callback is not invoked |
| 490 | * for animations with repeat count set to INFINITE.</p> |
| 491 | * |
| 492 | * @param animation The animation which was canceled. |
| 493 | */ |
| 494 | void onAnimationCancel(Animator animation); |
| 495 | |
| 496 | /** |
| 497 | * <p>Notifies the repetition of the animation.</p> |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 498 | * |
| 499 | * @param animation The animation which was repeated. |
| 500 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 501 | void onAnimationRepeat(Animator animation); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 502 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 503 | |
| 504 | /** |
| 505 | * A pause listener receives notifications from an animation when the |
| 506 | * animation is {@link #pause() paused} or {@link #resume() resumed}. |
| 507 | * |
| 508 | * @see #addPauseListener(AnimatorPauseListener) |
| 509 | */ |
| 510 | public static interface AnimatorPauseListener { |
| 511 | /** |
| 512 | * <p>Notifies that the animation was paused.</p> |
| 513 | * |
| 514 | * @param animation The animaton being paused. |
| 515 | * @see #pause() |
| 516 | */ |
| 517 | void onAnimationPause(Animator animation); |
| 518 | |
| 519 | /** |
| 520 | * <p>Notifies that the animation was resumed, after being |
| 521 | * previously paused.</p> |
| 522 | * |
| 523 | * @param animation The animation being resumed. |
| 524 | * @see #resume() |
| 525 | */ |
| 526 | void onAnimationResume(Animator animation); |
| 527 | } |
John Reck | c01bd11 | 2014-07-18 16:22:09 -0700 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * <p>Whether or not the Animator is allowed to run asynchronously off of |
| 531 | * the UI thread. This is a hint that informs the Animator that it is |
| 532 | * OK to run the animation off-thread, however the Animator may decide |
| 533 | * that it must run the animation on the UI thread anyway. |
| 534 | * |
| 535 | * <p>Regardless of whether or not the animation runs asynchronously, all |
| 536 | * listener callbacks will be called on the UI thread.</p> |
| 537 | * |
| 538 | * <p>To be able to use this hint the following must be true:</p> |
| 539 | * <ol> |
| 540 | * <li>The animator is immutable while {@link #isStarted()} is true. Requests |
| 541 | * to change duration, delay, etc... may be ignored.</li> |
| 542 | * <li>Lifecycle callback events may be asynchronous. Events such as |
| 543 | * {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or |
| 544 | * {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed |
| 545 | * as they must be posted back to the UI thread, and any actions performed |
| 546 | * by those callbacks (such as starting new animations) will not happen |
| 547 | * in the same frame.</li> |
| 548 | * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...) |
| 549 | * may be asynchronous. It is guaranteed that all state changes that are |
| 550 | * performed on the UI thread in the same frame will be applied as a single |
| 551 | * atomic update, however that frame may be the current frame, |
| 552 | * the next frame, or some future frame. This will also impact the observed |
| 553 | * state of the Animator. For example, {@link #isStarted()} may still return true |
| 554 | * after a call to {@link #end()}. Using the lifecycle callbacks is preferred over |
| 555 | * queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()} |
| 556 | * for this reason.</li> |
| 557 | * </ol> |
| 558 | * @hide |
| 559 | */ |
| 560 | public void setAllowRunningAsynchronously(boolean mayRunAsync) { |
| 561 | // It is up to subclasses to support this, if they can. |
| 562 | } |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 563 | |
| 564 | /** |
| 565 | * Creates a {@link ConstantState} which holds changing configurations information associated |
| 566 | * with the given Animator. |
| 567 | * <p> |
| 568 | * When {@link #newInstance()} is called, default implementation clones the Animator. |
| 569 | */ |
| 570 | private static class AnimatorConstantState extends ConstantState<Animator> { |
| 571 | |
| 572 | final Animator mAnimator; |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 573 | @Config int mChangingConf; |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 574 | |
| 575 | public AnimatorConstantState(Animator animator) { |
| 576 | mAnimator = animator; |
| 577 | // ensure a reference back to here so that constante state is not gc'ed. |
| 578 | mAnimator.mConstantState = this; |
| 579 | mChangingConf = mAnimator.getChangingConfigurations(); |
| 580 | } |
| 581 | |
| 582 | @Override |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 583 | public @Config int getChangingConfigurations() { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 584 | return mChangingConf; |
| 585 | } |
| 586 | |
| 587 | @Override |
| 588 | public Animator newInstance() { |
| 589 | final Animator clone = mAnimator.clone(); |
| 590 | clone.mConstantState = this; |
| 591 | return clone; |
| 592 | } |
| 593 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 594 | } |