Chet Haase | a18a86b | 2010-09-07 13:20:00 -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 | |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 19 | import android.annotation.CallSuper; |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Teng-Hui Zhu | 0a815bb | 2016-08-22 15:48:41 -0700 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Mathew Inwood | a228c37 | 2018-08-01 14:35:45 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Daniel Santiago Rivera | 61fe23a | 2019-02-05 12:41:39 -0800 | [diff] [blame] | 23 | import android.os.Build; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 24 | import android.os.Looper; |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 25 | import android.os.Trace; |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 26 | import android.util.AndroidRuntimeException; |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 27 | import android.util.Log; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 28 | import android.view.animation.AccelerateDecelerateInterpolator; |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 29 | import android.view.animation.Animation; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 30 | import android.view.animation.AnimationUtils; |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 31 | import android.view.animation.LinearInterpolator; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 32 | |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame] | 33 | import java.lang.annotation.Retention; |
| 34 | import java.lang.annotation.RetentionPolicy; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.HashMap; |
| 37 | |
| 38 | /** |
| 39 | * This class provides a simple timing engine for running animations |
| 40 | * which calculate animated values and set them on target objects. |
| 41 | * |
| 42 | * <p>There is a single timing pulse that all animations use. It runs in a |
| 43 | * custom handler to ensure that property changes happen on the UI thread.</p> |
| 44 | * |
| 45 | * <p>By default, ValueAnimator uses non-linear time interpolation, via the |
| 46 | * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates |
| 47 | * out of an animation. This behavior can be changed by calling |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 48 | * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 49 | * |
Chet Haase | d430753 | 2014-12-01 06:32:38 -0800 | [diff] [blame] | 50 | * <p>Animators can be created from either code or resource files. Here is an example |
| 51 | * of a ValueAnimator resource file:</p> |
| 52 | * |
| 53 | * {@sample development/samples/ApiDemos/res/anim/animator.xml ValueAnimatorResources} |
| 54 | * |
Doris Liu | 21b93c1 | 2016-12-02 15:23:55 -0800 | [diff] [blame] | 55 | * <p>Starting from API 23, it is also possible to use a combination of {@link PropertyValuesHolder} |
| 56 | * and {@link Keyframe} resource tags to create a multi-step animation. |
Chet Haase | d430753 | 2014-12-01 06:32:38 -0800 | [diff] [blame] | 57 | * Note that you can specify explicit fractional values (from 0 to 1) for |
| 58 | * each keyframe to determine when, in the overall duration, the animation should arrive at that |
| 59 | * value. Alternatively, you can leave the fractions off and the keyframes will be equally |
| 60 | * distributed within the total duration:</p> |
| 61 | * |
| 62 | * {@sample development/samples/ApiDemos/res/anim/value_animator_pvh_kf.xml |
| 63 | * ValueAnimatorKeyframeResources} |
| 64 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 65 | * <div class="special reference"> |
| 66 | * <h3>Developer Guides</h3> |
| 67 | * <p>For more information about animating with {@code ValueAnimator}, read the |
| 68 | * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property |
| 69 | * Animation</a> developer guide.</p> |
| 70 | * </div> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 71 | */ |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 72 | @SuppressWarnings("unchecked") |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 73 | public class ValueAnimator extends Animator implements AnimationHandler.AnimationFrameCallback { |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 74 | private static final String TAG = "ValueAnimator"; |
| 75 | private static final boolean DEBUG = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Internal constants |
| 79 | */ |
Daniel Santiago Rivera | 61fe23a | 2019-02-05 12:41:39 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * System-wide animation scale. |
| 83 | * |
| 84 | * <p>To check whether animations are enabled system-wise use {@link #areAnimatorsEnabled()}. |
| 85 | */ |
| 86 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Chet Haase | d21a9fe | 2012-02-01 14:14:17 -0800 | [diff] [blame] | 87 | private static float sDurationScale = 1.0f; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 88 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 89 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 90 | * Internal variables |
| 91 | * NOTE: This object implements the clone() method, making a deep copy of any referenced |
| 92 | * objects. As other non-trivial fields are added to this class, make sure to add logic |
| 93 | * to clone() to make deep copies of them. |
| 94 | */ |
| 95 | |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 96 | /** |
| 97 | * The first time that the animation's animateFrame() method is called. This time is used to |
| 98 | * determine elapsed time (and therefore the elapsed fraction) in subsequent calls |
| 99 | * to animateFrame(). |
| 100 | * |
| 101 | * Whenever mStartTime is set, you must also update mStartTimeCommitted. |
| 102 | */ |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 103 | long mStartTime = -1; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 106 | * When true, the start time has been firmly committed as a chosen reference point in |
| 107 | * time by which the progress of the animation will be evaluated. When false, the |
| 108 | * start time may be updated when the first animation frame is committed so as |
| 109 | * to compensate for jank that may have occurred between when the start time was |
| 110 | * initialized and when the frame was actually drawn. |
| 111 | * |
| 112 | * This flag is generally set to false during the first frame of the animation |
| 113 | * when the animation playing state transitions from STOPPED to RUNNING or |
| 114 | * resumes after having been paused. This flag is set to true when the start time |
| 115 | * is firmly committed and should not be further compensated for jank. |
| 116 | */ |
| 117 | boolean mStartTimeCommitted; |
| 118 | |
| 119 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 120 | * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked |
| 121 | * to a value. |
| 122 | */ |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 123 | float mSeekFraction = -1; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 124 | |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Set on the next frame after pause() is called, used to calculate a new startTime |
| 127 | * or delayStartTime which allows the animator to continue from the point at which |
| 128 | * it was paused. If negative, has not yet been set. |
| 129 | */ |
| 130 | private long mPauseTime; |
| 131 | |
| 132 | /** |
| 133 | * Set when an animator is resumed. This triggers logic in the next frame which |
| 134 | * actually resumes the animator. |
| 135 | */ |
| 136 | private boolean mResumed = false; |
| 137 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 138 | // The time interpolator to be used if none is set on the animation |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 139 | private static final TimeInterpolator sDefaultInterpolator = |
| 140 | new AccelerateDecelerateInterpolator(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 141 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 142 | /** |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 143 | * Flag to indicate whether this animator is playing in reverse mode, specifically |
| 144 | * by being started or interrupted by a call to reverse(). This flag is different than |
| 145 | * mPlayingBackwards, which indicates merely whether the current iteration of the |
| 146 | * animator is playing in reverse. It is used in corner cases to determine proper end |
| 147 | * behavior. |
| 148 | */ |
| 149 | private boolean mReversing; |
| 150 | |
| 151 | /** |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 152 | * Tracks the overall fraction of the animation, ranging from 0 to mRepeatCount + 1 |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 153 | */ |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 154 | private float mOverallFraction = 0f; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 157 | * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction(). |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 158 | * This is calculated by interpolating the fraction (range: [0, 1]) in the current iteration. |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 159 | */ |
| 160 | private float mCurrentFraction = 0f; |
| 161 | |
| 162 | /** |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 163 | * Tracks the time (in milliseconds) when the last frame arrived. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 164 | */ |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 165 | private long mLastFrameTime = -1; |
| 166 | |
| 167 | /** |
| 168 | * Tracks the time (in milliseconds) when the first frame arrived. Note the frame may arrive |
| 169 | * during the start delay. |
| 170 | */ |
| 171 | private long mFirstFrameTime = -1; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 174 | * Additional playing state to indicate whether an animator has been start()'d. There is |
| 175 | * some lag between a call to start() and the first animation frame. We should still note |
| 176 | * that the animation has been started, even if it's first animation frame has not yet |
| 177 | * happened, and reflect that state in isRunning(). |
| 178 | * Note that delayed animations are different: they are not started until their first |
| 179 | * animation frame, which occurs after their delay elapses. |
| 180 | */ |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 181 | private boolean mRunning = false; |
| 182 | |
| 183 | /** |
| 184 | * Additional playing state to indicate whether an animator has been start()'d, whether or |
| 185 | * not there is a nonzero startDelay. |
| 186 | */ |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 187 | private boolean mStarted = false; |
| 188 | |
| 189 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 190 | * Tracks whether we've notified listeners of the onAnimationStart() event. This can be |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 191 | * complex to keep track of since we notify listeners at different times depending on |
| 192 | * startDelay and whether start() was called before end(). |
| 193 | */ |
| 194 | private boolean mStartListenersCalled = false; |
| 195 | |
| 196 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 197 | * Flag that denotes whether the animation is set up and ready to go. Used to |
| 198 | * set up animation that has not yet been started. |
| 199 | */ |
| 200 | boolean mInitialized = false; |
| 201 | |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Flag that tracks whether animation has been requested to end. |
| 204 | */ |
| 205 | private boolean mAnimationEndRequested = false; |
| 206 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 207 | // |
| 208 | // Backing variables |
| 209 | // |
| 210 | |
| 211 | // How long the animation should last in ms |
Mathew Inwood | a228c37 | 2018-08-01 14:35:45 +0100 | [diff] [blame] | 212 | @UnsupportedAppUsage |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 213 | private long mDuration = 300; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 214 | |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 215 | // The amount of time in ms to delay starting the animation after start() is called. Note |
| 216 | // that this start delay is unscaled. When there is a duration scale set on the animator, the |
| 217 | // scaling factor will be applied to this delay. |
| 218 | private long mStartDelay = 0; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 219 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 220 | // The number of times the animation will repeat. The default is 0, which means the animation |
| 221 | // will play only once |
| 222 | private int mRepeatCount = 0; |
| 223 | |
| 224 | /** |
| 225 | * The type of repetition that will occur when repeatMode is nonzero. RESTART means the |
| 226 | * animation will start from the beginning on every new cycle. REVERSE means the animation |
| 227 | * will reverse directions on each iteration. |
| 228 | */ |
| 229 | private int mRepeatMode = RESTART; |
| 230 | |
| 231 | /** |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 232 | * Whether or not the animator should register for its own animation callback to receive |
| 233 | * animation pulse. |
| 234 | */ |
| 235 | private boolean mSelfPulse = true; |
| 236 | |
| 237 | /** |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 238 | * Whether or not the animator has been requested to start without pulsing. This flag gets set |
| 239 | * in startWithoutPulsing(), and reset in start(). |
| 240 | */ |
| 241 | private boolean mSuppressSelfPulseRequested = false; |
| 242 | |
| 243 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 244 | * The time interpolator to be used. The elapsed fraction of the animation will be passed |
| 245 | * through this interpolator to calculate the interpolated fraction, which is then used to |
| 246 | * calculate the animated values. |
| 247 | */ |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 248 | private TimeInterpolator mInterpolator = sDefaultInterpolator; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * The set of listeners to be sent events through the life of an animation. |
| 252 | */ |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 253 | ArrayList<AnimatorUpdateListener> mUpdateListeners = null; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * The property/value sets being animated. |
| 257 | */ |
| 258 | PropertyValuesHolder[] mValues; |
| 259 | |
| 260 | /** |
| 261 | * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values |
| 262 | * by property name during calls to getAnimatedValue(String). |
| 263 | */ |
| 264 | HashMap<String, PropertyValuesHolder> mValuesMap; |
| 265 | |
| 266 | /** |
Jorim Jaggi | c2333b7 | 2017-11-13 15:47:46 +0100 | [diff] [blame] | 267 | * If set to non-negative value, this will override {@link #sDurationScale}. |
| 268 | */ |
| 269 | private float mDurationScale = -1f; |
| 270 | |
| 271 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 272 | * Public constants |
| 273 | */ |
| 274 | |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame] | 275 | /** @hide */ |
| 276 | @IntDef({RESTART, REVERSE}) |
| 277 | @Retention(RetentionPolicy.SOURCE) |
| 278 | public @interface RepeatMode {} |
| 279 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 280 | /** |
| 281 | * When the animation reaches the end and <code>repeatCount</code> is INFINITE |
| 282 | * or a positive value, the animation restarts from the beginning. |
| 283 | */ |
| 284 | public static final int RESTART = 1; |
| 285 | /** |
| 286 | * When the animation reaches the end and <code>repeatCount</code> is INFINITE |
| 287 | * or a positive value, the animation reverses direction on every iteration. |
| 288 | */ |
| 289 | public static final int REVERSE = 2; |
| 290 | /** |
| 291 | * This value used used with the {@link #setRepeatCount(int)} property to repeat |
| 292 | * the animation indefinitely. |
| 293 | */ |
| 294 | public static final int INFINITE = -1; |
| 295 | |
Chet Haase | c38fa1f | 2012-02-01 16:37:46 -0800 | [diff] [blame] | 296 | /** |
| 297 | * @hide |
| 298 | */ |
Artur Satayev | 5a52585 | 2019-10-31 15:15:50 +0000 | [diff] [blame^] | 299 | @UnsupportedAppUsage |
Teng-Hui Zhu | 0a815bb | 2016-08-22 15:48:41 -0700 | [diff] [blame] | 300 | @TestApi |
Chet Haase | c38fa1f | 2012-02-01 16:37:46 -0800 | [diff] [blame] | 301 | public static void setDurationScale(float durationScale) { |
| 302 | sDurationScale = durationScale; |
| 303 | } |
| 304 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 305 | /** |
Jeff Brown | ff7e6ef | 2012-08-15 02:05:18 -0700 | [diff] [blame] | 306 | * @hide |
| 307 | */ |
Artur Satayev | 5a52585 | 2019-10-31 15:15:50 +0000 | [diff] [blame^] | 308 | @UnsupportedAppUsage |
Teng-Hui Zhu | 0a815bb | 2016-08-22 15:48:41 -0700 | [diff] [blame] | 309 | @TestApi |
Jeff Brown | ff7e6ef | 2012-08-15 02:05:18 -0700 | [diff] [blame] | 310 | public static float getDurationScale() { |
| 311 | return sDurationScale; |
| 312 | } |
| 313 | |
| 314 | /** |
Chet Haase | 49a513b | 2016-10-08 09:53:23 -0700 | [diff] [blame] | 315 | * Returns whether animators are currently enabled, system-wide. By default, all |
| 316 | * animators are enabled. This can change if either the user sets a Developer Option |
| 317 | * to set the animator duration scale to 0 or by Battery Savery mode being enabled |
| 318 | * (which disables all animations). |
| 319 | * |
| 320 | * <p>Developers should not typically need to call this method, but should an app wish |
| 321 | * to show a different experience when animators are disabled, this return value |
| 322 | * can be used as a decider of which experience to offer. |
| 323 | * |
| 324 | * @return boolean Whether animators are currently enabled. The default value is |
| 325 | * <code>true</code>. |
| 326 | */ |
| 327 | public static boolean areAnimatorsEnabled() { |
| 328 | return !(sDurationScale == 0); |
| 329 | } |
| 330 | |
| 331 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 332 | * Creates a new ValueAnimator object. This default constructor is primarily for |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 333 | * use internally; the factory methods which take parameters are more generally |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 334 | * useful. |
| 335 | */ |
| 336 | public ValueAnimator() { |
| 337 | } |
| 338 | |
| 339 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 340 | * Constructs and returns a ValueAnimator that animates between int values. A single |
| 341 | * value implies that that value is the one being animated to. However, this is not typically |
| 342 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 343 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 344 | * from the target object and property being animated). Therefore, there should typically |
| 345 | * be two or more values. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 346 | * |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 347 | * @param values A set of values that the animation will animate between over time. |
| 348 | * @return A ValueAnimator object that is set up to animate between the given values. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 349 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 350 | public static ValueAnimator ofInt(int... values) { |
| 351 | ValueAnimator anim = new ValueAnimator(); |
| 352 | anim.setIntValues(values); |
| 353 | return anim; |
| 354 | } |
| 355 | |
| 356 | /** |
George Mount | 1ffb280 | 2013-10-09 16:13:54 -0700 | [diff] [blame] | 357 | * Constructs and returns a ValueAnimator that animates between color values. A single |
| 358 | * value implies that that value is the one being animated to. However, this is not typically |
| 359 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 360 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 361 | * from the target object and property being animated). Therefore, there should typically |
| 362 | * be two or more values. |
| 363 | * |
| 364 | * @param values A set of values that the animation will animate between over time. |
| 365 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 366 | */ |
| 367 | public static ValueAnimator ofArgb(int... values) { |
| 368 | ValueAnimator anim = new ValueAnimator(); |
| 369 | anim.setIntValues(values); |
| 370 | anim.setEvaluator(ArgbEvaluator.getInstance()); |
| 371 | return anim; |
| 372 | } |
| 373 | |
| 374 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 375 | * Constructs and returns a ValueAnimator that animates between float values. A single |
| 376 | * value implies that that value is the one being animated to. However, this is not typically |
| 377 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 378 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 379 | * from the target object and property being animated). Therefore, there should typically |
| 380 | * be two or more values. |
| 381 | * |
| 382 | * @param values A set of values that the animation will animate between over time. |
| 383 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 384 | */ |
| 385 | public static ValueAnimator ofFloat(float... values) { |
| 386 | ValueAnimator anim = new ValueAnimator(); |
| 387 | anim.setFloatValues(values); |
| 388 | return anim; |
| 389 | } |
| 390 | |
| 391 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 392 | * Constructs and returns a ValueAnimator that animates between the values |
| 393 | * specified in the PropertyValuesHolder objects. |
| 394 | * |
| 395 | * @param values A set of PropertyValuesHolder objects whose values will be animated |
| 396 | * between over time. |
| 397 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 398 | */ |
| 399 | public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) { |
| 400 | ValueAnimator anim = new ValueAnimator(); |
| 401 | anim.setValues(values); |
| 402 | return anim; |
| 403 | } |
| 404 | /** |
| 405 | * Constructs and returns a ValueAnimator that animates between Object values. A single |
| 406 | * value implies that that value is the one being animated to. However, this is not typically |
| 407 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 408 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 409 | * from the target object and property being animated). Therefore, there should typically |
| 410 | * be two or more values. |
| 411 | * |
Chet Haase | fa21bdf | 2016-04-21 17:41:54 -0700 | [diff] [blame] | 412 | * <p><strong>Note:</strong> The Object values are stored as references to the original |
| 413 | * objects, which means that changes to those objects after this method is called will |
| 414 | * affect the values on the animator. If the objects will be mutated externally after |
| 415 | * this method is called, callers should pass a copy of those objects instead. |
| 416 | * |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 417 | * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this |
| 418 | * factory method also takes a TypeEvaluator object that the ValueAnimator will use |
| 419 | * to perform that interpolation. |
| 420 | * |
| 421 | * @param evaluator A TypeEvaluator that will be called on each animation frame to |
| 422 | * provide the ncessry interpolation between the Object values to derive the animated |
| 423 | * value. |
| 424 | * @param values A set of values that the animation will animate between over time. |
| 425 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 426 | */ |
| 427 | public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) { |
| 428 | ValueAnimator anim = new ValueAnimator(); |
| 429 | anim.setObjectValues(values); |
| 430 | anim.setEvaluator(evaluator); |
| 431 | return anim; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Sets int values that will be animated between. A single |
| 436 | * value implies that that value is the one being animated to. However, this is not typically |
| 437 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 438 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 439 | * from the target object and property being animated). Therefore, there should typically |
| 440 | * be two or more values. |
| 441 | * |
| 442 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 443 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 444 | * of those objects.</p> |
| 445 | * |
| 446 | * @param values A set of values that the animation will animate between over time. |
| 447 | */ |
| 448 | public void setIntValues(int... values) { |
| 449 | if (values == null || values.length == 0) { |
| 450 | return; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 451 | } |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 452 | if (mValues == null || mValues.length == 0) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 453 | setValues(PropertyValuesHolder.ofInt("", values)); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 454 | } else { |
| 455 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 456 | valuesHolder.setIntValues(values); |
| 457 | } |
| 458 | // New property/values/target should cause re-initialization prior to starting |
| 459 | mInitialized = false; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Sets float values that will be animated between. A single |
| 464 | * value implies that that value is the one being animated to. However, this is not typically |
| 465 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 466 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 467 | * from the target object and property being animated). Therefore, there should typically |
| 468 | * be two or more values. |
| 469 | * |
| 470 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 471 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 472 | * of those objects.</p> |
| 473 | * |
| 474 | * @param values A set of values that the animation will animate between over time. |
| 475 | */ |
| 476 | public void setFloatValues(float... values) { |
| 477 | if (values == null || values.length == 0) { |
| 478 | return; |
| 479 | } |
| 480 | if (mValues == null || mValues.length == 0) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 481 | setValues(PropertyValuesHolder.ofFloat("", values)); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 482 | } else { |
| 483 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 484 | valuesHolder.setFloatValues(values); |
| 485 | } |
| 486 | // New property/values/target should cause re-initialization prior to starting |
| 487 | mInitialized = false; |
| 488 | } |
| 489 | |
| 490 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 491 | * Sets the values to animate between for this animation. A single |
| 492 | * value implies that that value is the one being animated to. However, this is not typically |
| 493 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 494 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 495 | * from the target object and property being animated). Therefore, there should typically |
| 496 | * be two or more values. |
| 497 | * |
Chet Haase | fa21bdf | 2016-04-21 17:41:54 -0700 | [diff] [blame] | 498 | * <p><strong>Note:</strong> The Object values are stored as references to the original |
| 499 | * objects, which means that changes to those objects after this method is called will |
| 500 | * affect the values on the animator. If the objects will be mutated externally after |
| 501 | * this method is called, callers should pass a copy of those objects instead. |
| 502 | * |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 503 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 504 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 505 | * of those objects.</p> |
| 506 | * |
| 507 | * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate |
| 508 | * between these value objects. ValueAnimator only knows how to interpolate between the |
| 509 | * primitive types specified in the other setValues() methods.</p> |
| 510 | * |
| 511 | * @param values The set of values to animate between. |
| 512 | */ |
| 513 | public void setObjectValues(Object... values) { |
| 514 | if (values == null || values.length == 0) { |
| 515 | return; |
| 516 | } |
| 517 | if (mValues == null || mValues.length == 0) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 518 | setValues(PropertyValuesHolder.ofObject("", null, values)); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 519 | } else { |
| 520 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 521 | valuesHolder.setObjectValues(values); |
| 522 | } |
| 523 | // New property/values/target should cause re-initialization prior to starting |
| 524 | mInitialized = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Sets the values, per property, being animated between. This function is called internally |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 529 | * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 530 | * be constructed without values and this method can be called to set the values manually |
| 531 | * instead. |
| 532 | * |
| 533 | * @param values The set of values, per property, being animated between. |
| 534 | */ |
| 535 | public void setValues(PropertyValuesHolder... values) { |
| 536 | int numValues = values.length; |
| 537 | mValues = values; |
| 538 | mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); |
| 539 | for (int i = 0; i < numValues; ++i) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 540 | PropertyValuesHolder valuesHolder = values[i]; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 541 | mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder); |
| 542 | } |
Chet Haase | 0e0590b | 2010-09-26 11:57:28 -0700 | [diff] [blame] | 543 | // New property/values/target should cause re-initialization prior to starting |
| 544 | mInitialized = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Returns the values that this ValueAnimator animates between. These values are stored in |
| 549 | * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list |
| 550 | * of value objects instead. |
| 551 | * |
| 552 | * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the |
| 553 | * values, per property, that define the animation. |
| 554 | */ |
| 555 | public PropertyValuesHolder[] getValues() { |
| 556 | return mValues; |
| 557 | } |
| 558 | |
| 559 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 560 | * This function is called immediately before processing the first animation |
| 561 | * frame of an animation. If there is a nonzero <code>startDelay</code>, the |
| 562 | * function is called after that delay ends. |
| 563 | * It takes care of the final initialization steps for the |
| 564 | * animation. |
| 565 | * |
| 566 | * <p>Overrides of this method should call the superclass method to ensure |
| 567 | * that internal mechanisms for the animation are set up correctly.</p> |
| 568 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 569 | @CallSuper |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 570 | void initAnimation() { |
| 571 | if (!mInitialized) { |
| 572 | int numValues = mValues.length; |
| 573 | for (int i = 0; i < numValues; ++i) { |
| 574 | mValues[i].init(); |
| 575 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 576 | mInitialized = true; |
| 577 | } |
| 578 | } |
| 579 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 580 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 581 | * Sets the length of the animation. The default duration is 300 milliseconds. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 582 | * |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 583 | * @param duration The length of the animation, in milliseconds. This value cannot |
| 584 | * be negative. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 585 | * @return ValueAnimator The object called with setDuration(). This return |
| 586 | * value makes it easier to compose statements together that construct and then set the |
| 587 | * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 588 | */ |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 589 | @Override |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 590 | public ValueAnimator setDuration(long duration) { |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 591 | if (duration < 0) { |
| 592 | throw new IllegalArgumentException("Animators cannot have negative duration: " + |
| 593 | duration); |
| 594 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 595 | mDuration = duration; |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 596 | return this; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Jorim Jaggi | c2333b7 | 2017-11-13 15:47:46 +0100 | [diff] [blame] | 599 | /** |
| 600 | * Overrides the global duration scale by a custom value. |
| 601 | * |
| 602 | * @param durationScale The duration scale to set; or {@code -1f} to use the global duration |
| 603 | * scale. |
| 604 | * @hide |
| 605 | */ |
| 606 | public void overrideDurationScale(float durationScale) { |
| 607 | mDurationScale = durationScale; |
| 608 | } |
| 609 | |
| 610 | private float resolveDurationScale() { |
| 611 | return mDurationScale >= 0f ? mDurationScale : sDurationScale; |
| 612 | } |
| 613 | |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 614 | private long getScaledDuration() { |
Jorim Jaggi | c2333b7 | 2017-11-13 15:47:46 +0100 | [diff] [blame] | 615 | return (long)(mDuration * resolveDurationScale()); |
Jeff Brown | 7a08fe0 | 2014-10-07 15:55:35 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 618 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 619 | * Gets the length of the animation. The default duration is 300 milliseconds. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 620 | * |
| 621 | * @return The length of the animation, in milliseconds. |
| 622 | */ |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 623 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 624 | public long getDuration() { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 625 | return mDuration; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 628 | @Override |
| 629 | public long getTotalDuration() { |
| 630 | if (mRepeatCount == INFINITE) { |
| 631 | return DURATION_INFINITE; |
| 632 | } else { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 633 | return mStartDelay + (mDuration * (mRepeatCount + 1)); |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 638 | * Sets the position of the animation to the specified point in time. This time should |
| 639 | * be between 0 and the total duration of the animation, including any repetition. If |
| 640 | * the animation has not yet been started, then it will not advance forward after it is |
| 641 | * set to this time; it will simply set the time to this value and perform any appropriate |
| 642 | * actions based on that time. If the animation is already running, then setCurrentPlayTime() |
| 643 | * will set the current playing time to this value and continue playing from that point. |
| 644 | * |
| 645 | * @param playTime The time, in milliseconds, to which the animation is advanced or rewound. |
| 646 | */ |
| 647 | public void setCurrentPlayTime(long playTime) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 648 | float fraction = mDuration > 0 ? (float) playTime / mDuration : 1; |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 649 | setCurrentFraction(fraction); |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Sets the position of the animation to the specified fraction. This fraction should |
| 654 | * be between 0 and the total fraction of the animation, including any repetition. That is, |
| 655 | * a fraction of 0 will position the animation at the beginning, a value of 1 at the end, |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 656 | * and a value of 2 at the end of a reversing animator that repeats once. If |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 657 | * the animation has not yet been started, then it will not advance forward after it is |
| 658 | * set to this fraction; it will simply set the fraction to this value and perform any |
| 659 | * appropriate actions based on that fraction. If the animation is already running, then |
| 660 | * setCurrentFraction() will set the current fraction to this value and continue |
Eino-Ville Talvala | 5a5afe0 | 2014-12-05 11:11:45 -0800 | [diff] [blame] | 661 | * playing from that point. {@link Animator.AnimatorListener} events are not called |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 662 | * due to changing the fraction; those events are only processed while the animation |
| 663 | * is running. |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 664 | * |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 665 | * @param fraction The fraction to which the animation is advanced or rewound. Values |
| 666 | * outside the range of 0 to the maximum fraction for the animator will be clamped to |
| 667 | * the correct range. |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 668 | */ |
| 669 | public void setCurrentFraction(float fraction) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 670 | initAnimation(); |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 671 | fraction = clampFraction(fraction); |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 672 | mStartTimeCommitted = true; // do not allow start time to be compensated for jank |
Doris Liu | 3b46687 | 2017-02-03 16:49:20 -0800 | [diff] [blame] | 673 | if (isPulsingInternal()) { |
| 674 | long seekTime = (long) (getScaledDuration() * fraction); |
| 675 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 676 | // Only modify the start time when the animation is running. Seek fraction will ensure |
| 677 | // non-running animations skip to the correct start time. |
| 678 | mStartTime = currentTime - seekTime; |
| 679 | } else { |
| 680 | // If the animation loop hasn't started, or during start delay, the startTime will be |
| 681 | // adjusted once the delay has passed based on seek fraction. |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 682 | mSeekFraction = fraction; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 683 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 684 | mOverallFraction = fraction; |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 685 | final float currentIterationFraction = getCurrentIterationFraction(fraction, mReversing); |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 686 | animateValue(currentIterationFraction); |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Calculates current iteration based on the overall fraction. The overall fraction will be |
| 691 | * in the range of [0, mRepeatCount + 1]. Both current iteration and fraction in the current |
| 692 | * iteration can be derived from it. |
| 693 | */ |
| 694 | private int getCurrentIteration(float fraction) { |
| 695 | fraction = clampFraction(fraction); |
| 696 | // If the overall fraction is a positive integer, we consider the current iteration to be |
| 697 | // complete. In other words, the fraction for the current iteration would be 1, and the |
| 698 | // current iteration would be overall fraction - 1. |
| 699 | double iteration = Math.floor(fraction); |
| 700 | if (fraction == iteration && fraction > 0) { |
| 701 | iteration--; |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 702 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 703 | return (int) iteration; |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * Calculates the fraction of the current iteration, taking into account whether the animation |
| 708 | * should be played backwards. E.g. When the animation is played backwards in an iteration, |
| 709 | * the fraction for that iteration will go from 1f to 0f. |
| 710 | */ |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 711 | private float getCurrentIterationFraction(float fraction, boolean inReverse) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 712 | fraction = clampFraction(fraction); |
| 713 | int iteration = getCurrentIteration(fraction); |
| 714 | float currentFraction = fraction - iteration; |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 715 | return shouldPlayBackward(iteration, inReverse) ? 1f - currentFraction : currentFraction; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Clamps fraction into the correct range: [0, mRepeatCount + 1]. If repeat count is infinite, |
| 720 | * no upper bound will be set for the fraction. |
| 721 | * |
| 722 | * @param fraction fraction to be clamped |
| 723 | * @return fraction clamped into the range of [0, mRepeatCount + 1] |
| 724 | */ |
| 725 | private float clampFraction(float fraction) { |
| 726 | if (fraction < 0) { |
| 727 | fraction = 0; |
| 728 | } else if (mRepeatCount != INFINITE) { |
| 729 | fraction = Math.min(fraction, mRepeatCount + 1); |
| 730 | } |
| 731 | return fraction; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Calculates the direction of animation playing (i.e. forward or backward), based on 1) |
| 736 | * whether the entire animation is being reversed, 2) repeat mode applied to the current |
| 737 | * iteration. |
| 738 | */ |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 739 | private boolean shouldPlayBackward(int iteration, boolean inReverse) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 740 | if (iteration > 0 && mRepeatMode == REVERSE && |
| 741 | (iteration < (mRepeatCount + 1) || mRepeatCount == INFINITE)) { |
| 742 | // if we were seeked to some other iteration in a reversing animator, |
| 743 | // figure out the correct direction to start playing based on the iteration |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 744 | if (inReverse) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 745 | return (iteration % 2) == 0; |
| 746 | } else { |
| 747 | return (iteration % 2) != 0; |
| 748 | } |
| 749 | } else { |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 750 | return inReverse; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 751 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Gets the current position of the animation in time, which is equal to the current |
| 756 | * time minus the time that the animation started. An animation that is not yet started will |
Chet Haase | 4365e5a | 2015-10-06 19:32:57 -0700 | [diff] [blame] | 757 | * return a value of zero, unless the animation has has its play time set via |
| 758 | * {@link #setCurrentPlayTime(long)} or {@link #setCurrentFraction(float)}, in which case |
| 759 | * it will return the time that was set. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 760 | * |
| 761 | * @return The current position in time of the animation. |
| 762 | */ |
| 763 | public long getCurrentPlayTime() { |
Chet Haase | 4365e5a | 2015-10-06 19:32:57 -0700 | [diff] [blame] | 764 | if (!mInitialized || (!mStarted && mSeekFraction < 0)) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 765 | return 0; |
| 766 | } |
Chet Haase | 4365e5a | 2015-10-06 19:32:57 -0700 | [diff] [blame] | 767 | if (mSeekFraction >= 0) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 768 | return (long) (mDuration * mSeekFraction); |
Chet Haase | 4365e5a | 2015-10-06 19:32:57 -0700 | [diff] [blame] | 769 | } |
Jorim Jaggi | c2333b7 | 2017-11-13 15:47:46 +0100 | [diff] [blame] | 770 | float durationScale = resolveDurationScale(); |
| 771 | if (durationScale == 0f) { |
| 772 | durationScale = 1f; |
| 773 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 774 | return (long) ((AnimationUtils.currentAnimationTimeMillis() - mStartTime) / durationScale); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 778 | * The amount of time, in milliseconds, to delay starting the animation after |
| 779 | * {@link #start()} is called. |
| 780 | * |
| 781 | * @return the number of milliseconds to delay running the animation |
| 782 | */ |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 783 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 784 | public long getStartDelay() { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 785 | return mStartDelay; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /** |
| 789 | * The amount of time, in milliseconds, to delay starting the animation after |
Doris Liu | 61045c5 | 2016-05-24 16:38:19 -0700 | [diff] [blame] | 790 | * {@link #start()} is called. Note that the start delay should always be non-negative. Any |
| 791 | * negative start delay will be clamped to 0 on N and above. |
| 792 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 793 | * @param startDelay The amount of the delay, in milliseconds |
| 794 | */ |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 795 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 796 | public void setStartDelay(long startDelay) { |
Doris Liu | 61045c5 | 2016-05-24 16:38:19 -0700 | [diff] [blame] | 797 | // Clamp start delay to non-negative range. |
| 798 | if (startDelay < 0) { |
| 799 | Log.w(TAG, "Start delay should always be non-negative"); |
| 800 | startDelay = 0; |
| 801 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 802 | mStartDelay = startDelay; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /** |
| 806 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 807 | * requested time that the animation will attempt to honor, but the actual delay between |
| 808 | * frames may be different, depending on system load and capabilities. This is a static |
| 809 | * function because the same delay will be applied to all animations, since they are all |
| 810 | * run off of a single timing loop. |
| 811 | * |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 812 | * The frame delay may be ignored when the animation system uses an external timing |
| 813 | * source, such as the display refresh rate (vsync), to govern animations. |
| 814 | * |
Doris Liu | 2b2e2c8 | 2015-10-01 11:08:54 -0700 | [diff] [blame] | 815 | * Note that this method should be called from the same thread that {@link #start()} is |
| 816 | * called in order to check the frame delay for that animation. A runtime exception will be |
| 817 | * thrown if the calling thread does not have a Looper. |
| 818 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 819 | * @return the requested time between frames, in milliseconds |
| 820 | */ |
| 821 | public static long getFrameDelay() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 822 | return AnimationHandler.getInstance().getFrameDelay(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | /** |
| 826 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 827 | * requested time that the animation will attempt to honor, but the actual delay between |
| 828 | * frames may be different, depending on system load and capabilities. This is a static |
| 829 | * function because the same delay will be applied to all animations, since they are all |
| 830 | * run off of a single timing loop. |
| 831 | * |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 832 | * The frame delay may be ignored when the animation system uses an external timing |
| 833 | * source, such as the display refresh rate (vsync), to govern animations. |
| 834 | * |
Doris Liu | 2b2e2c8 | 2015-10-01 11:08:54 -0700 | [diff] [blame] | 835 | * Note that this method should be called from the same thread that {@link #start()} is |
| 836 | * called in order to have the new frame delay take effect on that animation. A runtime |
| 837 | * exception will be thrown if the calling thread does not have a Looper. |
| 838 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 839 | * @param frameDelay the requested time between frames, in milliseconds |
| 840 | */ |
| 841 | public static void setFrameDelay(long frameDelay) { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 842 | AnimationHandler.getInstance().setFrameDelay(frameDelay); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /** |
| 846 | * The most recent value calculated by this <code>ValueAnimator</code> when there is just one |
| 847 | * property being animated. This value is only sensible while the animation is running. The main |
| 848 | * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code> |
| 849 | * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which |
| 850 | * is called during each animation frame, immediately after the value is calculated. |
| 851 | * |
| 852 | * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for |
| 853 | * the single property being animated. If there are several properties being animated |
| 854 | * (specified by several PropertyValuesHolder objects in the constructor), this function |
| 855 | * returns the animated value for the first of those objects. |
| 856 | */ |
| 857 | public Object getAnimatedValue() { |
| 858 | if (mValues != null && mValues.length > 0) { |
| 859 | return mValues[0].getAnimatedValue(); |
| 860 | } |
| 861 | // Shouldn't get here; should always have values unless ValueAnimator was set up wrong |
| 862 | return null; |
| 863 | } |
| 864 | |
| 865 | /** |
| 866 | * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>. |
| 867 | * The main purpose for this read-only property is to retrieve the value from the |
| 868 | * <code>ValueAnimator</code> during a call to |
| 869 | * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which |
| 870 | * is called during each animation frame, immediately after the value is calculated. |
| 871 | * |
| 872 | * @return animatedValue The value most recently calculated for the named property |
| 873 | * by this <code>ValueAnimator</code>. |
| 874 | */ |
| 875 | public Object getAnimatedValue(String propertyName) { |
| 876 | PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName); |
| 877 | if (valuesHolder != null) { |
| 878 | return valuesHolder.getAnimatedValue(); |
| 879 | } else { |
| 880 | // At least avoid crashing if called with bogus propertyName |
| 881 | return null; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * Sets how many times the animation should be repeated. If the repeat |
| 887 | * count is 0, the animation is never repeated. If the repeat count is |
| 888 | * greater than 0 or {@link #INFINITE}, the repeat mode will be taken |
| 889 | * into account. The repeat count is 0 by default. |
| 890 | * |
| 891 | * @param value the number of times the animation should be repeated |
| 892 | */ |
| 893 | public void setRepeatCount(int value) { |
| 894 | mRepeatCount = value; |
| 895 | } |
| 896 | /** |
| 897 | * Defines how many times the animation should repeat. The default value |
| 898 | * is 0. |
| 899 | * |
| 900 | * @return the number of times the animation should repeat, or {@link #INFINITE} |
| 901 | */ |
| 902 | public int getRepeatCount() { |
| 903 | return mRepeatCount; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Defines what this animation should do when it reaches the end. This |
| 908 | * setting is applied only when the repeat count is either greater than |
| 909 | * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. |
| 910 | * |
| 911 | * @param value {@link #RESTART} or {@link #REVERSE} |
| 912 | */ |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame] | 913 | public void setRepeatMode(@RepeatMode int value) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 914 | mRepeatMode = value; |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * Defines what this animation should do when it reaches the end. |
| 919 | * |
| 920 | * @return either one of {@link #REVERSE} or {@link #RESTART} |
| 921 | */ |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame] | 922 | @RepeatMode |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 923 | public int getRepeatMode() { |
| 924 | return mRepeatMode; |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * Adds a listener to the set of listeners that are sent update events through the life of |
| 929 | * an animation. This method is called on all listeners for every frame of the animation, |
| 930 | * after the values for the animation have been calculated. |
| 931 | * |
| 932 | * @param listener the listener to be added to the current set of listeners for this animation. |
| 933 | */ |
| 934 | public void addUpdateListener(AnimatorUpdateListener listener) { |
| 935 | if (mUpdateListeners == null) { |
| 936 | mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); |
| 937 | } |
| 938 | mUpdateListeners.add(listener); |
| 939 | } |
| 940 | |
| 941 | /** |
Jim Miller | 3060421 | 2010-09-22 19:56:23 -0700 | [diff] [blame] | 942 | * Removes all listeners from the set listening to frame updates for this animation. |
| 943 | */ |
| 944 | public void removeAllUpdateListeners() { |
| 945 | if (mUpdateListeners == null) { |
| 946 | return; |
| 947 | } |
| 948 | mUpdateListeners.clear(); |
| 949 | mUpdateListeners = null; |
| 950 | } |
| 951 | |
| 952 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 953 | * Removes a listener from the set listening to frame updates for this animation. |
| 954 | * |
| 955 | * @param listener the listener to be removed from the current set of update listeners |
| 956 | * for this animation. |
| 957 | */ |
| 958 | public void removeUpdateListener(AnimatorUpdateListener listener) { |
| 959 | if (mUpdateListeners == null) { |
| 960 | return; |
| 961 | } |
| 962 | mUpdateListeners.remove(listener); |
| 963 | if (mUpdateListeners.size() == 0) { |
| 964 | mUpdateListeners = null; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | |
| 969 | /** |
| 970 | * The time interpolator used in calculating the elapsed fraction of this animation. The |
| 971 | * interpolator determines whether the animation runs with linear or non-linear motion, |
| 972 | * such as acceleration and deceleration. The default value is |
| 973 | * {@link android.view.animation.AccelerateDecelerateInterpolator} |
| 974 | * |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 975 | * @param value the interpolator to be used by this animation. A value of <code>null</code> |
| 976 | * will result in linear interpolation. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 977 | */ |
| 978 | @Override |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 979 | public void setInterpolator(TimeInterpolator value) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 980 | if (value != null) { |
| 981 | mInterpolator = value; |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 982 | } else { |
| 983 | mInterpolator = new LinearInterpolator(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * Returns the timing interpolator that this ValueAnimator uses. |
| 989 | * |
| 990 | * @return The timing interpolator for this ValueAnimator. |
| 991 | */ |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 992 | @Override |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 993 | public TimeInterpolator getInterpolator() { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 994 | return mInterpolator; |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * The type evaluator to be used when calculating the animated values of this animation. |
Chet Haase | b2ab04f | 2011-01-16 11:03:22 -0800 | [diff] [blame] | 999 | * The system will automatically assign a float or int evaluator based on the type |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1000 | * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values |
| 1001 | * are not one of these primitive types, or if different evaluation is desired (such as is |
| 1002 | * necessary with int values that represent colors), a custom evaluator needs to be assigned. |
Chet Haase | 53ee331 | 2011-01-10 15:56:56 -0800 | [diff] [blame] | 1003 | * For example, when running an animation on color values, the {@link ArgbEvaluator} |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1004 | * should be used to get correct RGB color interpolation. |
| 1005 | * |
| 1006 | * <p>If this ValueAnimator has only one set of values being animated between, this evaluator |
| 1007 | * will be used for that set. If there are several sets of values being animated, which is |
Chet Haase | fdd3ad7 | 2013-04-24 16:38:20 -0700 | [diff] [blame] | 1008 | * the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1009 | * is assigned just to the first PropertyValuesHolder object.</p> |
| 1010 | * |
| 1011 | * @param value the evaluator to be used this animation |
| 1012 | */ |
| 1013 | public void setEvaluator(TypeEvaluator value) { |
| 1014 | if (value != null && mValues != null && mValues.length > 0) { |
| 1015 | mValues[0].setEvaluator(value); |
| 1016 | } |
| 1017 | } |
| 1018 | |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1019 | private void notifyStartListeners() { |
| 1020 | if (mListeners != null && !mStartListenersCalled) { |
| 1021 | ArrayList<AnimatorListener> tmpListeners = |
| 1022 | (ArrayList<AnimatorListener>) mListeners.clone(); |
| 1023 | int numListeners = tmpListeners.size(); |
| 1024 | for (int i = 0; i < numListeners; ++i) { |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1025 | tmpListeners.get(i).onAnimationStart(this, mReversing); |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | mStartListenersCalled = true; |
| 1029 | } |
| 1030 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1031 | /** |
| 1032 | * Start the animation playing. This version of start() takes a boolean flag that indicates |
| 1033 | * whether the animation should play in reverse. The flag is usually false, but may be set |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1034 | * to true if called from the reverse() method. |
| 1035 | * |
| 1036 | * <p>The animation started by calling this method will be run on the thread that called |
| 1037 | * this method. This thread should have a Looper on it (a runtime exception will be thrown if |
| 1038 | * this is not the case). Also, if the animation will animate |
| 1039 | * properties of objects in the view hierarchy, then the calling thread should be the UI |
| 1040 | * thread for that view hierarchy.</p> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1041 | * |
| 1042 | * @param playBackwards Whether the ValueAnimator should start playing in reverse. |
| 1043 | */ |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 1044 | private void start(boolean playBackwards) { |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1045 | if (Looper.myLooper() == null) { |
| 1046 | throw new AndroidRuntimeException("Animators may only be run on Looper threads"); |
Jim Miller | 3060421 | 2010-09-22 19:56:23 -0700 | [diff] [blame] | 1047 | } |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 1048 | mReversing = playBackwards; |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 1049 | mSelfPulse = !mSuppressSelfPulseRequested; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1050 | // Special case: reversing from seek-to-0 should act as if not seeked at all. |
| 1051 | if (playBackwards && mSeekFraction != -1 && mSeekFraction != 0) { |
| 1052 | if (mRepeatCount == INFINITE) { |
| 1053 | // Calculate the fraction of the current iteration. |
| 1054 | float fraction = (float) (mSeekFraction - Math.floor(mSeekFraction)); |
| 1055 | mSeekFraction = 1 - fraction; |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 1056 | } else { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1057 | mSeekFraction = 1 + mRepeatCount - mSeekFraction; |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 1058 | } |
| 1059 | } |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 1060 | mStarted = true; |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1061 | mPaused = false; |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1062 | mRunning = false; |
Justin Klaassen | 543a7ed | 2016-07-22 19:30:03 -0700 | [diff] [blame] | 1063 | mAnimationEndRequested = false; |
Doris Liu | caa2ebf | 2016-06-10 14:46:43 -0700 | [diff] [blame] | 1064 | // Resets mLastFrameTime when start() is called, so that if the animation was running, |
| 1065 | // calling start() would put the animation in the |
| 1066 | // started-but-not-yet-reached-the-first-frame phase. |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1067 | mLastFrameTime = -1; |
| 1068 | mFirstFrameTime = -1; |
Doris Liu | 6d45209 | 2017-02-08 14:47:08 -0800 | [diff] [blame] | 1069 | mStartTime = -1; |
Doris Liu | 3b46687 | 2017-02-03 16:49:20 -0800 | [diff] [blame] | 1070 | addAnimationCallback(0); |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 1071 | |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1072 | if (mStartDelay == 0 || mSeekFraction >= 0 || mReversing) { |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 1073 | // If there's no start delay, init the animation and notify start listeners right away |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1074 | // to be consistent with the previous behavior. Otherwise, postpone this until the first |
| 1075 | // frame after the start delay. |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 1076 | startAnimation(); |
Doris Liu | b199da7 | 2016-02-25 14:23:09 -0800 | [diff] [blame] | 1077 | if (mSeekFraction == -1) { |
| 1078 | // No seek, start at play time 0. Note that the reason we are not using fraction 0 |
| 1079 | // is because for animations with 0 duration, we want to be consistent with pre-N |
| 1080 | // behavior: skip to the final value immediately. |
| 1081 | setCurrentPlayTime(0); |
| 1082 | } else { |
| 1083 | setCurrentFraction(mSeekFraction); |
| 1084 | } |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 1085 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1088 | void startWithoutPulsing(boolean inReverse) { |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 1089 | mSuppressSelfPulseRequested = true; |
| 1090 | if (inReverse) { |
| 1091 | reverse(); |
| 1092 | } else { |
| 1093 | start(); |
| 1094 | } |
| 1095 | mSuppressSelfPulseRequested = false; |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1098 | @Override |
| 1099 | public void start() { |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 1100 | start(false); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | @Override |
| 1104 | public void cancel() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1105 | if (Looper.myLooper() == null) { |
| 1106 | throw new AndroidRuntimeException("Animators may only be run on Looper threads"); |
| 1107 | } |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 1108 | |
| 1109 | // If end has already been requested, through a previous end() or cancel() call, no-op |
| 1110 | // until animation starts again. |
| 1111 | if (mAnimationEndRequested) { |
| 1112 | return; |
| 1113 | } |
| 1114 | |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1115 | // Only cancel if the animation is actually running or has been started and is about |
| 1116 | // to run |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1117 | // Only notify listeners if the animator has actually started |
| 1118 | if ((mStarted || mRunning) && mListeners != null) { |
| 1119 | if (!mRunning) { |
| 1120 | // If it's not yet running, then start listeners weren't called. Call them now. |
| 1121 | notifyStartListeners(); |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 1122 | } |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1123 | ArrayList<AnimatorListener> tmpListeners = |
| 1124 | (ArrayList<AnimatorListener>) mListeners.clone(); |
| 1125 | for (AnimatorListener listener : tmpListeners) { |
| 1126 | listener.onAnimationCancel(this); |
| 1127 | } |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1128 | } |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1129 | endAnimation(); |
| 1130 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | @Override |
| 1134 | public void end() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1135 | if (Looper.myLooper() == null) { |
| 1136 | throw new AndroidRuntimeException("Animators may only be run on Looper threads"); |
| 1137 | } |
| 1138 | if (!mRunning) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1139 | // Special case if the animation has not yet started; get it ready for ending |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1140 | startAnimation(); |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1141 | mStarted = true; |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 1142 | } else if (!mInitialized) { |
| 1143 | initAnimation(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1144 | } |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1145 | animateValue(shouldPlayBackward(mRepeatCount, mReversing) ? 0f : 1f); |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1146 | endAnimation(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | @Override |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1150 | public void resume() { |
George Mount | a06b3f1 | 2016-03-02 08:06:32 -0800 | [diff] [blame] | 1151 | if (Looper.myLooper() == null) { |
| 1152 | throw new AndroidRuntimeException("Animators may only be resumed from the same " + |
| 1153 | "thread that the animator was started on"); |
| 1154 | } |
| 1155 | if (mPaused && !mResumed) { |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1156 | mResumed = true; |
George Mount | a06b3f1 | 2016-03-02 08:06:32 -0800 | [diff] [blame] | 1157 | if (mPauseTime > 0) { |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1158 | addAnimationCallback(0); |
George Mount | a06b3f1 | 2016-03-02 08:06:32 -0800 | [diff] [blame] | 1159 | } |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1160 | } |
| 1161 | super.resume(); |
| 1162 | } |
| 1163 | |
| 1164 | @Override |
| 1165 | public void pause() { |
| 1166 | boolean previouslyPaused = mPaused; |
| 1167 | super.pause(); |
| 1168 | if (!previouslyPaused && mPaused) { |
| 1169 | mPauseTime = -1; |
| 1170 | mResumed = false; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1175 | public boolean isRunning() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1176 | return mRunning; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | @Override |
| 1180 | public boolean isStarted() { |
| 1181 | return mStarted; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * Plays the ValueAnimator in reverse. If the animation is already running, |
| 1186 | * it will stop itself and play backwards from the point reached when reverse was called. |
| 1187 | * If the animation is not currently running, then it will start from the end and |
| 1188 | * play backwards. This behavior is only set for the current animation; future playing |
| 1189 | * of the animation will use the default behavior of playing forward. |
| 1190 | */ |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 1191 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1192 | public void reverse() { |
Doris Liu | caa2ebf | 2016-06-10 14:46:43 -0700 | [diff] [blame] | 1193 | if (isPulsingInternal()) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1194 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 1195 | long currentPlayTime = currentTime - mStartTime; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1196 | long timeLeft = getScaledDuration() - currentPlayTime; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1197 | mStartTime = currentTime - timeLeft; |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1198 | mStartTimeCommitted = true; // do not allow start time to be compensated for jank |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 1199 | mReversing = !mReversing; |
Chet Haase | f43fb2a | 2013-09-06 07:59:36 -0700 | [diff] [blame] | 1200 | } else if (mStarted) { |
Doris Liu | caa2ebf | 2016-06-10 14:46:43 -0700 | [diff] [blame] | 1201 | mReversing = !mReversing; |
Chet Haase | f43fb2a | 2013-09-06 07:59:36 -0700 | [diff] [blame] | 1202 | end(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1203 | } else { |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 1204 | start(true); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | /** |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 1209 | * @hide |
| 1210 | */ |
| 1211 | @Override |
| 1212 | public boolean canReverse() { |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
| 1216 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1217 | * Called internally to end an animation by removing it from the animations list. Must be |
| 1218 | * called on the UI thread. |
| 1219 | */ |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 1220 | private void endAnimation() { |
| 1221 | if (mAnimationEndRequested) { |
| 1222 | return; |
| 1223 | } |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1224 | removeAnimationCallback(); |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 1225 | |
| 1226 | mAnimationEndRequested = true; |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1227 | mPaused = false; |
George Mount | 0eba1df | 2016-06-13 13:53:37 -0700 | [diff] [blame] | 1228 | boolean notify = (mStarted || mRunning) && mListeners != null; |
| 1229 | if (notify && !mRunning) { |
| 1230 | // If it's not yet running, then start listeners weren't called. Call them now. |
| 1231 | notifyStartListeners(); |
| 1232 | } |
| 1233 | mRunning = false; |
| 1234 | mStarted = false; |
| 1235 | mStartListenersCalled = false; |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1236 | mLastFrameTime = -1; |
| 1237 | mFirstFrameTime = -1; |
Doris Liu | 6d45209 | 2017-02-08 14:47:08 -0800 | [diff] [blame] | 1238 | mStartTime = -1; |
George Mount | 0eba1df | 2016-06-13 13:53:37 -0700 | [diff] [blame] | 1239 | if (notify && mListeners != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1240 | ArrayList<AnimatorListener> tmpListeners = |
| 1241 | (ArrayList<AnimatorListener>) mListeners.clone(); |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 1242 | int numListeners = tmpListeners.size(); |
| 1243 | for (int i = 0; i < numListeners; ++i) { |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1244 | tmpListeners.get(i).onAnimationEnd(this, mReversing); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1245 | } |
| 1246 | } |
Doris Liu | 6d45209 | 2017-02-08 14:47:08 -0800 | [diff] [blame] | 1247 | // mReversing needs to be reset *after* notifying the listeners for the end callbacks. |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1248 | mReversing = false; |
Chet Haase | 9b80ca8 | 2013-06-04 09:37:38 -0700 | [diff] [blame] | 1249 | if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { |
| 1250 | Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(), |
| 1251 | System.identityHashCode(this)); |
| 1252 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Called internally to start an animation by adding it to the active animations list. Must be |
| 1257 | * called on the UI thread. |
| 1258 | */ |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1259 | private void startAnimation() { |
Chet Haase | 9b80ca8 | 2013-06-04 09:37:38 -0700 | [diff] [blame] | 1260 | if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { |
| 1261 | Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(), |
| 1262 | System.identityHashCode(this)); |
| 1263 | } |
Doris Liu | 66d4856 | 2015-11-09 14:44:35 -0800 | [diff] [blame] | 1264 | |
| 1265 | mAnimationEndRequested = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1266 | initAnimation(); |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1267 | mRunning = true; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1268 | if (mSeekFraction >= 0) { |
| 1269 | mOverallFraction = mSeekFraction; |
| 1270 | } else { |
| 1271 | mOverallFraction = 0f; |
| 1272 | } |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1273 | if (mListeners != null) { |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1274 | notifyStartListeners(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /** |
Doris Liu | caa2ebf | 2016-06-10 14:46:43 -0700 | [diff] [blame] | 1279 | * Internal only: This tracks whether the animation has gotten on the animation loop. Note |
| 1280 | * this is different than {@link #isRunning()} in that the latter tracks the time after start() |
| 1281 | * is called (or after start delay if any), which may be before the animation loop starts. |
| 1282 | */ |
| 1283 | private boolean isPulsingInternal() { |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1284 | return mLastFrameTime >= 0; |
Doris Liu | caa2ebf | 2016-06-10 14:46:43 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | /** |
Chet Haase | fdd3ad7 | 2013-04-24 16:38:20 -0700 | [diff] [blame] | 1288 | * Returns the name of this animator for debugging purposes. |
| 1289 | */ |
| 1290 | String getNameForTrace() { |
| 1291 | return "animator"; |
| 1292 | } |
| 1293 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1294 | /** |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1295 | * Applies an adjustment to the animation to compensate for jank between when |
| 1296 | * the animation first ran and when the frame was drawn. |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1297 | * @hide |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1298 | */ |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1299 | public void commitAnimationFrame(long frameTime) { |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1300 | if (!mStartTimeCommitted) { |
| 1301 | mStartTimeCommitted = true; |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1302 | long adjustment = frameTime - mLastFrameTime; |
| 1303 | if (adjustment > 0) { |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1304 | mStartTime += adjustment; |
| 1305 | if (DEBUG) { |
| 1306 | Log.d(TAG, "Adjusted start time by " + adjustment + " ms: " + toString()); |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1313 | * This internal function processes a single animation frame for a given animation. The |
| 1314 | * currentTime parameter is the timing pulse sent by the handler, used to calculate the |
| 1315 | * elapsed duration, and therefore |
| 1316 | * the elapsed fraction, of the animation. The return value indicates whether the animation |
| 1317 | * should be ended (which happens when the elapsed time of the animation exceeds the |
| 1318 | * animation's duration, including the repeatCount). |
| 1319 | * |
| 1320 | * @param currentTime The current time, as tracked by the static timing handler |
| 1321 | * @return true if the animation's duration, including any repetitions due to |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1322 | * <code>repeatCount</code> has been exceeded and the animation should be ended. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1323 | */ |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1324 | boolean animateBasedOnTime(long currentTime) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1325 | boolean done = false; |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1326 | if (mRunning) { |
Doris Liu | 56b0b57 | 2016-04-13 14:13:49 -0700 | [diff] [blame] | 1327 | final long scaledDuration = getScaledDuration(); |
| 1328 | final float fraction = scaledDuration > 0 ? |
| 1329 | (float)(currentTime - mStartTime) / scaledDuration : 1f; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1330 | final float lastFraction = mOverallFraction; |
| 1331 | final boolean newIteration = (int) fraction > (int) lastFraction; |
| 1332 | final boolean lastIterationFinished = (fraction >= mRepeatCount + 1) && |
| 1333 | (mRepeatCount != INFINITE); |
Doris Liu | 56b0b57 | 2016-04-13 14:13:49 -0700 | [diff] [blame] | 1334 | if (scaledDuration == 0) { |
| 1335 | // 0 duration animator, ignore the repeat count and skip to the end |
| 1336 | done = true; |
| 1337 | } else if (newIteration && !lastIterationFinished) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1338 | // Time to repeat |
| 1339 | if (mListeners != null) { |
| 1340 | int numListeners = mListeners.size(); |
| 1341 | for (int i = 0; i < numListeners; ++i) { |
| 1342 | mListeners.get(i).onAnimationRepeat(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1343 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1344 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1345 | } else if (lastIterationFinished) { |
| 1346 | done = true; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1347 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1348 | mOverallFraction = clampFraction(fraction); |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1349 | float currentIterationFraction = getCurrentIterationFraction( |
| 1350 | mOverallFraction, mReversing); |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1351 | animateValue(currentIterationFraction); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1352 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1353 | return done; |
| 1354 | } |
| 1355 | |
| 1356 | /** |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1357 | * Internal use only. |
| 1358 | * |
| 1359 | * This method does not modify any fields of the animation. It should be called when seeking |
| 1360 | * in an AnimatorSet. When the last play time and current play time are of different repeat |
| 1361 | * iterations, |
| 1362 | * {@link android.view.animation.Animation.AnimationListener#onAnimationRepeat(Animation)} |
| 1363 | * will be called. |
| 1364 | */ |
| 1365 | @Override |
| 1366 | void animateBasedOnPlayTime(long currentPlayTime, long lastPlayTime, boolean inReverse) { |
| 1367 | if (currentPlayTime < 0 || lastPlayTime < 0) { |
| 1368 | throw new UnsupportedOperationException("Error: Play time should never be negative."); |
| 1369 | } |
| 1370 | |
| 1371 | initAnimation(); |
| 1372 | // Check whether repeat callback is needed only when repeat count is non-zero |
| 1373 | if (mRepeatCount > 0) { |
| 1374 | int iteration = (int) (currentPlayTime / mDuration); |
| 1375 | int lastIteration = (int) (lastPlayTime / mDuration); |
| 1376 | |
| 1377 | // Clamp iteration to [0, mRepeatCount] |
| 1378 | iteration = Math.min(iteration, mRepeatCount); |
| 1379 | lastIteration = Math.min(lastIteration, mRepeatCount); |
| 1380 | |
| 1381 | if (iteration != lastIteration) { |
| 1382 | if (mListeners != null) { |
| 1383 | int numListeners = mListeners.size(); |
| 1384 | for (int i = 0; i < numListeners; ++i) { |
| 1385 | mListeners.get(i).onAnimationRepeat(this); |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | if (mRepeatCount != INFINITE && currentPlayTime >= (mRepeatCount + 1) * mDuration) { |
| 1392 | skipToEndValue(inReverse); |
| 1393 | } else { |
| 1394 | // Find the current fraction: |
| 1395 | float fraction = currentPlayTime / (float) mDuration; |
| 1396 | fraction = getCurrentIterationFraction(fraction, inReverse); |
| 1397 | animateValue(fraction); |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | /** |
| 1402 | * Internal use only. |
| 1403 | * Skips the animation value to end/start, depending on whether the play direction is forward |
| 1404 | * or backward. |
| 1405 | * |
| 1406 | * @param inReverse whether the end value is based on a reverse direction. If yes, this is |
| 1407 | * equivalent to skip to start value in a forward playing direction. |
| 1408 | */ |
| 1409 | void skipToEndValue(boolean inReverse) { |
| 1410 | initAnimation(); |
| 1411 | float endFraction = inReverse ? 0f : 1f; |
| 1412 | if (mRepeatCount % 2 == 1 && mRepeatMode == REVERSE) { |
| 1413 | // This would end on fraction = 0 |
| 1414 | endFraction = 0f; |
| 1415 | } |
| 1416 | animateValue(endFraction); |
| 1417 | } |
| 1418 | |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 1419 | @Override |
| 1420 | boolean isInitialized() { |
| 1421 | return mInitialized; |
| 1422 | } |
| 1423 | |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1424 | /** |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1425 | * Processes a frame of the animation, adjusting the start time if needed. |
| 1426 | * |
| 1427 | * @param frameTime The frame time. |
| 1428 | * @return true if the animation has ended. |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1429 | * @hide |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1430 | */ |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1431 | public final boolean doAnimationFrame(long frameTime) { |
Doris Liu | 6d45209 | 2017-02-08 14:47:08 -0800 | [diff] [blame] | 1432 | if (mStartTime < 0) { |
| 1433 | // First frame. If there is start delay, start delay count down will happen *after* this |
| 1434 | // frame. |
Jorim Jaggi | c2333b7 | 2017-11-13 15:47:46 +0100 | [diff] [blame] | 1435 | mStartTime = mReversing |
| 1436 | ? frameTime |
| 1437 | : frameTime + (long) (mStartDelay * resolveDurationScale()); |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | // Handle pause/resume |
| 1441 | if (mPaused) { |
| 1442 | mPauseTime = frameTime; |
| 1443 | removeAnimationCallback(); |
| 1444 | return false; |
| 1445 | } else if (mResumed) { |
| 1446 | mResumed = false; |
| 1447 | if (mPauseTime > 0) { |
| 1448 | // Offset by the duration that the animation was paused |
| 1449 | mStartTime += (frameTime - mPauseTime); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | if (!mRunning) { |
Doris Liu | 6d45209 | 2017-02-08 14:47:08 -0800 | [diff] [blame] | 1454 | // If not running, that means the animation is in the start delay phase of a forward |
| 1455 | // running animation. In the case of reversing, we want to run start delay in the end. |
| 1456 | if (mStartTime > frameTime && mSeekFraction == -1) { |
| 1457 | // This is when no seek fraction is set during start delay. If developers change the |
| 1458 | // seek fraction during the delay, animation will start from the seeked position |
| 1459 | // right away. |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1460 | return false; |
| 1461 | } else { |
Doris Liu | 6d45209 | 2017-02-08 14:47:08 -0800 | [diff] [blame] | 1462 | // If mRunning is not set by now, that means non-zero start delay, |
| 1463 | // no seeking, not reversing. At this point, start delay has passed. |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1464 | mRunning = true; |
Doris Liu | 6d45209 | 2017-02-08 14:47:08 -0800 | [diff] [blame] | 1465 | startAnimation(); |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | if (mLastFrameTime < 0) { |
Doris Liu | 6d45209 | 2017-02-08 14:47:08 -0800 | [diff] [blame] | 1470 | if (mSeekFraction >= 0) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1471 | long seekTime = (long) (getScaledDuration() * mSeekFraction); |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 1472 | mStartTime = frameTime - seekTime; |
| 1473 | mSeekFraction = -1; |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1474 | } |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1475 | mStartTimeCommitted = false; // allow start time to be compensated for jank |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1476 | } |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 1477 | mLastFrameTime = frameTime; |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1478 | // The frame time might be before the start time during the first frame of |
| 1479 | // an animation. The "current time" must always be on or after the start |
| 1480 | // time to avoid animating frames at negative time intervals. In practice, this |
| 1481 | // is very rare and only happens when seeking backwards. |
| 1482 | final long currentTime = Math.max(frameTime, mStartTime); |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1483 | boolean finished = animateBasedOnTime(currentTime); |
| 1484 | |
| 1485 | if (finished) { |
| 1486 | endAnimation(); |
| 1487 | } |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1488 | return finished; |
| 1489 | } |
| 1490 | |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 1491 | @Override |
| 1492 | boolean pulseAnimationFrame(long frameTime) { |
| 1493 | if (mSelfPulse) { |
| 1494 | // Pulse animation frame will *always* be after calling start(). If mSelfPulse isn't |
| 1495 | // set to false at this point, that means child animators did not call super's start(). |
| 1496 | // This can happen when the Animator is just a non-animating wrapper around a real |
| 1497 | // functional animation. In this case, we can't really pulse a frame into the animation, |
| 1498 | // because the animation cannot necessarily be properly initialized (i.e. no start/end |
| 1499 | // values set). |
| 1500 | return false; |
| 1501 | } |
| 1502 | return doAnimationFrame(frameTime); |
| 1503 | } |
| 1504 | |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1505 | private void addOneShotCommitCallback() { |
| 1506 | if (!mSelfPulse) { |
| 1507 | return; |
| 1508 | } |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 1509 | getAnimationHandler().addOneShotCommitCallback(this); |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | private void removeAnimationCallback() { |
| 1513 | if (!mSelfPulse) { |
| 1514 | return; |
| 1515 | } |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 1516 | getAnimationHandler().removeCallback(this); |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | private void addAnimationCallback(long delay) { |
| 1520 | if (!mSelfPulse) { |
| 1521 | return; |
| 1522 | } |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 1523 | getAnimationHandler().addAnimationFrameCallback(this, delay); |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 1527 | * Returns the current animation fraction, which is the elapsed/interpolated fraction used in |
| 1528 | * the most recent frame update on the animation. |
| 1529 | * |
| 1530 | * @return Elapsed/interpolated fraction of the animation. |
| 1531 | */ |
| 1532 | public float getAnimatedFraction() { |
| 1533 | return mCurrentFraction; |
| 1534 | } |
| 1535 | |
| 1536 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1537 | * This method is called with the elapsed fraction of the animation during every |
| 1538 | * animation frame. This function turns the elapsed fraction into an interpolated fraction |
| 1539 | * and then into an animated value (from the evaluator. The function is called mostly during |
| 1540 | * animation updates, but it is also called when the <code>end()</code> |
| 1541 | * function is called, to set the final value on the property. |
| 1542 | * |
| 1543 | * <p>Overrides of this method must call the superclass to perform the calculation |
| 1544 | * of the animated value.</p> |
| 1545 | * |
| 1546 | * @param fraction The elapsed fraction of the animation. |
| 1547 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1548 | @CallSuper |
Mathew Inwood | a228c37 | 2018-08-01 14:35:45 +0100 | [diff] [blame] | 1549 | @UnsupportedAppUsage |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1550 | void animateValue(float fraction) { |
| 1551 | fraction = mInterpolator.getInterpolation(fraction); |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 1552 | mCurrentFraction = fraction; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1553 | int numValues = mValues.length; |
| 1554 | for (int i = 0; i < numValues; ++i) { |
| 1555 | mValues[i].calculateValue(fraction); |
| 1556 | } |
| 1557 | if (mUpdateListeners != null) { |
| 1558 | int numListeners = mUpdateListeners.size(); |
| 1559 | for (int i = 0; i < numListeners; ++i) { |
| 1560 | mUpdateListeners.get(i).onAnimationUpdate(this); |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | @Override |
| 1566 | public ValueAnimator clone() { |
| 1567 | final ValueAnimator anim = (ValueAnimator) super.clone(); |
| 1568 | if (mUpdateListeners != null) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 1569 | anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(mUpdateListeners); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1570 | } |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 1571 | anim.mSeekFraction = -1; |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 1572 | anim.mReversing = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1573 | anim.mInitialized = false; |
ztenghui | 26e9a19 | 2015-04-10 13:14:17 -0700 | [diff] [blame] | 1574 | anim.mStarted = false; |
| 1575 | anim.mRunning = false; |
| 1576 | anim.mPaused = false; |
| 1577 | anim.mResumed = false; |
| 1578 | anim.mStartListenersCalled = false; |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1579 | anim.mStartTime = -1; |
ztenghui | e1b5c2b | 2015-04-21 14:17:00 -0700 | [diff] [blame] | 1580 | anim.mStartTimeCommitted = false; |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 1581 | anim.mAnimationEndRequested = false; |
Doris Liu | 1335199 | 2017-01-17 17:10:42 -0800 | [diff] [blame] | 1582 | anim.mPauseTime = -1; |
| 1583 | anim.mLastFrameTime = -1; |
| 1584 | anim.mFirstFrameTime = -1; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1585 | anim.mOverallFraction = 0; |
ztenghui | e1b5c2b | 2015-04-21 14:17:00 -0700 | [diff] [blame] | 1586 | anim.mCurrentFraction = 0; |
Doris Liu | 5c71b8c | 2017-01-31 14:38:21 -0800 | [diff] [blame] | 1587 | anim.mSelfPulse = true; |
| 1588 | anim.mSuppressSelfPulseRequested = false; |
ztenghui | 26e9a19 | 2015-04-10 13:14:17 -0700 | [diff] [blame] | 1589 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1590 | PropertyValuesHolder[] oldValues = mValues; |
| 1591 | if (oldValues != null) { |
| 1592 | int numValues = oldValues.length; |
| 1593 | anim.mValues = new PropertyValuesHolder[numValues]; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1594 | anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); |
| 1595 | for (int i = 0; i < numValues; ++i) { |
Chet Haase | d4dd702 | 2011-04-04 15:25:09 -0700 | [diff] [blame] | 1596 | PropertyValuesHolder newValuesHolder = oldValues[i].clone(); |
| 1597 | anim.mValues[i] = newValuesHolder; |
| 1598 | anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | return anim; |
| 1602 | } |
| 1603 | |
| 1604 | /** |
| 1605 | * Implementors of this interface can add themselves as update listeners |
| 1606 | * to an <code>ValueAnimator</code> instance to receive callbacks on every animation |
| 1607 | * frame, after the current frame's values have been calculated for that |
| 1608 | * <code>ValueAnimator</code>. |
| 1609 | */ |
| 1610 | public static interface AnimatorUpdateListener { |
| 1611 | /** |
| 1612 | * <p>Notifies the occurrence of another frame of the animation.</p> |
| 1613 | * |
| 1614 | * @param animation The animation which was repeated. |
| 1615 | */ |
| 1616 | void onAnimationUpdate(ValueAnimator animation); |
| 1617 | |
| 1618 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1619 | |
| 1620 | /** |
| 1621 | * Return the number of animations currently running. |
| 1622 | * |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1623 | * Used by StrictMode internally to annotate violations. |
| 1624 | * May be called on arbitrary threads! |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1625 | * |
| 1626 | * @hide |
| 1627 | */ |
| 1628 | public static int getCurrentAnimationsCount() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1629 | return AnimationHandler.getAnimationCount(); |
Patrick Dubroy | 8901ffa | 2011-01-16 17:13:55 -0800 | [diff] [blame] | 1630 | } |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 1631 | |
| 1632 | @Override |
| 1633 | public String toString() { |
| 1634 | String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode()); |
| 1635 | if (mValues != null) { |
| 1636 | for (int i = 0; i < mValues.length; ++i) { |
| 1637 | returnVal += "\n " + mValues[i].toString(); |
| 1638 | } |
| 1639 | } |
| 1640 | return returnVal; |
| 1641 | } |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 1642 | |
| 1643 | /** |
| 1644 | * <p>Whether or not the ValueAnimator is allowed to run asynchronously off of |
| 1645 | * the UI thread. This is a hint that informs the ValueAnimator that it is |
| 1646 | * OK to run the animation off-thread, however ValueAnimator may decide |
| 1647 | * that it must run the animation on the UI thread anyway. For example if there |
| 1648 | * is an {@link AnimatorUpdateListener} the animation will run on the UI thread, |
| 1649 | * regardless of the value of this hint.</p> |
| 1650 | * |
| 1651 | * <p>Regardless of whether or not the animation runs asynchronously, all |
| 1652 | * listener callbacks will be called on the UI thread.</p> |
| 1653 | * |
| 1654 | * <p>To be able to use this hint the following must be true:</p> |
| 1655 | * <ol> |
| 1656 | * <li>{@link #getAnimatedFraction()} is not needed (it will return undefined values).</li> |
| 1657 | * <li>The animator is immutable while {@link #isStarted()} is true. Requests |
| 1658 | * to change values, duration, delay, etc... may be ignored.</li> |
| 1659 | * <li>Lifecycle callback events may be asynchronous. Events such as |
| 1660 | * {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or |
| 1661 | * {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed |
| 1662 | * as they must be posted back to the UI thread, and any actions performed |
| 1663 | * by those callbacks (such as starting new animations) will not happen |
| 1664 | * in the same frame.</li> |
| 1665 | * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...) |
| 1666 | * may be asynchronous. It is guaranteed that all state changes that are |
| 1667 | * performed on the UI thread in the same frame will be applied as a single |
| 1668 | * atomic update, however that frame may be the current frame, |
| 1669 | * the next frame, or some future frame. This will also impact the observed |
| 1670 | * state of the Animator. For example, {@link #isStarted()} may still return true |
| 1671 | * after a call to {@link #end()}. Using the lifecycle callbacks is preferred over |
| 1672 | * queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()} |
| 1673 | * for this reason.</li> |
| 1674 | * </ol> |
| 1675 | * @hide |
| 1676 | */ |
John Reck | c01bd11 | 2014-07-18 16:22:09 -0700 | [diff] [blame] | 1677 | @Override |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 1678 | public void setAllowRunningAsynchronously(boolean mayRunAsync) { |
| 1679 | // It is up to subclasses to support this, if they can. |
| 1680 | } |
Winson Chung | 4a526c1 | 2017-05-16 13:35:43 -0700 | [diff] [blame] | 1681 | |
| 1682 | /** |
| 1683 | * @return The {@link AnimationHandler} that will be used to schedule updates for this animator. |
| 1684 | * @hide |
| 1685 | */ |
| 1686 | public AnimationHandler getAnimationHandler() { |
| 1687 | return AnimationHandler.getInstance(); |
| 1688 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1689 | } |