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 | |
Doris Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 19 | import android.content.res.Configuration; |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 20 | import android.content.res.ConstantState; |
Doris Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
| 22 | import android.util.DisplayMetrics; |
| 23 | import android.util.Log; |
| 24 | import android.view.animation.AnimationUtils; |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 25 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
| 27 | |
| 28 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 29 | * 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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 31 | */ |
Chet Haase | e8cee38c | 2013-04-16 17:54:14 -0700 | [diff] [blame] | 32 | public abstract class Animator implements Cloneable { |
Doris Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 33 | /** |
| 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 Liu | 7513aab | 2015-04-18 00:14:14 +0000 | [diff] [blame^] | 40 | * Set this scale hint (using {@link #setDurationScaleHint(int, Resources)} when the animation's |
Doris Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 41 | * 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 Liu | 7513aab | 2015-04-18 00:14:14 +0000 | [diff] [blame^] | 48 | * Set this scale hint (using {@link #setDurationScaleHint(int, Resources)}) if the animation |
Doris Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 49 | * 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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 58 | * The set of listeners to be sent events through the life of an animation. |
| 59 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 60 | ArrayList<AnimatorListener> mListeners = null; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 63 | * 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 Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 74 | * 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 Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 86 | * 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 Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 104 | * 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] | 105 | * 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 Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 108 | * |
| 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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 115 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 116 | public void start() { |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 119 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 120 | * 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] | 121 | * 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 Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 127 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 128 | public void cancel() { |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 131 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 132 | * 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] | 133 | * animated, then calling the |
| 134 | * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} method on |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 135 | * its listeners. |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 136 | * |
| 137 | * <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] | 138 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 139 | public void end() { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 143 | * 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 Haase | e8cee38c | 2013-04-16 17:54:14 -0700 | [diff] [blame] | 203 | * 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 Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 233 | * 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 Liu | 7513aab | 2015-04-18 00:14:14 +0000 | [diff] [blame^] | 243 | * @param res The resources {@see android.content.res.Resources} for getting display metrics |
Doris Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 244 | */ |
Doris Liu | 7513aab | 2015-04-18 00:14:14 +0000 | [diff] [blame^] | 245 | public void setDurationScaleHint(int hint, Resources res) { |
Doris Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 246 | 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 Liu | 7513aab | 2015-04-18 00:14:14 +0000 | [diff] [blame^] | 255 | DisplayMetrics metrics = res.getDisplayMetrics(); |
| 256 | float width = metrics.widthPixels / metrics.xdpi; |
| 257 | float height = metrics.heightPixels / metrics.ydpi; |
Doris Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 258 | 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 Liu | 7513aab | 2015-04-18 00:14:14 +0000 | [diff] [blame^] | 266 | metrics.density, metrics.xdpi, viewingDistance); |
Doris Liu | 0084e37 | 2015-04-10 12:39:35 -0700 | [diff] [blame] | 267 | 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 Haase | e8cee38c | 2013-04-16 17:54:14 -0700 | [diff] [blame] | 305 | * 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 Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 324 | * Returns whether this Animator is currently running (having been started and gone past any |
| 325 | * initial startDelay period and not yet ended). |
| 326 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 327 | * @return Whether the Animator is running. |
| 328 | */ |
| 329 | public abstract boolean isRunning(); |
| 330 | |
| 331 | /** |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 332 | * 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 Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 347 | * 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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 376 | * Gets the set of {@link android.animation.Animator.AnimatorListener} objects that are currently |
| 377 | * listening for events on this <code>Animator</code> object. |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 378 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 379 | * @return ArrayList<AnimatorListener> The set of listeners. |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 380 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 381 | public ArrayList<AnimatorListener> getListeners() { |
| 382 | return mListeners; |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 386 | * 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 Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 415 | * Removes all {@link #addListener(android.animation.Animator.AnimatorListener) listeners} |
| 416 | * and {@link #addPauseListener(android.animation.Animator.AnimatorPauseListener) |
| 417 | * pauseListeners} from this object. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 418 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 419 | public void removeAllListeners() { |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 420 | if (mListeners != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 421 | mListeners.clear(); |
| 422 | mListeners = null; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 423 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 424 | if (mPauseListeners != null) { |
| 425 | mPauseListeners.clear(); |
| 426 | mPauseListeners = null; |
| 427 | } |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 430 | /** |
| 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 Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 486 | @Override |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 487 | public Animator clone() { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 488 | try { |
| 489 | final Animator anim = (Animator) super.clone(); |
| 490 | if (mListeners != null) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 491 | anim.mListeners = new ArrayList<AnimatorListener>(mListeners); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 492 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 493 | if (mPauseListeners != null) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 494 | anim.mPauseListeners = new ArrayList<AnimatorPauseListener>(mPauseListeners); |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 495 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 496 | return anim; |
| 497 | } catch (CloneNotSupportedException e) { |
| 498 | throw new AssertionError(); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 499 | } |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 502 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 503 | * 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 Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 508 | * 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] | 509 | * information (such as a target object) to gather these values. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 510 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 511 | 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 Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 520 | * 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] | 521 | * 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 | |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 537 | // 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 Reck | 291161a | 2014-07-22 07:31:09 -0700 | [diff] [blame] | 551 | throw new IllegalStateException("Reverse is not supported"); |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 554 | /** |
| 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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 560 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 561 | * <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 Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 585 | * |
| 586 | * @param animation The animation which was repeated. |
| 587 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 588 | void onAnimationRepeat(Animator animation); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 589 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 590 | |
| 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 Reck | c01bd11 | 2014-07-18 16:22:09 -0700 | [diff] [blame] | 615 | |
| 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 Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 650 | |
| 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 Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 681 | } |