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; |
Artur Satayev | c895b1b | 2019-12-10 17:47:51 +0000 | [diff] [blame] | 20 | import android.compat.annotation.UnsupportedAppUsage; |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 21 | import android.content.pm.ActivityInfo.Config; |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 22 | import android.content.res.ConstantState; |
| 23 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
| 25 | |
| 26 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 27 | * 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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 29 | */ |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 30 | public abstract class Animator implements Cloneable { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 33 | * 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] | 34 | */ |
| 35 | public static final long DURATION_INFINITE = -1; |
| 36 | /** |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 37 | * The set of listeners to be sent events through the life of an animation. |
| 38 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 39 | ArrayList<AnimatorListener> mListeners = null; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 42 | * 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 Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 53 | * A set of flags which identify the type of configuration changes that can affect this |
| 54 | * Animator. Used by the Animator cache. |
| 55 | */ |
Alan Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 56 | @Config int mChangingConfigurations = 0; |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 57 | |
| 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 Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 65 | * 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] | 66 | * 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 Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 69 | * |
| 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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 76 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 77 | public void start() { |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 80 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 81 | * 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] | 82 | * 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 Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 88 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 89 | public void cancel() { |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 92 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 93 | * 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] | 94 | * animated, then calling the |
| 95 | * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} method on |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 96 | * its listeners. |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 97 | * |
| 98 | * <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] | 99 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 100 | public void end() { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 104 | * 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 Haase | e8cee38c | 2013-04-16 17:54:14 -0700 | [diff] [blame] | 164 | * 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 Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 194 | * 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 Liu | 8b7c99c | 2015-10-01 17:27:20 -0700 | [diff] [blame] | 196 | * |
| 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 Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 200 | */ |
| 201 | public long getTotalDuration() { |
Doris Liu | 8b7c99c | 2015-10-01 17:27:20 -0700 | [diff] [blame] | 202 | long duration = getDuration(); |
| 203 | if (duration == DURATION_INFINITE) { |
| 204 | return DURATION_INFINITE; |
| 205 | } else { |
| 206 | return getStartDelay() + duration; |
| 207 | } |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
Chet Haase | e8cee38c | 2013-04-16 17:54:14 -0700 | [diff] [blame] | 211 | * 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 Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 230 | * Returns whether this Animator is currently running (having been started and gone past any |
| 231 | * initial startDelay period and not yet ended). |
| 232 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 233 | * @return Whether the Animator is running. |
| 234 | */ |
| 235 | public abstract boolean isRunning(); |
| 236 | |
| 237 | /** |
Chet Haase | 4ddd925 | 2015-06-10 09:25:16 +0100 | [diff] [blame] | 238 | * 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 Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 247 | * |
| 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 Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 257 | * 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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 286 | * Gets the set of {@link android.animation.Animator.AnimatorListener} objects that are currently |
| 287 | * listening for events on this <code>Animator</code> object. |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 288 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 289 | * @return ArrayList<AnimatorListener> The set of listeners. |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 290 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 291 | public ArrayList<AnimatorListener> getListeners() { |
| 292 | return mListeners; |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 296 | * 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 Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 325 | * Removes all {@link #addListener(android.animation.Animator.AnimatorListener) listeners} |
| 326 | * and {@link #addPauseListener(android.animation.Animator.AnimatorPauseListener) |
| 327 | * pauseListeners} from this object. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 328 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 329 | public void removeAllListeners() { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 330 | if (mListeners != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 331 | mListeners.clear(); |
| 332 | mListeners = null; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 333 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 334 | if (mPauseListeners != null) { |
| 335 | mPauseListeners.clear(); |
| 336 | mPauseListeners = null; |
| 337 | } |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 340 | /** |
| 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 Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 350 | public @Config int getChangingConfigurations() { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 351 | 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 Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 364 | public void setChangingConfigurations(@Config int configs) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 365 | 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 Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 374 | public void appendChangingConfigurations(@Config int configs) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 375 | 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 Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 396 | @Override |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 397 | public Animator clone() { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 398 | try { |
| 399 | final Animator anim = (Animator) super.clone(); |
| 400 | if (mListeners != null) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 401 | anim.mListeners = new ArrayList<AnimatorListener>(mListeners); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 402 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 403 | if (mPauseListeners != null) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 404 | anim.mPauseListeners = new ArrayList<AnimatorPauseListener>(mPauseListeners); |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 405 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 406 | return anim; |
| 407 | } catch (CloneNotSupportedException e) { |
| 408 | throw new AssertionError(); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 409 | } |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 412 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 413 | * 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 Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 418 | * 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] | 419 | * information (such as a target object) to gather these values. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 420 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 421 | 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 Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 430 | * 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] | 431 | * 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 Haase | e60a693 | 2016-03-17 06:59:16 -0700 | [diff] [blame] | 441 | * <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 Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 445 | * |
| 446 | * @param target The object being animated |
| 447 | */ |
Chet Haase | e60a693 | 2016-03-17 06:59:16 -0700 | [diff] [blame] | 448 | public void setTarget(@Nullable Object target) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 449 | } |
| 450 | |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 451 | // 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 Inwood | a228c37 | 2018-08-01 14:35:45 +0100 | [diff] [blame] | 464 | @UnsupportedAppUsage |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 465 | public void reverse() { |
John Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 466 | throw new IllegalStateException("Reverse is not supported"); |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 469 | // Pulse an animation frame into the animation. |
| 470 | boolean pulseAnimationFrame(long frameTime) { |
Doris Liu | 6ba5ed3 | 2017-01-26 18:50:04 -0800 | [diff] [blame] | 471 | // 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 Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /** |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 477 | * 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 Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 521 | * <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 Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 526 | |
| 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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 557 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 558 | * <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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 582 | * |
| 583 | * @param animation The animation which was repeated. |
| 584 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 585 | void onAnimationRepeat(Animator animation); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 586 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 587 | |
| 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 Reck | c01bd11 | 2014-07-18 16:22:09 -0700 | [diff] [blame] | 612 | |
| 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 Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 647 | |
| 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 Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 657 | @Config int mChangingConf; |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 658 | |
| 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 Viverette | ac85f90 | 2016-03-11 15:15:51 -0500 | [diff] [blame] | 667 | public @Config int getChangingConfigurations() { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 668 | 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 Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 678 | } |