ValueAnimator.java revision 3b466875bc9b931fce339b95aeaa7d19bb3fc640
1a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/* 2a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Copyright (C) 2010 The Android Open Source Project 3a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 4a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Licensed under the Apache License, Version 2.0 (the "License"); 5a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * you may not use this file except in compliance with the License. 6a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * You may obtain a copy of the License at 7a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 8a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * http://www.apache.org/licenses/LICENSE-2.0 9a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 10a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Unless required by applicable law or agreed to in writing, software 11a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * distributed under the License is distributed on an "AS IS" BASIS, 12a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * See the License for the specific language governing permissions and 14a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * limitations under the License. 15a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 16a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 17a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haasepackage android.animation; 18a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 19c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbyeimport android.annotation.CallSuper; 207764b920f21e0b9250122ff26533d5dac98df6b3George Mountimport android.annotation.IntDef; 210a815bb94fa2aad016a10fe23633efc5682d2a7cTeng-Hui Zhuimport android.annotation.TestApi; 22a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Looper; 2318772ea228f3d292629c4f0b5f6392d047e0530dRomain Guyimport android.os.Trace; 242970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haaseimport android.util.AndroidRuntimeException; 25c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brownimport android.util.Log; 26a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator; 2713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liuimport android.view.animation.Animation; 28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AnimationUtils; 2927c1d4debb3848f5accd5673fffeeacad3e61648Chet Haaseimport android.view.animation.LinearInterpolator; 30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 317764b920f21e0b9250122ff26533d5dac98df6b3George Mountimport java.lang.annotation.Retention; 327764b920f21e0b9250122ff26533d5dac98df6b3George Mountimport java.lang.annotation.RetentionPolicy; 33a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.ArrayList; 34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.HashMap; 35a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/** 37a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class provides a simple timing engine for running animations 38a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * which calculate animated values and set them on target objects. 39a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 40a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There is a single timing pulse that all animations use. It runs in a 41a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * custom handler to ensure that property changes happen on the UI thread.</p> 42a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 43a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>By default, ValueAnimator uses non-linear time interpolation, via the 44a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates 45a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * out of an animation. This behavior can be changed by calling 46e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p> 473aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * 48d430753cba09acb07af8b313286f247c78a41a32Chet Haase * <p>Animators can be created from either code or resource files. Here is an example 49d430753cba09acb07af8b313286f247c78a41a32Chet Haase * of a ValueAnimator resource file:</p> 50d430753cba09acb07af8b313286f247c78a41a32Chet Haase * 51d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/animator.xml ValueAnimatorResources} 52d430753cba09acb07af8b313286f247c78a41a32Chet Haase * 5321b93c1f662c2d399fa0769b05383be8308b2ecfDoris Liu * <p>Starting from API 23, it is also possible to use a combination of {@link PropertyValuesHolder} 5421b93c1f662c2d399fa0769b05383be8308b2ecfDoris Liu * and {@link Keyframe} resource tags to create a multi-step animation. 55d430753cba09acb07af8b313286f247c78a41a32Chet Haase * Note that you can specify explicit fractional values (from 0 to 1) for 56d430753cba09acb07af8b313286f247c78a41a32Chet Haase * each keyframe to determine when, in the overall duration, the animation should arrive at that 57d430753cba09acb07af8b313286f247c78a41a32Chet Haase * value. Alternatively, you can leave the fractions off and the keyframes will be equally 58d430753cba09acb07af8b313286f247c78a41a32Chet Haase * distributed within the total duration:</p> 59d430753cba09acb07af8b313286f247c78a41a32Chet Haase * 60d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/value_animator_pvh_kf.xml 61d430753cba09acb07af8b313286f247c78a41a32Chet Haase * ValueAnimatorKeyframeResources} 62d430753cba09acb07af8b313286f247c78a41a32Chet Haase * 633aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference"> 643aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3> 653aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about animating with {@code ValueAnimator}, read the 663aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property 673aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * Animation</a> developer guide.</p> 683aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div> 69a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 7018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy@SuppressWarnings("unchecked") 7113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liupublic class ValueAnimator extends Animator { 72c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown private static final String TAG = "ValueAnimator"; 73c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown private static final boolean DEBUG = false; 74a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 75a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 76a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Internal constants 77a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 78d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase private static float sDurationScale = 1.0f; 79a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 80a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 81a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Internal variables 82a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * NOTE: This object implements the clone() method, making a deep copy of any referenced 83a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * objects. As other non-trivial fields are added to this class, make sure to add logic 84a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to clone() to make deep copies of them. 85a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 86a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 87c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown /** 88c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * The first time that the animation's animateFrame() method is called. This time is used to 89c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * determine elapsed time (and therefore the elapsed fraction) in subsequent calls 90c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * to animateFrame(). 91c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * 92c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * Whenever mStartTime is set, you must also update mStartTimeCommitted. 93c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown */ 9413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu long mStartTime = -1; 95a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 96a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 97c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * When true, the start time has been firmly committed as a chosen reference point in 98c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * time by which the progress of the animation will be evaluated. When false, the 99c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * start time may be updated when the first animation frame is committed so as 100c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * to compensate for jank that may have occurred between when the start time was 101c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * initialized and when the frame was actually drawn. 102c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * 103c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * This flag is generally set to false during the first frame of the animation 104c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * when the animation playing state transitions from STOPPED to RUNNING or 105c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * resumes after having been paused. This flag is set to true when the start time 106c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * is firmly committed and should not be further compensated for jank. 107c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown */ 108c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown boolean mStartTimeCommitted; 109c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown 110c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown /** 111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked 112a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to a value. 113a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1140d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase float mSeekFraction = -1; 115a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1168aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase /** 1178aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * Set on the next frame after pause() is called, used to calculate a new startTime 1188aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * or delayStartTime which allows the animator to continue from the point at which 1198aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * it was paused. If negative, has not yet been set. 1208aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase */ 1218aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase private long mPauseTime; 1228aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase 1238aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase /** 1248aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * Set when an animator is resumed. This triggers logic in the next frame which 1258aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * actually resumes the animator. 1268aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase */ 1278aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase private boolean mResumed = false; 1288aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase 129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The time interpolator to be used if none is set on the animation 130e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase private static final TimeInterpolator sDefaultInterpolator = 131e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase new AccelerateDecelerateInterpolator(); 132a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 133a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 134f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * Flag to indicate whether this animator is playing in reverse mode, specifically 135f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * by being started or interrupted by a call to reverse(). This flag is different than 136f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * mPlayingBackwards, which indicates merely whether the current iteration of the 137f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * animator is playing in reverse. It is used in corner cases to determine proper end 138f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * behavior. 139f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase */ 140f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase private boolean mReversing; 141f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase 142f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase /** 143fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * Tracks the overall fraction of the animation, ranging from 0 to mRepeatCount + 1 144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 145fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu private float mOverallFraction = 0f; 146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 148a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction(). 149fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * This is calculated by interpolating the fraction (range: [0, 1]) in the current iteration. 150a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase */ 151a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase private float mCurrentFraction = 0f; 152a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase 153a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase /** 1543618d30f8ab6018025b11869676b309c3b4961cfDoris Liu * Tracks the time (in milliseconds) when the last frame arrived. 155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 15613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu private long mLastFrameTime = -1; 15713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 15813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu /** 15913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * Tracks the time (in milliseconds) when the first frame arrived. Note the frame may arrive 16013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * during the start delay. 16113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu */ 16213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu private long mFirstFrameTime = -1; 163a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 164a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 165b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * Additional playing state to indicate whether an animator has been start()'d. There is 166b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * some lag between a call to start() and the first animation frame. We should still note 167b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * that the animation has been started, even if it's first animation frame has not yet 168b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * happened, and reflect that state in isRunning(). 169b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * Note that delayed animations are different: they are not started until their first 170b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * animation frame, which occurs after their delay elapses. 171b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase */ 1728b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase private boolean mRunning = false; 1738b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase 1748b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase /** 1758b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase * Additional playing state to indicate whether an animator has been start()'d, whether or 1768b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase * not there is a nonzero startDelay. 1778b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase */ 178b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase private boolean mStarted = false; 179b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase 180b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase /** 1818aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * Tracks whether we've notified listeners of the onAnimationStart() event. This can be 18217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase * complex to keep track of since we notify listeners at different times depending on 18317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase * startDelay and whether start() was called before end(). 18417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase */ 18517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase private boolean mStartListenersCalled = false; 18617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase 18717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase /** 188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Flag that denotes whether the animation is set up and ready to go. Used to 189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * set up animation that has not yet been started. 190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 191a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase boolean mInitialized = false; 192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1933dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu /** 1943dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu * Flag that tracks whether animation has been requested to end. 1953dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu */ 1963dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu private boolean mAnimationEndRequested = false; 1973dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu 198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // 199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Backing variables 200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // 201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // How long the animation should last in ms 203fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu private long mDuration = 300; 204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 205fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // The amount of time in ms to delay starting the animation after start() is called. Note 206fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // that this start delay is unscaled. When there is a duration scale set on the animator, the 207fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // scaling factor will be applied to this delay. 208fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu private long mStartDelay = 0; 209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The number of times the animation will repeat. The default is 0, which means the animation 211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // will play only once 212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private int mRepeatCount = 0; 213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The type of repetition that will occur when repeatMode is nonzero. RESTART means the 216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation will start from the beginning on every new cycle. REVERSE means the animation 217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will reverse directions on each iteration. 218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private int mRepeatMode = RESTART; 220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 22213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * Whether or not the animator should register for its own animation callback to receive 22313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * animation pulse. 22413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu */ 22513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu private boolean mSelfPulse = true; 22613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 22713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu /** 228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The time interpolator to be used. The elapsed fraction of the animation will be passed 229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * through this interpolator to calculate the interpolated fraction, which is then used to 230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * calculate the animated values. 231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 232e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase private TimeInterpolator mInterpolator = sDefaultInterpolator; 233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The set of listeners to be sent events through the life of an animation. 236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 237d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck ArrayList<AnimatorUpdateListener> mUpdateListeners = null; 238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The property/value sets being animated. 241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 242a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder[] mValues; 243a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 244a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values 246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * by property name during calls to getAnimatedValue(String). 247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase HashMap<String, PropertyValuesHolder> mValuesMap; 249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Public constants 252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 2547764b920f21e0b9250122ff26533d5dac98df6b3George Mount /** @hide */ 2557764b920f21e0b9250122ff26533d5dac98df6b3George Mount @IntDef({RESTART, REVERSE}) 2567764b920f21e0b9250122ff26533d5dac98df6b3George Mount @Retention(RetentionPolicy.SOURCE) 2577764b920f21e0b9250122ff26533d5dac98df6b3George Mount public @interface RepeatMode {} 2587764b920f21e0b9250122ff26533d5dac98df6b3George Mount 259a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 261a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * or a positive value, the animation restarts from the beginning. 262a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static final int RESTART = 1; 264a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 266a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * or a positive value, the animation reverses direction on every iteration. 267a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 268a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static final int REVERSE = 2; 269a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 270a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This value used used with the {@link #setRepeatCount(int)} property to repeat 271a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the animation indefinitely. 272a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 273a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static final int INFINITE = -1; 274a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 275c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase /** 276c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase * @hide 277c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase */ 2780a815bb94fa2aad016a10fe23633efc5682d2a7cTeng-Hui Zhu @TestApi 279c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase public static void setDurationScale(float durationScale) { 280c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase sDurationScale = durationScale; 281c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase } 282c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase 283a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 284ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown * @hide 285ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown */ 2860a815bb94fa2aad016a10fe23633efc5682d2a7cTeng-Hui Zhu @TestApi 287ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown public static float getDurationScale() { 288ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown return sDurationScale; 289ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown } 290ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown 291ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown /** 29249a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * Returns whether animators are currently enabled, system-wide. By default, all 29349a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * animators are enabled. This can change if either the user sets a Developer Option 29449a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * to set the animator duration scale to 0 or by Battery Savery mode being enabled 29549a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * (which disables all animations). 29649a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * 29749a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * <p>Developers should not typically need to call this method, but should an app wish 29849a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * to show a different experience when animators are disabled, this return value 29949a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * can be used as a decider of which experience to offer. 30049a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * 30149a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * @return boolean Whether animators are currently enabled. The default value is 30249a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase * <code>true</code>. 30349a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase */ 30449a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase public static boolean areAnimatorsEnabled() { 30549a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase return !(sDurationScale == 0); 30649a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase } 30749a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase 30849a513b571bcb8e5da94bd50ae465cbaafa44734Chet Haase /** 309a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Creates a new ValueAnimator object. This default constructor is primarily for 3102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * use internally; the factory methods which take parameters are more generally 311a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * useful. 312a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 313a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public ValueAnimator() { 314a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 315a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 316a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 3172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between int values. A single 3182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 3192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 3202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 3212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 3222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 323a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 3242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 3252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 32641f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase */ 3272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofInt(int... values) { 3282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 3292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setIntValues(values); 3302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 3312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 3332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3341ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * Constructs and returns a ValueAnimator that animates between color values. A single 3351ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * value implies that that value is the one being animated to. However, this is not typically 3361ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * useful in a ValueAnimator object because there is no way for the object to determine the 3371ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * starting value for the animation (unlike ObjectAnimator, which can derive that value 3381ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * from the target object and property being animated). Therefore, there should typically 3391ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * be two or more values. 3401ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * 3411ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * @param values A set of values that the animation will animate between over time. 3421ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * @return A ValueAnimator object that is set up to animate between the given values. 3431ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount */ 3441ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount public static ValueAnimator ofArgb(int... values) { 3451ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount ValueAnimator anim = new ValueAnimator(); 3461ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount anim.setIntValues(values); 3471ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount anim.setEvaluator(ArgbEvaluator.getInstance()); 3481ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount return anim; 3491ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount } 3501ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount 3511ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount /** 3522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between float values. A single 3532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 3542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 3552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 3562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 3572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 3582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 3602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 3612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofFloat(float... values) { 3632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 3642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setFloatValues(values); 3652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 3662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 3682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between the values 3702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * specified in the PropertyValuesHolder objects. 3712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of PropertyValuesHolder objects whose values will be animated 3732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * between over time. 3742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 3752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) { 3772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 3782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setValues(values); 3792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 3802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between Object values. A single 3832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 3842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 3852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 3862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 3872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 3882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 389fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * <p><strong>Note:</strong> The Object values are stored as references to the original 390fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * objects, which means that changes to those objects after this method is called will 391fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * affect the values on the animator. If the objects will be mutated externally after 392fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * this method is called, callers should pass a copy of those objects instead. 393fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * 3942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this 3952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * factory method also takes a TypeEvaluator object that the ValueAnimator will use 3962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * to perform that interpolation. 3972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param evaluator A TypeEvaluator that will be called on each animation frame to 3992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * provide the ncessry interpolation between the Object values to derive the animated 4002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value. 4012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 4022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 4032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 4042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) { 4052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 4062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setObjectValues(values); 4072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setEvaluator(evaluator); 4082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 4092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 4112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 4122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets int values that will be animated between. A single 4132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 4142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 4152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 4162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 4172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 4182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>If there are already multiple sets of values defined for this ValueAnimator via more 4202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * than one PropertyValuesHolder object, this method will set the values for the first 4212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * of those objects.</p> 4222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 4242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 4252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public void setIntValues(int... values) { 4262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (values == null || values.length == 0) { 4272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return; 4282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (mValues == null || mValues.length == 0) { 43018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy setValues(PropertyValuesHolder.ofInt("", values)); 4312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } else { 4322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase PropertyValuesHolder valuesHolder = mValues[0]; 4332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase valuesHolder.setIntValues(values); 4342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase // New property/values/target should cause re-initialization prior to starting 4362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase mInitialized = false; 4372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 4392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 4402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets float values that will be animated between. A single 4412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 4422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 4432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 4442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 4452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 4462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>If there are already multiple sets of values defined for this ValueAnimator via more 4482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * than one PropertyValuesHolder object, this method will set the values for the first 4492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * of those objects.</p> 4502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 4522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 4532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public void setFloatValues(float... values) { 4542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (values == null || values.length == 0) { 4552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return; 45641f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase } 4572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (mValues == null || mValues.length == 0) { 45818772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy setValues(PropertyValuesHolder.ofFloat("", values)); 4592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } else { 4602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase PropertyValuesHolder valuesHolder = mValues[0]; 4612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase valuesHolder.setFloatValues(values); 4622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase // New property/values/target should cause re-initialization prior to starting 4642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase mInitialized = false; 4652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 4672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 4682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets the values to animate between for this animation. A single 4692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 4702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 4712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 4722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 4732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 4742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 475fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * <p><strong>Note:</strong> The Object values are stored as references to the original 476fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * objects, which means that changes to those objects after this method is called will 477fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * affect the values on the animator. If the objects will be mutated externally after 478fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * this method is called, callers should pass a copy of those objects instead. 479fa21bdfd5a8a5da3ec0530f7cc884994f92dc597Chet Haase * 4802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>If there are already multiple sets of values defined for this ValueAnimator via more 4812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * than one PropertyValuesHolder object, this method will set the values for the first 4822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * of those objects.</p> 4832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate 4852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * between these value objects. ValueAnimator only knows how to interpolate between the 4862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * primitive types specified in the other setValues() methods.</p> 4872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values The set of values to animate between. 4892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 4902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public void setObjectValues(Object... values) { 4912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (values == null || values.length == 0) { 4922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return; 4932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (mValues == null || mValues.length == 0) { 49518772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy setValues(PropertyValuesHolder.ofObject("", null, values)); 4962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } else { 4972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase PropertyValuesHolder valuesHolder = mValues[0]; 4982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase valuesHolder.setObjectValues(values); 4992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 5002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase // New property/values/target should cause re-initialization prior to starting 5012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase mInitialized = false; 502a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 503a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 504a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 505a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets the values, per property, being animated between. This function is called internally 506f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can 507a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * be constructed without values and this method can be called to set the values manually 508a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * instead. 509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 510a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param values The set of values, per property, being animated between. 511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 512a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setValues(PropertyValuesHolder... values) { 513a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = values.length; 514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues = values; 515a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); 516a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 51718772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy PropertyValuesHolder valuesHolder = values[i]; 518a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder); 519a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 5200e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase // New property/values/target should cause re-initialization prior to starting 5210e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase mInitialized = false; 522a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 523a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 524a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 525a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Returns the values that this ValueAnimator animates between. These values are stored in 526a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list 527a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of value objects instead. 528a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 529a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the 530a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * values, per property, that define the animation. 531a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 532a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public PropertyValuesHolder[] getValues() { 533a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mValues; 534a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 535a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 536a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 537a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This function is called immediately before processing the first animation 538a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frame of an animation. If there is a nonzero <code>startDelay</code>, the 539a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function is called after that delay ends. 540a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * It takes care of the final initialization steps for the 541a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation. 542a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 543a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Overrides of this method should call the superclass method to ensure 544a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * that internal mechanisms for the animation are set up correctly.</p> 545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 546c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye @CallSuper 547a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase void initAnimation() { 548a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (!mInitialized) { 549a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = mValues.length; 550a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 551a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues[i].init(); 552a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 553a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mInitialized = true; 554a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 555a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 556a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 557a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 5582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets the length of the animation. The default duration is 300 milliseconds. 559a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 56027c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * @param duration The length of the animation, in milliseconds. This value cannot 56127c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * be negative. 5622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return ValueAnimator The object called with setDuration(). This return 5632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value makes it easier to compose statements together that construct and then set the 5642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>. 565a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 566c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown @Override 5672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public ValueAnimator setDuration(long duration) { 56827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase if (duration < 0) { 56927c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase throw new IllegalArgumentException("Animators cannot have negative duration: " + 57027c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase duration); 57127c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase } 572fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu mDuration = duration; 5732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return this; 574a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 575a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 576fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu private long getScaledDuration() { 577fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return (long)(mDuration * sDurationScale); 5787a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown } 5797a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown 580a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 5812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Gets the length of the animation. The default duration is 300 milliseconds. 582a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 583a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return The length of the animation, in milliseconds. 584a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 585c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown @Override 586a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public long getDuration() { 587fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return mDuration; 588a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 589a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 5901309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu @Override 5911309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu public long getTotalDuration() { 5921309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu if (mRepeatCount == INFINITE) { 5931309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu return DURATION_INFINITE; 5941309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu } else { 595fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return mStartDelay + (mDuration * (mRepeatCount + 1)); 5961309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu } 5971309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu } 5981309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu 5991309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu /** 600a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets the position of the animation to the specified point in time. This time should 601a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * be between 0 and the total duration of the animation, including any repetition. If 602a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the animation has not yet been started, then it will not advance forward after it is 603a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * set to this time; it will simply set the time to this value and perform any appropriate 604a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * actions based on that time. If the animation is already running, then setCurrentPlayTime() 605a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will set the current playing time to this value and continue playing from that point. 606a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 607a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param playTime The time, in milliseconds, to which the animation is advanced or rewound. 608a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 609a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setCurrentPlayTime(long playTime) { 610fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu float fraction = mDuration > 0 ? (float) playTime / mDuration : 1; 6110d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase setCurrentFraction(fraction); 6120d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase } 6130d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase 6140d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase /** 6150d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * Sets the position of the animation to the specified fraction. This fraction should 6160d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * be between 0 and the total fraction of the animation, including any repetition. That is, 6170d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * a fraction of 0 will position the animation at the beginning, a value of 1 at the end, 618f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * and a value of 2 at the end of a reversing animator that repeats once. If 6190d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * the animation has not yet been started, then it will not advance forward after it is 6200d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * set to this fraction; it will simply set the fraction to this value and perform any 6210d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * appropriate actions based on that fraction. If the animation is already running, then 6220d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * setCurrentFraction() will set the current fraction to this value and continue 6235a5afe010d2f8fb7e8f00858b8a305b4745c0469Eino-Ville Talvala * playing from that point. {@link Animator.AnimatorListener} events are not called 624f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * due to changing the fraction; those events are only processed while the animation 625f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * is running. 6260d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * 627f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * @param fraction The fraction to which the animation is advanced or rewound. Values 628f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * outside the range of 0 to the maximum fraction for the animator will be clamped to 629f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * the correct range. 6300d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase */ 6310d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase public void setCurrentFraction(float fraction) { 632a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase initAnimation(); 633fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu fraction = clampFraction(fraction); 634c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown mStartTimeCommitted = true; // do not allow start time to be compensated for jank 6353b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu if (isPulsingInternal()) { 6363b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu long seekTime = (long) (getScaledDuration() * fraction); 6373b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu long currentTime = AnimationUtils.currentAnimationTimeMillis(); 6383b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu // Only modify the start time when the animation is running. Seek fraction will ensure 6393b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu // non-running animations skip to the correct start time. 6403b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu mStartTime = currentTime - seekTime; 6413b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu } else { 6423b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu // If the animation loop hasn't started, or during start delay, the startTime will be 6433b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu // adjusted once the delay has passed based on seek fraction. 6440d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase mSeekFraction = fraction; 645a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 646fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu mOverallFraction = fraction; 64713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu final float currentIterationFraction = getCurrentIterationFraction(fraction, mReversing); 648fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu animateValue(currentIterationFraction); 649fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } 650fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu 651fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu /** 652fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * Calculates current iteration based on the overall fraction. The overall fraction will be 653fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * in the range of [0, mRepeatCount + 1]. Both current iteration and fraction in the current 654fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * iteration can be derived from it. 655fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu */ 656fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu private int getCurrentIteration(float fraction) { 657fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu fraction = clampFraction(fraction); 658fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // If the overall fraction is a positive integer, we consider the current iteration to be 659fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // complete. In other words, the fraction for the current iteration would be 1, and the 660fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // current iteration would be overall fraction - 1. 661fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu double iteration = Math.floor(fraction); 662fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu if (fraction == iteration && fraction > 0) { 663fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu iteration--; 664fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } 665fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return (int) iteration; 666fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } 667fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu 668fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu /** 669fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * Calculates the fraction of the current iteration, taking into account whether the animation 670fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * should be played backwards. E.g. When the animation is played backwards in an iteration, 671fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * the fraction for that iteration will go from 1f to 0f. 672fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu */ 67313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu private float getCurrentIterationFraction(float fraction, boolean inReverse) { 674fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu fraction = clampFraction(fraction); 675fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu int iteration = getCurrentIteration(fraction); 676fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu float currentFraction = fraction - iteration; 67713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu return shouldPlayBackward(iteration, inReverse) ? 1f - currentFraction : currentFraction; 678fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } 679fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu 680fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu /** 681fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * Clamps fraction into the correct range: [0, mRepeatCount + 1]. If repeat count is infinite, 682fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * no upper bound will be set for the fraction. 683fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * 684fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * @param fraction fraction to be clamped 685fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * @return fraction clamped into the range of [0, mRepeatCount + 1] 686fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu */ 687fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu private float clampFraction(float fraction) { 688fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu if (fraction < 0) { 689fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu fraction = 0; 690fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } else if (mRepeatCount != INFINITE) { 691fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu fraction = Math.min(fraction, mRepeatCount + 1); 692fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } 693fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return fraction; 694fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } 695fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu 696fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu /** 697fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * Calculates the direction of animation playing (i.e. forward or backward), based on 1) 698fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * whether the entire animation is being reversed, 2) repeat mode applied to the current 699fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu * iteration. 700fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu */ 70113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu private boolean shouldPlayBackward(int iteration, boolean inReverse) { 702fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu if (iteration > 0 && mRepeatMode == REVERSE && 703fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu (iteration < (mRepeatCount + 1) || mRepeatCount == INFINITE)) { 704fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // if we were seeked to some other iteration in a reversing animator, 705fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // figure out the correct direction to start playing based on the iteration 70613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (inReverse) { 707fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return (iteration % 2) == 0; 708fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } else { 709fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return (iteration % 2) != 0; 710fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } 711fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } else { 71213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu return inReverse; 713f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 714a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 715a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 716a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 717a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Gets the current position of the animation in time, which is equal to the current 718a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * time minus the time that the animation started. An animation that is not yet started will 7194365e5a13755ffbdb977586c90b8b515add599aaChet Haase * return a value of zero, unless the animation has has its play time set via 7204365e5a13755ffbdb977586c90b8b515add599aaChet Haase * {@link #setCurrentPlayTime(long)} or {@link #setCurrentFraction(float)}, in which case 7214365e5a13755ffbdb977586c90b8b515add599aaChet Haase * it will return the time that was set. 722a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 723a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return The current position in time of the animation. 724a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 725a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public long getCurrentPlayTime() { 7264365e5a13755ffbdb977586c90b8b515add599aaChet Haase if (!mInitialized || (!mStarted && mSeekFraction < 0)) { 727a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return 0; 728a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 7294365e5a13755ffbdb977586c90b8b515add599aaChet Haase if (mSeekFraction >= 0) { 730fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return (long) (mDuration * mSeekFraction); 7314365e5a13755ffbdb977586c90b8b515add599aaChet Haase } 732fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu float durationScale = sDurationScale == 0 ? 1 : sDurationScale; 733fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return (long) ((AnimationUtils.currentAnimationTimeMillis() - mStartTime) / durationScale); 734a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 735a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 736a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 737a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, to delay starting the animation after 738a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link #start()} is called. 739a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 740a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return the number of milliseconds to delay running the animation 741a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 742c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown @Override 743a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public long getStartDelay() { 744fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu return mStartDelay; 745a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 746a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 747a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 748a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, to delay starting the animation after 74961045c518b18a7cee30954fe45f9db8c14e705e1Doris Liu * {@link #start()} is called. Note that the start delay should always be non-negative. Any 75061045c518b18a7cee30954fe45f9db8c14e705e1Doris Liu * negative start delay will be clamped to 0 on N and above. 75161045c518b18a7cee30954fe45f9db8c14e705e1Doris Liu * 752a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param startDelay The amount of the delay, in milliseconds 753a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 754c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown @Override 755a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setStartDelay(long startDelay) { 75661045c518b18a7cee30954fe45f9db8c14e705e1Doris Liu // Clamp start delay to non-negative range. 75761045c518b18a7cee30954fe45f9db8c14e705e1Doris Liu if (startDelay < 0) { 75861045c518b18a7cee30954fe45f9db8c14e705e1Doris Liu Log.w(TAG, "Start delay should always be non-negative"); 75961045c518b18a7cee30954fe45f9db8c14e705e1Doris Liu startDelay = 0; 76061045c518b18a7cee30954fe45f9db8c14e705e1Doris Liu } 761fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu mStartDelay = startDelay; 762a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 763a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 765a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 766a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * requested time that the animation will attempt to honor, but the actual delay between 767a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frames may be different, depending on system load and capabilities. This is a static 768a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function because the same delay will be applied to all animations, since they are all 769a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * run off of a single timing loop. 770a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 77196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * The frame delay may be ignored when the animation system uses an external timing 77296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * source, such as the display refresh rate (vsync), to govern animations. 77396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * 7742b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu * Note that this method should be called from the same thread that {@link #start()} is 7752b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu * called in order to check the frame delay for that animation. A runtime exception will be 7762b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu * thrown if the calling thread does not have a Looper. 7772b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu * 778a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return the requested time between frames, in milliseconds 779a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 780a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static long getFrameDelay() { 7813618d30f8ab6018025b11869676b309c3b4961cfDoris Liu return AnimationHandler.getInstance().getFrameDelay(); 782a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 783a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 784a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 785a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 786a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * requested time that the animation will attempt to honor, but the actual delay between 787a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frames may be different, depending on system load and capabilities. This is a static 788a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function because the same delay will be applied to all animations, since they are all 789a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * run off of a single timing loop. 790a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 79196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * The frame delay may be ignored when the animation system uses an external timing 79296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * source, such as the display refresh rate (vsync), to govern animations. 79396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * 7942b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu * Note that this method should be called from the same thread that {@link #start()} is 7952b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu * called in order to have the new frame delay take effect on that animation. A runtime 7962b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu * exception will be thrown if the calling thread does not have a Looper. 7972b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu * 798a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param frameDelay the requested time between frames, in milliseconds 799a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 800a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static void setFrameDelay(long frameDelay) { 8013618d30f8ab6018025b11869676b309c3b4961cfDoris Liu AnimationHandler.getInstance().setFrameDelay(frameDelay); 802a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 803a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 804a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 805a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The most recent value calculated by this <code>ValueAnimator</code> when there is just one 806a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * property being animated. This value is only sensible while the animation is running. The main 807a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code> 808a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which 809a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is called during each animation frame, immediately after the value is calculated. 810a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 811a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for 812a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the single property being animated. If there are several properties being animated 813a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * (specified by several PropertyValuesHolder objects in the constructor), this function 814a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * returns the animated value for the first of those objects. 815a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 816a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public Object getAnimatedValue() { 817a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mValues != null && mValues.length > 0) { 818a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mValues[0].getAnimatedValue(); 819a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 820a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Shouldn't get here; should always have values unless ValueAnimator was set up wrong 821a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return null; 822a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 823a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 824a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 825a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>. 826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The main purpose for this read-only property is to retrieve the value from the 827a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>ValueAnimator</code> during a call to 828a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which 829a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is called during each animation frame, immediately after the value is calculated. 830a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 831a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return animatedValue The value most recently calculated for the named property 832a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * by this <code>ValueAnimator</code>. 833a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 834a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public Object getAnimatedValue(String propertyName) { 835a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName); 836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (valuesHolder != null) { 837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return valuesHolder.getAnimatedValue(); 838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 839a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // At least avoid crashing if called with bogus propertyName 840a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return null; 841a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 844a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 845a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets how many times the animation should be repeated. If the repeat 846a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * count is 0, the animation is never repeated. If the repeat count is 847a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * greater than 0 or {@link #INFINITE}, the repeat mode will be taken 848a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * into account. The repeat count is 0 by default. 849a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 850a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param value the number of times the animation should be repeated 851a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 852a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setRepeatCount(int value) { 853a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mRepeatCount = value; 854a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 855a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 856a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Defines how many times the animation should repeat. The default value 857a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is 0. 858a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 859a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return the number of times the animation should repeat, or {@link #INFINITE} 860a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 861a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public int getRepeatCount() { 862a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mRepeatCount; 863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 865a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 866a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Defines what this animation should do when it reaches the end. This 867a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * setting is applied only when the repeat count is either greater than 868a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. 869a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 870a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param value {@link #RESTART} or {@link #REVERSE} 871a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 8727764b920f21e0b9250122ff26533d5dac98df6b3George Mount public void setRepeatMode(@RepeatMode int value) { 873a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mRepeatMode = value; 874a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 875a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 876a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 877a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Defines what this animation should do when it reaches the end. 878a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 879a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return either one of {@link #REVERSE} or {@link #RESTART} 880a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 8817764b920f21e0b9250122ff26533d5dac98df6b3George Mount @RepeatMode 882a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public int getRepeatMode() { 883a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mRepeatMode; 884a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 885a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 886a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 887a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Adds a listener to the set of listeners that are sent update events through the life of 888a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * an animation. This method is called on all listeners for every frame of the animation, 889a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * after the values for the animation have been calculated. 890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 891a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param listener the listener to be added to the current set of listeners for this animation. 892a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 893a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void addUpdateListener(AnimatorUpdateListener listener) { 894a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners == null) { 895a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); 896a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 897a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners.add(listener); 898a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 899a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 900a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 9013060421045d4d9e411797f91bb509824b03e33fbJim Miller * Removes all listeners from the set listening to frame updates for this animation. 9023060421045d4d9e411797f91bb509824b03e33fbJim Miller */ 9033060421045d4d9e411797f91bb509824b03e33fbJim Miller public void removeAllUpdateListeners() { 9043060421045d4d9e411797f91bb509824b03e33fbJim Miller if (mUpdateListeners == null) { 9053060421045d4d9e411797f91bb509824b03e33fbJim Miller return; 9063060421045d4d9e411797f91bb509824b03e33fbJim Miller } 9073060421045d4d9e411797f91bb509824b03e33fbJim Miller mUpdateListeners.clear(); 9083060421045d4d9e411797f91bb509824b03e33fbJim Miller mUpdateListeners = null; 9093060421045d4d9e411797f91bb509824b03e33fbJim Miller } 9103060421045d4d9e411797f91bb509824b03e33fbJim Miller 9113060421045d4d9e411797f91bb509824b03e33fbJim Miller /** 912a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Removes a listener from the set listening to frame updates for this animation. 913a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 914a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param listener the listener to be removed from the current set of update listeners 915a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * for this animation. 916a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 917a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void removeUpdateListener(AnimatorUpdateListener listener) { 918a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners == null) { 919a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return; 920a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 921a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners.remove(listener); 922a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners.size() == 0) { 923a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners = null; 924a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 925a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 926a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 927a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 928a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 929a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The time interpolator used in calculating the elapsed fraction of this animation. The 930a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * interpolator determines whether the animation runs with linear or non-linear motion, 931a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * such as acceleration and deceleration. The default value is 932a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link android.view.animation.AccelerateDecelerateInterpolator} 933a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 93427c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * @param value the interpolator to be used by this animation. A value of <code>null</code> 93527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * will result in linear interpolation. 936a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 937a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 938e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase public void setInterpolator(TimeInterpolator value) { 939a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (value != null) { 940a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mInterpolator = value; 94127c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase } else { 94227c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase mInterpolator = new LinearInterpolator(); 943a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 944a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 945a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 946a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 947a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Returns the timing interpolator that this ValueAnimator uses. 948a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 949a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return The timing interpolator for this ValueAnimator. 950a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 951430742f09063574271e6c4091de13b9b9e762514Chet Haase @Override 952e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase public TimeInterpolator getInterpolator() { 953a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mInterpolator; 954a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 955a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 956a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 957a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The type evaluator to be used when calculating the animated values of this animation. 958b2ab04ffb6894f399d5c9ceb15f64eb17b654426Chet Haase * The system will automatically assign a float or int evaluator based on the type 959a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values 960a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * are not one of these primitive types, or if different evaluation is desired (such as is 961a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * necessary with int values that represent colors), a custom evaluator needs to be assigned. 96253ee3316bcb3590ff156b3fd7108903c0817c35dChet Haase * For example, when running an animation on color values, the {@link ArgbEvaluator} 963a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be used to get correct RGB color interpolation. 964a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 965a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>If this ValueAnimator has only one set of values being animated between, this evaluator 966a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will be used for that set. If there are several sets of values being animated, which is 967fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase * the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator 968a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is assigned just to the first PropertyValuesHolder object.</p> 969a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 970a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param value the evaluator to be used this animation 971a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 972a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setEvaluator(TypeEvaluator value) { 973a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (value != null && mValues != null && mValues.length > 0) { 974a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues[0].setEvaluator(value); 975a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 976a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 977a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 97817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase private void notifyStartListeners() { 97917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase if (mListeners != null && !mStartListenersCalled) { 98017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase ArrayList<AnimatorListener> tmpListeners = 98117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 98217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase int numListeners = tmpListeners.size(); 98317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase for (int i = 0; i < numListeners; ++i) { 98413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu tmpListeners.get(i).onAnimationStart(this, mReversing); 98517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase } 98617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase } 98717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase mStartListenersCalled = true; 98817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase } 98917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase 990a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 991a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Start the animation playing. This version of start() takes a boolean flag that indicates 992a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * whether the animation should play in reverse. The flag is usually false, but may be set 9932970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * to true if called from the reverse() method. 9942970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * 9952970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * <p>The animation started by calling this method will be run on the thread that called 9962970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * this method. This thread should have a Looper on it (a runtime exception will be thrown if 9972970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * this is not the case). Also, if the animation will animate 9982970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * properties of objects in the view hierarchy, then the calling thread should be the UI 9992970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * thread for that view hierarchy.</p> 1000a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1001a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param playBackwards Whether the ValueAnimator should start playing in reverse. 1002a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 100313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu private void start(boolean playBackwards, boolean selfPulse) { 10042970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase if (Looper.myLooper() == null) { 10052970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase throw new AndroidRuntimeException("Animators may only be run on Looper threads"); 10063060421045d4d9e411797f91bb509824b03e33fbJim Miller } 1007f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mReversing = playBackwards; 100813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mSelfPulse = selfPulse; 1009fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // Special case: reversing from seek-to-0 should act as if not seeked at all. 1010fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu if (playBackwards && mSeekFraction != -1 && mSeekFraction != 0) { 1011fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu if (mRepeatCount == INFINITE) { 1012fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // Calculate the fraction of the current iteration. 1013fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu float fraction = (float) (mSeekFraction - Math.floor(mSeekFraction)); 1014fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu mSeekFraction = 1 - fraction; 1015f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } else { 1016fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu mSeekFraction = 1 + mRepeatCount - mSeekFraction; 1017f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 1018f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 10198b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase mStarted = true; 10208aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mPaused = false; 10213618d30f8ab6018025b11869676b309c3b4961cfDoris Liu mRunning = false; 1022543a7ed7af992c5f62a9df1a20f3a2397d3e6ec3Justin Klaassen mAnimationEndRequested = false; 1023caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu // Resets mLastFrameTime when start() is called, so that if the animation was running, 1024caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu // calling start() would put the animation in the 1025caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu // started-but-not-yet-reached-the-first-frame phase. 102613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mLastFrameTime = -1; 102713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mFirstFrameTime = -1; 10283b466875bc9b931fce339b95aeaa7d19bb3fc640Doris Liu addAnimationCallback(0); 1029f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu 103013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (mStartDelay == 0 || mSeekFraction >= 0 || mReversing) { 1031f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu // If there's no start delay, init the animation and notify start listeners right away 1032fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // to be consistent with the previous behavior. Otherwise, postpone this until the first 1033fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // frame after the start delay. 1034f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu startAnimation(); 1035b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu if (mSeekFraction == -1) { 1036b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu // No seek, start at play time 0. Note that the reason we are not using fraction 0 1037b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu // is because for animations with 0 duration, we want to be consistent with pre-N 1038b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu // behavior: skip to the final value immediately. 1039b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu setCurrentPlayTime(0); 1040b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu } else { 1041b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu setCurrentFraction(mSeekFraction); 1042b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu } 1043f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu } 1044a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1045a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 104613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu void startWithoutPulsing(boolean inReverse) { 104713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu start(inReverse, false); 104813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 104913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 1050a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 1051a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void start() { 105213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu start(false, true); 1053a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1054a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1055a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 1056a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void cancel() { 10573618d30f8ab6018025b11869676b309c3b4961cfDoris Liu if (Looper.myLooper() == null) { 10583618d30f8ab6018025b11869676b309c3b4961cfDoris Liu throw new AndroidRuntimeException("Animators may only be run on Looper threads"); 10593618d30f8ab6018025b11869676b309c3b4961cfDoris Liu } 10603dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu 10613dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu // If end has already been requested, through a previous end() or cancel() call, no-op 10623dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu // until animation starts again. 10633dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu if (mAnimationEndRequested) { 10643dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu return; 10653dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu } 10663dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu 10672970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase // Only cancel if the animation is actually running or has been started and is about 10682970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase // to run 10693618d30f8ab6018025b11869676b309c3b4961cfDoris Liu // Only notify listeners if the animator has actually started 10703618d30f8ab6018025b11869676b309c3b4961cfDoris Liu if ((mStarted || mRunning) && mListeners != null) { 10713618d30f8ab6018025b11869676b309c3b4961cfDoris Liu if (!mRunning) { 10723618d30f8ab6018025b11869676b309c3b4961cfDoris Liu // If it's not yet running, then start listeners weren't called. Call them now. 10733618d30f8ab6018025b11869676b309c3b4961cfDoris Liu notifyStartListeners(); 10743618d30f8ab6018025b11869676b309c3b4961cfDoris Liu } 10753618d30f8ab6018025b11869676b309c3b4961cfDoris Liu ArrayList<AnimatorListener> tmpListeners = 10763618d30f8ab6018025b11869676b309c3b4961cfDoris Liu (ArrayList<AnimatorListener>) mListeners.clone(); 10773618d30f8ab6018025b11869676b309c3b4961cfDoris Liu for (AnimatorListener listener : tmpListeners) { 10783618d30f8ab6018025b11869676b309c3b4961cfDoris Liu listener.onAnimationCancel(this); 10797dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase } 10802970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase } 10813618d30f8ab6018025b11869676b309c3b4961cfDoris Liu endAnimation(); 10823618d30f8ab6018025b11869676b309c3b4961cfDoris Liu 1083a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1084a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1085a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 1086a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void end() { 10873618d30f8ab6018025b11869676b309c3b4961cfDoris Liu if (Looper.myLooper() == null) { 10883618d30f8ab6018025b11869676b309c3b4961cfDoris Liu throw new AndroidRuntimeException("Animators may only be run on Looper threads"); 10893618d30f8ab6018025b11869676b309c3b4961cfDoris Liu } 10903618d30f8ab6018025b11869676b309c3b4961cfDoris Liu if (!mRunning) { 1091a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Special case if the animation has not yet started; get it ready for ending 10923618d30f8ab6018025b11869676b309c3b4961cfDoris Liu startAnimation(); 109317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase mStarted = true; 1094add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase } else if (!mInitialized) { 1095add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase initAnimation(); 1096e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase } 109713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu animateValue(shouldPlayBackward(mRepeatCount, mReversing) ? 0f : 1f); 10983618d30f8ab6018025b11869676b309c3b4961cfDoris Liu endAnimation(); 1099a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1100a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1101a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 11028aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase public void resume() { 1103a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount if (Looper.myLooper() == null) { 1104a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount throw new AndroidRuntimeException("Animators may only be resumed from the same " + 1105a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount "thread that the animator was started on"); 1106a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount } 1107a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount if (mPaused && !mResumed) { 11088aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mResumed = true; 1109a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount if (mPauseTime > 0) { 111013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu addAnimationCallback(0); 1111a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount } 11128aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase } 11138aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase super.resume(); 11148aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase } 11158aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase 11168aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase @Override 11178aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase public void pause() { 11188aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase boolean previouslyPaused = mPaused; 11198aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase super.pause(); 11208aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase if (!previouslyPaused && mPaused) { 11218aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mPauseTime = -1; 11228aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mResumed = false; 11238aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase } 11248aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase } 11258aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase 11268aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase @Override 1127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public boolean isRunning() { 11283618d30f8ab6018025b11869676b309c3b4961cfDoris Liu return mRunning; 11298b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase } 11308b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase 11318b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase @Override 11328b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase public boolean isStarted() { 11338b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase return mStarted; 1134a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1135a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1136a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1137a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Plays the ValueAnimator in reverse. If the animation is already running, 1138a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * it will stop itself and play backwards from the point reached when reverse was called. 1139a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * If the animation is not currently running, then it will start from the end and 1140a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * play backwards. This behavior is only set for the current animation; future playing 1141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of the animation will use the default behavior of playing forward. 1142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 11437bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui @Override 1144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void reverse() { 1145caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu if (isPulsingInternal()) { 1146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 1147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long currentPlayTime = currentTime - mStartTime; 1148fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu long timeLeft = getScaledDuration() - currentPlayTime; 1149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartTime = currentTime - timeLeft; 1150c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown mStartTimeCommitted = true; // do not allow start time to be compensated for jank 1151f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mReversing = !mReversing; 1152f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase } else if (mStarted) { 1153caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu mReversing = !mReversing; 1154f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase end(); 1155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 115613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu start(true, true); 1157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 11617bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui * @hide 11627bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui */ 11637bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui @Override 11647bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui public boolean canReverse() { 11657bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui return true; 11667bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui } 11677bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui 11687bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui /** 1169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Called internally to end an animation by removing it from the animations list. Must be 1170a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * called on the UI thread. 1171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 11723dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu private void endAnimation() { 11733dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu if (mAnimationEndRequested) { 11743dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu return; 11753dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu } 117613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu removeAnimationCallback(); 11773dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu 11783dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu mAnimationEndRequested = true; 11798aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mPaused = false; 11800eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount boolean notify = (mStarted || mRunning) && mListeners != null; 11810eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount if (notify && !mRunning) { 11820eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount // If it's not yet running, then start listeners weren't called. Call them now. 11830eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount notifyStartListeners(); 11840eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount } 11850eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount mRunning = false; 11860eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount mStarted = false; 11870eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount mStartListenersCalled = false; 118813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mLastFrameTime = -1; 118913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mFirstFrameTime = -1; 11900eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount mReversing = false; 11910eba1dfba040c096f8cd1b7b6d4c289ee78db4b8George Mount if (notify && mListeners != null) { 1192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorListener> tmpListeners = 1193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 11947c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = tmpListeners.size(); 11957c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 119613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu tmpListeners.get(i).onAnimationEnd(this, mReversing); 1197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 119913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mReversing = false; 12009b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { 12019b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(), 12029b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase System.identityHashCode(this)); 12039b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase } 1204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Called internally to start an animation by adding it to the active animations list. Must be 1208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * called on the UI thread. 1209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 12103618d30f8ab6018025b11869676b309c3b4961cfDoris Liu private void startAnimation() { 12119b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { 12129b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(), 12139b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase System.identityHashCode(this)); 12149b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase } 121566d48566537bd2d037e1eff919542731b93fa85cDoris Liu 121666d48566537bd2d037e1eff919542731b93fa85cDoris Liu mAnimationEndRequested = false; 1217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase initAnimation(); 12183618d30f8ab6018025b11869676b309c3b4961cfDoris Liu mRunning = true; 1219fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu if (mSeekFraction >= 0) { 1220fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu mOverallFraction = mSeekFraction; 1221fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } else { 1222fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu mOverallFraction = 0f; 1223fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } 12243618d30f8ab6018025b11869676b309c3b4961cfDoris Liu if (mListeners != null) { 122517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase notifyStartListeners(); 1226a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1227a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1228caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu 1229caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu /** 1230caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu * Internal only: This tracks whether the animation has gotten on the animation loop. Note 1231caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu * this is different than {@link #isRunning()} in that the latter tracks the time after start() 1232caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu * is called (or after start delay if any), which may be before the animation loop starts. 1233caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu */ 1234caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu private boolean isPulsingInternal() { 123513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu return mLastFrameTime >= 0; 1236caa2ebf814d0c0a4ff29e7f7a7ee8ca46df08802Doris Liu } 1237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1239fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase * Returns the name of this animator for debugging purposes. 1240fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase */ 1241fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase String getNameForTrace() { 1242fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase return "animator"; 1243fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase } 1244fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase 1245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1246c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * Applies an adjustment to the animation to compensate for jank between when 1247c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown * the animation first ran and when the frame was drawn. 12483618d30f8ab6018025b11869676b309c3b4961cfDoris Liu * @hide 1249c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown */ 12503618d30f8ab6018025b11869676b309c3b4961cfDoris Liu public void commitAnimationFrame(long frameTime) { 1251c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown if (!mStartTimeCommitted) { 1252c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown mStartTimeCommitted = true; 12533618d30f8ab6018025b11869676b309c3b4961cfDoris Liu long adjustment = frameTime - mLastFrameTime; 12543618d30f8ab6018025b11869676b309c3b4961cfDoris Liu if (adjustment > 0) { 1255c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown mStartTime += adjustment; 1256c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown if (DEBUG) { 1257c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown Log.d(TAG, "Adjusted start time by " + adjustment + " ms: " + toString()); 1258c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown } 1259c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown } 1260c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown } 1261c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown } 1262c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown 1263c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown /** 1264a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This internal function processes a single animation frame for a given animation. The 1265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * currentTime parameter is the timing pulse sent by the handler, used to calculate the 1266a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * elapsed duration, and therefore 1267a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the elapsed fraction, of the animation. The return value indicates whether the animation 1268a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be ended (which happens when the elapsed time of the animation exceeds the 1269a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation's duration, including the repeatCount). 1270a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1271a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param currentTime The current time, as tracked by the static timing handler 1272a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return true if the animation's duration, including any repetitions due to 12733618d30f8ab6018025b11869676b309c3b4961cfDoris Liu * <code>repeatCount</code> has been exceeded and the animation should be ended. 1274a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 12753618d30f8ab6018025b11869676b309c3b4961cfDoris Liu boolean animateBasedOnTime(long currentTime) { 1276a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase boolean done = false; 12773618d30f8ab6018025b11869676b309c3b4961cfDoris Liu if (mRunning) { 127856b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu final long scaledDuration = getScaledDuration(); 127956b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu final float fraction = scaledDuration > 0 ? 128056b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu (float)(currentTime - mStartTime) / scaledDuration : 1f; 1281fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu final float lastFraction = mOverallFraction; 1282fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu final boolean newIteration = (int) fraction > (int) lastFraction; 1283fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu final boolean lastIterationFinished = (fraction >= mRepeatCount + 1) && 1284fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu (mRepeatCount != INFINITE); 128556b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu if (scaledDuration == 0) { 128656b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu // 0 duration animator, ignore the repeat count and skip to the end 128756b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu done = true; 128856b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu } else if (newIteration && !lastIterationFinished) { 1289fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu // Time to repeat 1290fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu if (mListeners != null) { 1291fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu int numListeners = mListeners.size(); 1292fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu for (int i = 0; i < numListeners; ++i) { 1293fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu mListeners.get(i).onAnimationRepeat(this); 1294a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1295a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1296fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu } else if (lastIterationFinished) { 1297fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu done = true; 1298a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1299fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu mOverallFraction = clampFraction(fraction); 130013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu float currentIterationFraction = getCurrentIterationFraction( 130113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mOverallFraction, mReversing); 1302fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu animateValue(currentIterationFraction); 1303a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1304a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return done; 1305a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1306a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1307a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 130813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * Internal use only. 130913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * 131013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * This method does not modify any fields of the animation. It should be called when seeking 131113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * in an AnimatorSet. When the last play time and current play time are of different repeat 131213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * iterations, 131313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * {@link android.view.animation.Animation.AnimationListener#onAnimationRepeat(Animation)} 131413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * will be called. 131513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu */ 131613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu @Override 131713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu void animateBasedOnPlayTime(long currentPlayTime, long lastPlayTime, boolean inReverse) { 131813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (currentPlayTime < 0 || lastPlayTime < 0) { 131913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu throw new UnsupportedOperationException("Error: Play time should never be negative."); 132013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 132113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 132213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu initAnimation(); 132313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // Check whether repeat callback is needed only when repeat count is non-zero 132413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (mRepeatCount > 0) { 132513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu int iteration = (int) (currentPlayTime / mDuration); 132613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu int lastIteration = (int) (lastPlayTime / mDuration); 132713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 132813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // Clamp iteration to [0, mRepeatCount] 132913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu iteration = Math.min(iteration, mRepeatCount); 133013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu lastIteration = Math.min(lastIteration, mRepeatCount); 133113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 133213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (iteration != lastIteration) { 133313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (mListeners != null) { 133413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu int numListeners = mListeners.size(); 133513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu for (int i = 0; i < numListeners; ++i) { 133613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mListeners.get(i).onAnimationRepeat(this); 133713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 133813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 133913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 134013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 134113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 134213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (mRepeatCount != INFINITE && currentPlayTime >= (mRepeatCount + 1) * mDuration) { 134313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu skipToEndValue(inReverse); 134413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } else { 134513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // Find the current fraction: 134613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu float fraction = currentPlayTime / (float) mDuration; 134713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu fraction = getCurrentIterationFraction(fraction, inReverse); 134813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu animateValue(fraction); 134913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 135013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 135113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 135213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu /** 135313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * Internal use only. 135413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * Skips the animation value to end/start, depending on whether the play direction is forward 135513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * or backward. 135613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * 135713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * @param inReverse whether the end value is based on a reverse direction. If yes, this is 135813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu * equivalent to skip to start value in a forward playing direction. 135913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu */ 136013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu void skipToEndValue(boolean inReverse) { 136113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu initAnimation(); 136213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu float endFraction = inReverse ? 0f : 1f; 136313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (mRepeatCount % 2 == 1 && mRepeatMode == REVERSE) { 136413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // This would end on fraction = 0 136513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu endFraction = 0f; 136613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 136713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu animateValue(endFraction); 136813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 136913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 137013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu /** 137120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * Processes a frame of the animation, adjusting the start time if needed. 137220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * 137320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * @param frameTime The frame time. 137420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * @return true if the animation has ended. 13753618d30f8ab6018025b11869676b309c3b4961cfDoris Liu * @hide 137620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown */ 137713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu public final boolean doAnimationFrame(long frameTime) { 137813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (!mRunning && mStartTime < 0) { 137913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // First frame during delay 138013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mStartTime = frameTime + mStartDelay; 138113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 138213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 138313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // Handle pause/resume 138413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (mPaused) { 138513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mPauseTime = frameTime; 138613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu removeAnimationCallback(); 138713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu return false; 138813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } else if (mResumed) { 138913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mResumed = false; 139013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (mPauseTime > 0) { 139113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // Offset by the duration that the animation was paused 139213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mStartTime += (frameTime - mPauseTime); 139313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 139413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 139513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 139613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (!mRunning) { 139713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // If not running, that means the animation is in the start delay phase. In the case of 139813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // reversing, we want to run start delay in the end. 139913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (mStartTime > frameTime) { 140013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // During start delay 140113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu return false; 140213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } else { 140313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu // Start delay has passed. 140413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu mRunning = true; 140513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 140613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 140713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 140813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (mLastFrameTime < 0) { 14093618d30f8ab6018025b11869676b309c3b4961cfDoris Liu // First frame 1410f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu if (mStartDelay > 0) { 1411f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu startAnimation(); 1412f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu } 14130d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase if (mSeekFraction < 0) { 141420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown mStartTime = frameTime; 141520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } else { 1416fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu long seekTime = (long) (getScaledDuration() * mSeekFraction); 14170d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase mStartTime = frameTime - seekTime; 14180d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase mSeekFraction = -1; 141920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } 1420c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown mStartTimeCommitted = false; // allow start time to be compensated for jank 142120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } 1422f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu mLastFrameTime = frameTime; 142320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown // The frame time might be before the start time during the first frame of 142420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown // an animation. The "current time" must always be on or after the start 142520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown // time to avoid animating frames at negative time intervals. In practice, this 142620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown // is very rare and only happens when seeking backwards. 142720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown final long currentTime = Math.max(frameTime, mStartTime); 14283618d30f8ab6018025b11869676b309c3b4961cfDoris Liu boolean finished = animateBasedOnTime(currentTime); 14293618d30f8ab6018025b11869676b309c3b4961cfDoris Liu 14303618d30f8ab6018025b11869676b309c3b4961cfDoris Liu if (finished) { 14313618d30f8ab6018025b11869676b309c3b4961cfDoris Liu endAnimation(); 14323618d30f8ab6018025b11869676b309c3b4961cfDoris Liu } 143313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu return finished; 143413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 143513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 143613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu private void addOneShotCommitCallback() { 143713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (!mSelfPulse) { 143813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu return; 143913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 144013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu AnimationHandler handler = AnimationHandler.getInstance(); 144113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu handler.addOneShotCommitCallback(this); 144213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 144313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 144413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu private void removeAnimationCallback() { 144513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (!mSelfPulse) { 144613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu return; 144713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 144813351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu AnimationHandler handler = AnimationHandler.getInstance(); 144913351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu handler.removeCallback(this); 145013351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 145113351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu 145213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu private void addAnimationCallback(long delay) { 145313351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu if (!mSelfPulse) { 145413351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu return; 145513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu } 145613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu AnimationHandler handler = AnimationHandler.getInstance(); 145713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu handler.addAnimationFrameCallback(this, delay); 145820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } 145920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown 146020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown /** 1461a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * Returns the current animation fraction, which is the elapsed/interpolated fraction used in 1462a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * the most recent frame update on the animation. 1463a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * 1464a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * @return Elapsed/interpolated fraction of the animation. 1465a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase */ 1466a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase public float getAnimatedFraction() { 1467a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase return mCurrentFraction; 1468a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase } 1469a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase 1470a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase /** 1471a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This method is called with the elapsed fraction of the animation during every 1472a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation frame. This function turns the elapsed fraction into an interpolated fraction 1473a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * and then into an animated value (from the evaluator. The function is called mostly during 1474a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation updates, but it is also called when the <code>end()</code> 1475a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function is called, to set the final value on the property. 1476a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1477a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Overrides of this method must call the superclass to perform the calculation 1478a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of the animated value.</p> 1479a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1480a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param fraction The elapsed fraction of the animation. 1481a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1482c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye @CallSuper 1483a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase void animateValue(float fraction) { 1484a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase fraction = mInterpolator.getInterpolation(fraction); 1485a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase mCurrentFraction = fraction; 1486a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = mValues.length; 1487a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 1488a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues[i].calculateValue(fraction); 1489a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1490a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners != null) { 1491a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numListeners = mUpdateListeners.size(); 1492a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numListeners; ++i) { 1493a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners.get(i).onAnimationUpdate(this); 1494a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1495a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1496a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1497a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1498a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 1499a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public ValueAnimator clone() { 1500a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase final ValueAnimator anim = (ValueAnimator) super.clone(); 1501a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners != null) { 1502d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(mUpdateListeners); 1503a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 15040d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase anim.mSeekFraction = -1; 1505f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase anim.mReversing = false; 1506a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mInitialized = false; 150726e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mStarted = false; 150826e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mRunning = false; 150926e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mPaused = false; 151026e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mResumed = false; 151126e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mStartListenersCalled = false; 151213351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu anim.mStartTime = -1; 1513e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui anim.mStartTimeCommitted = false; 15143dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu anim.mAnimationEndRequested = false; 151513351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu anim.mPauseTime = -1; 151613351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu anim.mLastFrameTime = -1; 151713351997aa36eb53e5ff0fcee3f5e3da83787278Doris Liu anim.mFirstFrameTime = -1; 1518fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu anim.mOverallFraction = 0; 1519e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui anim.mCurrentFraction = 0; 152026e9a19900bae56b012425a114685d42dfa2fde1ztenghui 1521a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder[] oldValues = mValues; 1522a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (oldValues != null) { 1523a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = oldValues.length; 1524a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mValues = new PropertyValuesHolder[numValues]; 1525a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); 1526a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 1527d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase PropertyValuesHolder newValuesHolder = oldValues[i].clone(); 1528d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase anim.mValues[i] = newValuesHolder; 1529d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder); 1530a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1531a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1532a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return anim; 1533a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1534a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1535a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1536a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Implementors of this interface can add themselves as update listeners 1537a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to an <code>ValueAnimator</code> instance to receive callbacks on every animation 1538a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frame, after the current frame's values have been calculated for that 1539a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>ValueAnimator</code>. 1540a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1541a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static interface AnimatorUpdateListener { 1542a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1543a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Notifies the occurrence of another frame of the animation.</p> 1544a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param animation The animation which was repeated. 1546a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1547a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase void onAnimationUpdate(ValueAnimator animation); 1548a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1549a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1550599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick 1551599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick /** 1552599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * Return the number of animations currently running. 1553599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * 15549c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * Used by StrictMode internally to annotate violations. 15559c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * May be called on arbitrary threads! 1556599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * 1557599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * @hide 1558599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick */ 1559599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick public static int getCurrentAnimationsCount() { 15603618d30f8ab6018025b11869676b309c3b4961cfDoris Liu return AnimationHandler.getAnimationCount(); 15618901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy } 1562e9140a72b1059574046a624b471b2c3a35806496Chet Haase 1563e9140a72b1059574046a624b471b2c3a35806496Chet Haase @Override 1564e9140a72b1059574046a624b471b2c3a35806496Chet Haase public String toString() { 1565e9140a72b1059574046a624b471b2c3a35806496Chet Haase String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode()); 1566e9140a72b1059574046a624b471b2c3a35806496Chet Haase if (mValues != null) { 1567e9140a72b1059574046a624b471b2c3a35806496Chet Haase for (int i = 0; i < mValues.length; ++i) { 1568e9140a72b1059574046a624b471b2c3a35806496Chet Haase returnVal += "\n " + mValues[i].toString(); 1569e9140a72b1059574046a624b471b2c3a35806496Chet Haase } 1570e9140a72b1059574046a624b471b2c3a35806496Chet Haase } 1571e9140a72b1059574046a624b471b2c3a35806496Chet Haase return returnVal; 1572e9140a72b1059574046a624b471b2c3a35806496Chet Haase } 1573d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck 1574d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck /** 1575d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <p>Whether or not the ValueAnimator is allowed to run asynchronously off of 1576d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * the UI thread. This is a hint that informs the ValueAnimator that it is 1577d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * OK to run the animation off-thread, however ValueAnimator may decide 1578d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * that it must run the animation on the UI thread anyway. For example if there 1579d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * is an {@link AnimatorUpdateListener} the animation will run on the UI thread, 1580d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * regardless of the value of this hint.</p> 1581d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * 1582d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <p>Regardless of whether or not the animation runs asynchronously, all 1583d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * listener callbacks will be called on the UI thread.</p> 1584d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * 1585d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <p>To be able to use this hint the following must be true:</p> 1586d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <ol> 1587d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <li>{@link #getAnimatedFraction()} is not needed (it will return undefined values).</li> 1588d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <li>The animator is immutable while {@link #isStarted()} is true. Requests 1589d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * to change values, duration, delay, etc... may be ignored.</li> 1590d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <li>Lifecycle callback events may be asynchronous. Events such as 1591d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or 1592d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed 1593d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * as they must be posted back to the UI thread, and any actions performed 1594d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * by those callbacks (such as starting new animations) will not happen 1595d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * in the same frame.</li> 1596d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...) 1597d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * may be asynchronous. It is guaranteed that all state changes that are 1598d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * performed on the UI thread in the same frame will be applied as a single 1599d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * atomic update, however that frame may be the current frame, 1600d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * the next frame, or some future frame. This will also impact the observed 1601d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * state of the Animator. For example, {@link #isStarted()} may still return true 1602d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * after a call to {@link #end()}. Using the lifecycle callbacks is preferred over 1603d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()} 1604d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * for this reason.</li> 1605d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * </ol> 1606d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * @hide 1607d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck */ 1608c01bd1167a1b08d59557f214ddc48cf24d3b8d0aJohn Reck @Override 1609d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck public void setAllowRunningAsynchronously(boolean mayRunAsync) { 1610d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck // It is up to subclasses to support this, if they can. 1611d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck } 1612599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick} 1613