ValueAnimator.java revision 26e9a19900bae56b012425a114685d42dfa2fde1
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; 20a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Looper; 2118772ea228f3d292629c4f0b5f6392d047e0530dRomain Guyimport android.os.Trace; 222970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haaseimport android.util.AndroidRuntimeException; 2396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brownimport android.view.Choreographer; 24a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator; 25a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AnimationUtils; 2627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haaseimport android.view.animation.LinearInterpolator; 27a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.ArrayList; 29a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.HashMap; 30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 31a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/** 32a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class provides a simple timing engine for running animations 33a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * which calculate animated values and set them on target objects. 34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 35a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There is a single timing pulse that all animations use. It runs in a 36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * custom handler to ensure that property changes happen on the UI thread.</p> 37a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 38a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>By default, ValueAnimator uses non-linear time interpolation, via the 39a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates 40a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * out of an animation. This behavior can be changed by calling 41e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p> 423aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * 43d430753cba09acb07af8b313286f247c78a41a32Chet Haase * <p>Animators can be created from either code or resource files. Here is an example 44d430753cba09acb07af8b313286f247c78a41a32Chet Haase * of a ValueAnimator resource file:</p> 45d430753cba09acb07af8b313286f247c78a41a32Chet Haase * 46d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/animator.xml ValueAnimatorResources} 47d430753cba09acb07af8b313286f247c78a41a32Chet Haase * 48d430753cba09acb07af8b313286f247c78a41a32Chet Haase * <p>It is also possible to use a combination of {@link PropertyValuesHolder} and 49d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@link Keyframe} resource tags to create a multi-step animation. 50d430753cba09acb07af8b313286f247c78a41a32Chet Haase * Note that you can specify explicit fractional values (from 0 to 1) for 51d430753cba09acb07af8b313286f247c78a41a32Chet Haase * each keyframe to determine when, in the overall duration, the animation should arrive at that 52d430753cba09acb07af8b313286f247c78a41a32Chet Haase * value. Alternatively, you can leave the fractions off and the keyframes will be equally 53d430753cba09acb07af8b313286f247c78a41a32Chet Haase * distributed within the total duration:</p> 54d430753cba09acb07af8b313286f247c78a41a32Chet Haase * 55d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/value_animator_pvh_kf.xml 56d430753cba09acb07af8b313286f247c78a41a32Chet Haase * ValueAnimatorKeyframeResources} 57d430753cba09acb07af8b313286f247c78a41a32Chet Haase * 583aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference"> 593aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3> 603aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about animating with {@code ValueAnimator}, read the 613aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property 623aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * Animation</a> developer guide.</p> 633aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div> 64a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 6518772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy@SuppressWarnings("unchecked") 662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haasepublic class ValueAnimator extends Animator { 67a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 68a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 69a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Internal constants 70a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 71d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase private static float sDurationScale = 1.0f; 72a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 73a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 74a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Values used with internal variable mPlayingState to indicate the current state of an 75a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation. 76a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 77051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase static final int STOPPED = 0; // Not yet playing 78051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase static final int RUNNING = 1; // Playing normally 79051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase static final int SEEKED = 2; // Seeked to some time value 80a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 81a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 82a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Internal variables 83a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * NOTE: This object implements the clone() method, making a deep copy of any referenced 84a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * objects. As other non-trivial fields are added to this class, make sure to add logic 85a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to clone() to make deep copies of them. 86a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 87a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 88a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The first time that the animation's animateFrame() method is called. This time is used to 89a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // determine elapsed time (and therefore the elapsed fraction) in subsequent calls 90a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // to animateFrame() 91051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase long mStartTime; 92a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 93a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 94a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked 95a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to a value. 96a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 970d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase float mSeekFraction = -1; 98a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 998aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase /** 1008aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * Set on the next frame after pause() is called, used to calculate a new startTime 1018aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * or delayStartTime which allows the animator to continue from the point at which 1028aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * it was paused. If negative, has not yet been set. 1038aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase */ 1048aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase private long mPauseTime; 1058aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase 1068aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase /** 1078aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * Set when an animator is resumed. This triggers logic in the next frame which 1088aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * actually resumes the animator. 1098aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase */ 1108aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase private boolean mResumed = false; 1118aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase 1128aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase 113a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The static sAnimationHandler processes the internal timing loop on which all animations 114a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // are based 115be19e030a14c8e398e8af97fa898ea80187704dfChet Haase /** 116be19e030a14c8e398e8af97fa898ea80187704dfChet Haase * @hide 117be19e030a14c8e398e8af97fa898ea80187704dfChet Haase */ 118be19e030a14c8e398e8af97fa898ea80187704dfChet Haase protected static ThreadLocal<AnimationHandler> sAnimationHandler = 119e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase new ThreadLocal<AnimationHandler>(); 120e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase 121a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The time interpolator to be used if none is set on the animation 122e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase private static final TimeInterpolator sDefaultInterpolator = 123e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase new AccelerateDecelerateInterpolator(); 124a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 125a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 126a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Used to indicate whether the animation is currently playing in reverse. This causes the 127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * elapsed fraction to be inverted to calculate the appropriate values. 128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private boolean mPlayingBackwards = false; 130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 132f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * Flag to indicate whether this animator is playing in reverse mode, specifically 133f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * by being started or interrupted by a call to reverse(). This flag is different than 134f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * mPlayingBackwards, which indicates merely whether the current iteration of the 135f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * animator is playing in reverse. It is used in corner cases to determine proper end 136f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * behavior. 137f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase */ 138f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase private boolean mReversing; 139f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase 140f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase /** 141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the 142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * repeatCount (if repeatCount!=INFINITE), the animation ends 143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private int mCurrentIteration = 0; 145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 147a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction(). 148a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase */ 149a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase private float mCurrentFraction = 0f; 150a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase 151a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase /** 152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Tracks whether a startDelay'd animation has begun playing through the startDelay. 153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 154a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private boolean mStartedDelay = false; 155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Tracks the time at which the animation began playing through its startDelay. This is 158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * different from the mStartTime variable, which is used to track when the animation became 159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * active (which is when the startDelay expired and the animation was added to the active 160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animations list). 161a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 162a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private long mDelayStartTime; 163a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 164a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 165a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Flag that represents the current state of the animation. Used to figure out when to start 166a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * an animation (if state == STOPPED). Also used to end an animation that 167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * has been cancel()'d or end()'d since the last animation frame. Possible values are 168e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase * STOPPED, RUNNING, SEEKED. 169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 170051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase int mPlayingState = STOPPED; 171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 172a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 173b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * Additional playing state to indicate whether an animator has been start()'d. There is 174b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * some lag between a call to start() and the first animation frame. We should still note 175b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * that the animation has been started, even if it's first animation frame has not yet 176b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * happened, and reflect that state in isRunning(). 177b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * Note that delayed animations are different: they are not started until their first 178b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * animation frame, which occurs after their delay elapses. 179b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase */ 1808b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase private boolean mRunning = false; 1818b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase 1828b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase /** 1838b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase * Additional playing state to indicate whether an animator has been start()'d, whether or 1848b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase * not there is a nonzero startDelay. 1858b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase */ 186b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase private boolean mStarted = false; 187b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase 188b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase /** 1898aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * Tracks whether we've notified listeners of the onAnimationStart() event. This can be 19017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase * complex to keep track of since we notify listeners at different times depending on 19117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase * startDelay and whether start() was called before end(). 19217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase */ 19317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase private boolean mStartListenersCalled = false; 19417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase 19517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase /** 196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Flag that denotes whether the animation is set up and ready to go. Used to 197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * set up animation that has not yet been started. 198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase boolean mInitialized = false; 200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // 202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Backing variables 203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // 204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // How long the animation should last in ms 206c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase private long mDuration = (long)(300 * sDurationScale); 207d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase private long mUnscaledDuration = 300; 208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The amount of time in ms to delay starting the animation after start() is called 210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private long mStartDelay = 0; 211d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase private long mUnscaledStartDelay = 0; 212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The number of times the animation will repeat. The default is 0, which means the animation 214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // will play only once 215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private int mRepeatCount = 0; 216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The type of repetition that will occur when repeatMode is nonzero. RESTART means the 219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation will start from the beginning on every new cycle. REVERSE means the animation 220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will reverse directions on each iteration. 221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private int mRepeatMode = RESTART; 223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The time interpolator to be used. The elapsed fraction of the animation will be passed 226a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * through this interpolator to calculate the interpolated fraction, which is then used to 227a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * calculate the animated values. 228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 229e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase private TimeInterpolator mInterpolator = sDefaultInterpolator; 230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The set of listeners to be sent events through the life of an animation. 233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 234d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck ArrayList<AnimatorUpdateListener> mUpdateListeners = null; 235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The property/value sets being animated. 238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder[] mValues; 240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 242a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values 243a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * by property name during calls to getAnimatedValue(String). 244a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase HashMap<String, PropertyValuesHolder> mValuesMap; 246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Public constants 249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * or a positive value, the animation restarts from the beginning. 254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 255a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static final int RESTART = 1; 256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * or a positive value, the animation reverses direction on every iteration. 259a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static final int REVERSE = 2; 261a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 262a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This value used used with the {@link #setRepeatCount(int)} property to repeat 263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the animation indefinitely. 264a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static final int INFINITE = -1; 266a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 267c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase 268c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase /** 269c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase * @hide 270c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase */ 271c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase public static void setDurationScale(float durationScale) { 272c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase sDurationScale = durationScale; 273c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase } 274c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase 275a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 276ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown * @hide 277ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown */ 278ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown public static float getDurationScale() { 279ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown return sDurationScale; 280ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown } 281ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown 282ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown /** 283a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Creates a new ValueAnimator object. This default constructor is primarily for 2842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * use internally; the factory methods which take parameters are more generally 285a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * useful. 286a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 287a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public ValueAnimator() { 288a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 289a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 290a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 2912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between int values. A single 2922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 2932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 2942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 2952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 2962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 297a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 2982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 2992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 30041f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase */ 3012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofInt(int... values) { 3022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 3032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setIntValues(values); 3042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 3052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 3072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3081ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * Constructs and returns a ValueAnimator that animates between color values. A single 3091ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * value implies that that value is the one being animated to. However, this is not typically 3101ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * useful in a ValueAnimator object because there is no way for the object to determine the 3111ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * starting value for the animation (unlike ObjectAnimator, which can derive that value 3121ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * from the target object and property being animated). Therefore, there should typically 3131ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * be two or more values. 3141ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * 3151ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * @param values A set of values that the animation will animate between over time. 3161ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount * @return A ValueAnimator object that is set up to animate between the given values. 3171ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount */ 3181ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount public static ValueAnimator ofArgb(int... values) { 3191ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount ValueAnimator anim = new ValueAnimator(); 3201ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount anim.setIntValues(values); 3211ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount anim.setEvaluator(ArgbEvaluator.getInstance()); 3221ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount return anim; 3231ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount } 3241ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount 3251ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount /** 3262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between float values. A single 3272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 3282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 3292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 3302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 3312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 3322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 3342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 3352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofFloat(float... values) { 3372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 3382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setFloatValues(values); 3392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 3402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 3422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between the values 3442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * specified in the PropertyValuesHolder objects. 3452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of PropertyValuesHolder objects whose values will be animated 3472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * between over time. 3482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 3492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) { 3512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 3522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setValues(values); 3532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 3542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between Object values. A single 3572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 3582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 3592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 3602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 3612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 3622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this 3642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * factory method also takes a TypeEvaluator object that the ValueAnimator will use 3652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * to perform that interpolation. 3662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param evaluator A TypeEvaluator that will be called on each animation frame to 3682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * provide the ncessry interpolation between the Object values to derive the animated 3692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value. 3702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 3712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 3722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) { 3742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 3752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setObjectValues(values); 3762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setEvaluator(evaluator); 3772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 3782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 3802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets int values that will be animated between. A single 3822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 3832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 3842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 3852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 3862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 3872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>If there are already multiple sets of values defined for this ValueAnimator via more 3892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * than one PropertyValuesHolder object, this method will set the values for the first 3902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * of those objects.</p> 3912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 3932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public void setIntValues(int... values) { 3952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (values == null || values.length == 0) { 3962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return; 3972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (mValues == null || mValues.length == 0) { 39918772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy setValues(PropertyValuesHolder.ofInt("", values)); 4002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } else { 4012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase PropertyValuesHolder valuesHolder = mValues[0]; 4022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase valuesHolder.setIntValues(values); 4032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase // New property/values/target should cause re-initialization prior to starting 4052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase mInitialized = false; 4062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 4082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 4092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets float values that will be animated between. A single 4102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 4112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 4122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 4132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 4142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 4152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>If there are already multiple sets of values defined for this ValueAnimator via more 4172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * than one PropertyValuesHolder object, this method will set the values for the first 4182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * of those objects.</p> 4192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 4212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 4222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public void setFloatValues(float... values) { 4232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (values == null || values.length == 0) { 4242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return; 42541f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase } 4262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (mValues == null || mValues.length == 0) { 42718772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy setValues(PropertyValuesHolder.ofFloat("", values)); 4282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } else { 4292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase PropertyValuesHolder valuesHolder = mValues[0]; 4302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase valuesHolder.setFloatValues(values); 4312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase // New property/values/target should cause re-initialization prior to starting 4332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase mInitialized = false; 4342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 4362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 4372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets the values to animate between for this animation. A single 4382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 4392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 4402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 4412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 4422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 4432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>If there are already multiple sets of values defined for this ValueAnimator via more 4452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * than one PropertyValuesHolder object, this method will set the values for the first 4462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * of those objects.</p> 4472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate 4492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * between these value objects. ValueAnimator only knows how to interpolate between the 4502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * primitive types specified in the other setValues() methods.</p> 4512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 4522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values The set of values to animate between. 4532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 4542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public void setObjectValues(Object... values) { 4552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (values == null || values.length == 0) { 4562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return; 4572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (mValues == null || mValues.length == 0) { 45918772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy setValues(PropertyValuesHolder.ofObject("", null, values)); 4602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } else { 4612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase PropertyValuesHolder valuesHolder = mValues[0]; 4622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase valuesHolder.setObjectValues(values); 4632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 4642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase // New property/values/target should cause re-initialization prior to starting 4652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase mInitialized = false; 466a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 467a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 468a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 469a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets the values, per property, being animated between. This function is called internally 470f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can 471a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * be constructed without values and this method can be called to set the values manually 472a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * instead. 473a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 474a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param values The set of values, per property, being animated between. 475a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 476a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setValues(PropertyValuesHolder... values) { 477a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = values.length; 478a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues = values; 479a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); 480a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 48118772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy PropertyValuesHolder valuesHolder = values[i]; 482a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder); 483a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 4840e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase // New property/values/target should cause re-initialization prior to starting 4850e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase mInitialized = false; 486a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 487a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 488a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 489a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Returns the values that this ValueAnimator animates between. These values are stored in 490a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list 491a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of value objects instead. 492a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 493a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the 494a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * values, per property, that define the animation. 495a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 496a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public PropertyValuesHolder[] getValues() { 497a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mValues; 498a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 499a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 500a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 501a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This function is called immediately before processing the first animation 502a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frame of an animation. If there is a nonzero <code>startDelay</code>, the 503a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function is called after that delay ends. 504a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * It takes care of the final initialization steps for the 505a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation. 506a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 507a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Overrides of this method should call the superclass method to ensure 508a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * that internal mechanisms for the animation are set up correctly.</p> 509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 510c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye @CallSuper 511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase void initAnimation() { 512a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (!mInitialized) { 513a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = mValues.length; 514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 515a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues[i].init(); 516a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 517a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mInitialized = true; 518a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 519a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 520a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 521a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 522a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 5232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets the length of the animation. The default duration is 300 milliseconds. 524a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 52527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * @param duration The length of the animation, in milliseconds. This value cannot 52627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * be negative. 5272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return ValueAnimator The object called with setDuration(). This return 5282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value makes it easier to compose statements together that construct and then set the 5292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>. 530a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 5312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public ValueAnimator setDuration(long duration) { 53227c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase if (duration < 0) { 53327c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase throw new IllegalArgumentException("Animators cannot have negative duration: " + 53427c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase duration); 53527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase } 536d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase mUnscaledDuration = duration; 5377a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown updateScaledDuration(); 5382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return this; 539a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 540a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 5417a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown private void updateScaledDuration() { 5427a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown mDuration = (long)(mUnscaledDuration * sDurationScale); 5437a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown } 5447a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown 545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 5462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Gets the length of the animation. The default duration is 300 milliseconds. 547a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 548a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return The length of the animation, in milliseconds. 549a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 550a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public long getDuration() { 551d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase return mUnscaledDuration; 552a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 553a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 554a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 555a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets the position of the animation to the specified point in time. This time should 556a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * be between 0 and the total duration of the animation, including any repetition. If 557a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the animation has not yet been started, then it will not advance forward after it is 558a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * set to this time; it will simply set the time to this value and perform any appropriate 559a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * actions based on that time. If the animation is already running, then setCurrentPlayTime() 560a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will set the current playing time to this value and continue playing from that point. 561a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 562a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param playTime The time, in milliseconds, to which the animation is advanced or rewound. 563a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 564a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setCurrentPlayTime(long playTime) { 5655a25e5bae223bbee56dab75e36d1d947c8c3cb11Chet Haase float fraction = mUnscaledDuration > 0 ? (float) playTime / mUnscaledDuration : 1; 5660d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase setCurrentFraction(fraction); 5670d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase } 5680d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase 5690d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase /** 5700d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * Sets the position of the animation to the specified fraction. This fraction should 5710d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * be between 0 and the total fraction of the animation, including any repetition. That is, 5720d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * a fraction of 0 will position the animation at the beginning, a value of 1 at the end, 573f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * and a value of 2 at the end of a reversing animator that repeats once. If 5740d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * the animation has not yet been started, then it will not advance forward after it is 5750d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * set to this fraction; it will simply set the fraction to this value and perform any 5760d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * appropriate actions based on that fraction. If the animation is already running, then 5770d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * setCurrentFraction() will set the current fraction to this value and continue 5785a5afe010d2f8fb7e8f00858b8a305b4745c0469Eino-Ville Talvala * playing from that point. {@link Animator.AnimatorListener} events are not called 579f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * due to changing the fraction; those events are only processed while the animation 580f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * is running. 5810d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * 582f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * @param fraction The fraction to which the animation is advanced or rewound. Values 583f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * outside the range of 0 to the maximum fraction for the animator will be clamped to 584f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase * the correct range. 5850d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase */ 5860d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase public void setCurrentFraction(float fraction) { 587a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase initAnimation(); 588f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (fraction < 0) { 589f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase fraction = 0; 590f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 591f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase int iteration = (int) fraction; 592f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (fraction == 1) { 593f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase iteration -= 1; 594f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } else if (fraction > 1) { 595f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (iteration < (mRepeatCount + 1) || mRepeatCount == INFINITE) { 596f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (mRepeatMode == REVERSE) { 597f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mPlayingBackwards = (iteration % 2) != 0; 598f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 599f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase fraction = fraction % 1f; 600f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } else { 601f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase fraction = 1; 602f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase iteration -= 1; 603f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 604f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } else { 605f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mPlayingBackwards = mReversing; 606f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 607f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mCurrentIteration = iteration; 608f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase long seekTime = (long) (mDuration * fraction); 609f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 610f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mStartTime = currentTime - seekTime; 611a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mPlayingState != RUNNING) { 6120d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase mSeekFraction = fraction; 613a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mPlayingState = SEEKED; 614a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 615f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (mPlayingBackwards) { 616f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase fraction = 1f - fraction; 617f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 6180d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase animateValue(fraction); 619a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 620a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 621a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 622a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Gets the current position of the animation in time, which is equal to the current 623a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * time minus the time that the animation started. An animation that is not yet started will 624a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * return a value of zero. 625a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 626a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return The current position in time of the animation. 627a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 628a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public long getCurrentPlayTime() { 629a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (!mInitialized || mPlayingState == STOPPED) { 630a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return 0; 631a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 632a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return AnimationUtils.currentAnimationTimeMillis() - mStartTime; 633a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 634a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 635a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 636a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This custom, static handler handles the timing pulse that is shared by 637a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * all active animations. This approach ensures that the setting of animation 638a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * values will happen on the UI thread and that all animations will share 639a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the same times for calculating their values, which makes synchronizing 640a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animations possible. 641a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 64220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * The handler uses the Choreographer for executing periodic callbacks. 643be19e030a14c8e398e8af97fa898ea80187704dfChet Haase * 644be19e030a14c8e398e8af97fa898ea80187704dfChet Haase * @hide 645a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 64618772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy @SuppressWarnings("unchecked") 647be19e030a14c8e398e8af97fa898ea80187704dfChet Haase protected static class AnimationHandler implements Runnable { 6489c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown // The per-thread list of all active animations 649be19e030a14c8e398e8af97fa898ea80187704dfChet Haase /** @hide */ 650be19e030a14c8e398e8af97fa898ea80187704dfChet Haase protected final ArrayList<ValueAnimator> mAnimations = new ArrayList<ValueAnimator>(); 6519c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown 6522936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase // Used in doAnimationFrame() to avoid concurrent modifications of mAnimations 6532936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase private final ArrayList<ValueAnimator> mTmpAnimations = new ArrayList<ValueAnimator>(); 6542936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase 6559c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown // The per-thread set of animations to be started on the next animation frame 656be19e030a14c8e398e8af97fa898ea80187704dfChet Haase /** @hide */ 657be19e030a14c8e398e8af97fa898ea80187704dfChet Haase protected final ArrayList<ValueAnimator> mPendingAnimations = new ArrayList<ValueAnimator>(); 6589c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown 6599c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown /** 6609c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * Internal per-thread collections used to avoid set collisions as animations start and end 6619c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * while being processed. 662be19e030a14c8e398e8af97fa898ea80187704dfChet Haase * @hide 6639c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown */ 664be19e030a14c8e398e8af97fa898ea80187704dfChet Haase protected final ArrayList<ValueAnimator> mDelayedAnims = new ArrayList<ValueAnimator>(); 6659c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private final ArrayList<ValueAnimator> mEndingAnims = new ArrayList<ValueAnimator>(); 6669c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private final ArrayList<ValueAnimator> mReadyAnims = new ArrayList<ValueAnimator>(); 6679c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown 66896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown private final Choreographer mChoreographer; 6694a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown private boolean mAnimationScheduled; 67096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 67196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown private AnimationHandler() { 67296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mChoreographer = Choreographer.getInstance(); 67396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 67496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 675a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 67620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * Start animating on the next frame. 677a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 67820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown public void start() { 67920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown scheduleAnimation(); 68096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 681a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 68220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown private void doAnimationFrame(long frameTime) { 68396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // mPendingAnimations holds any animations that have requested to be started 68496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // We're going to clear mPendingAnimations, but starting animation may 68596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // cause more to be added to the pending list (for example, if one animation 68696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // starting triggers another starting). So we loop until mPendingAnimations 68796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // is empty. 68896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown while (mPendingAnimations.size() > 0) { 68996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ArrayList<ValueAnimator> pendingCopy = 69096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown (ArrayList<ValueAnimator>) mPendingAnimations.clone(); 69196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mPendingAnimations.clear(); 69296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown int count = pendingCopy.size(); 69396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown for (int i = 0; i < count; ++i) { 69496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ValueAnimator anim = pendingCopy.get(i); 69596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // If the animation has a startDelay, place it on the delayed list 69696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown if (anim.mStartDelay == 0) { 69796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown anim.startAnimation(this); 69896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } else { 69996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mDelayedAnims.add(anim); 700a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 70196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 70296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 703c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase // Next, process animations currently sitting on the delayed queue, adding 70496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // them to the active animations if they are ready 70596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown int numDelayedAnims = mDelayedAnims.size(); 70696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown for (int i = 0; i < numDelayedAnims; ++i) { 70796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ValueAnimator anim = mDelayedAnims.get(i); 70820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown if (anim.delayedAnimationFrame(frameTime)) { 70996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mReadyAnims.add(anim); 71096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 71196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 71296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown int numReadyAnims = mReadyAnims.size(); 71396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown if (numReadyAnims > 0) { 71496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown for (int i = 0; i < numReadyAnims; ++i) { 71596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ValueAnimator anim = mReadyAnims.get(i); 71696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown anim.startAnimation(this); 71796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown anim.mRunning = true; 71896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mDelayedAnims.remove(anim); 71996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 72096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mReadyAnims.clear(); 72196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 72296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 72396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // Now process all active animations. The return value from animationFrame() 72496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // tells the handler whether it should now be ended 72596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown int numAnims = mAnimations.size(); 7262936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase for (int i = 0; i < numAnims; ++i) { 7272936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase mTmpAnimations.add(mAnimations.get(i)); 7282936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase } 7292936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase for (int i = 0; i < numAnims; ++i) { 7302936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase ValueAnimator anim = mTmpAnimations.get(i); 7312936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase if (mAnimations.contains(anim) && anim.doAnimationFrame(frameTime)) { 73296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mEndingAnims.add(anim); 73396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 73496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 7352936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase mTmpAnimations.clear(); 73696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown if (mEndingAnims.size() > 0) { 7372936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase for (int i = 0; i < mEndingAnims.size(); ++i) { 73896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mEndingAnims.get(i).endAnimation(this); 73996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 74096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mEndingAnims.clear(); 74196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 74296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 74396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // If there are still active or delayed animations, schedule a future call to 74496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // onAnimate to process the next frame of the animations. 74520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown if (!mAnimations.isEmpty() || !mDelayedAnims.isEmpty()) { 74620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown scheduleAnimation(); 747a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 748a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 74996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 7504a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown // Called by the Choreographer. 75196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown @Override 7524a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown public void run() { 7534a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown mAnimationScheduled = false; 75420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown doAnimationFrame(mChoreographer.getFrameTime()); 75520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } 75620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown 75720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown private void scheduleAnimation() { 75820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown if (!mAnimationScheduled) { 75920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null); 76020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown mAnimationScheduled = true; 76120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } 76296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 763a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 765a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 766a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, to delay starting the animation after 767a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link #start()} is called. 768a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 769a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return the number of milliseconds to delay running the animation 770a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 771a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public long getStartDelay() { 772d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase return mUnscaledStartDelay; 773a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 774a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 775a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 776a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, to delay starting the animation after 777a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link #start()} is called. 778a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 779a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param startDelay The amount of the delay, in milliseconds 780a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 781a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setStartDelay(long startDelay) { 782d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase this.mStartDelay = (long)(startDelay * sDurationScale); 783d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase mUnscaledStartDelay = startDelay; 784a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 785a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 786a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 787a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 788a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * requested time that the animation will attempt to honor, but the actual delay between 789a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frames may be different, depending on system load and capabilities. This is a static 790a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function because the same delay will be applied to all animations, since they are all 791a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * run off of a single timing loop. 792a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 79396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * The frame delay may be ignored when the animation system uses an external timing 79496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * source, such as the display refresh rate (vsync), to govern animations. 79596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * 796a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return the requested time between frames, in milliseconds 797a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 798a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static long getFrameDelay() { 79996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown return Choreographer.getFrameDelay(); 800a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 801a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 802a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 803a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 804a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * requested time that the animation will attempt to honor, but the actual delay between 805a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frames may be different, depending on system load and capabilities. This is a static 806a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function because the same delay will be applied to all animations, since they are all 807a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * run off of a single timing loop. 808a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 80996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * The frame delay may be ignored when the animation system uses an external timing 81096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * source, such as the display refresh rate (vsync), to govern animations. 81196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * 812a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param frameDelay the requested time between frames, in milliseconds 813a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 814a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static void setFrameDelay(long frameDelay) { 81596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown Choreographer.setFrameDelay(frameDelay); 816a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 817a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 818a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 819a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The most recent value calculated by this <code>ValueAnimator</code> when there is just one 820a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * property being animated. This value is only sensible while the animation is running. The main 821a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code> 822a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which 823a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is called during each animation frame, immediately after the value is calculated. 824a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 825a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for 826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the single property being animated. If there are several properties being animated 827a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * (specified by several PropertyValuesHolder objects in the constructor), this function 828a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * returns the animated value for the first of those objects. 829a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 830a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public Object getAnimatedValue() { 831a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mValues != null && mValues.length > 0) { 832a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mValues[0].getAnimatedValue(); 833a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 834a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Shouldn't get here; should always have values unless ValueAnimator was set up wrong 835a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return null; 836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 839a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>. 840a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The main purpose for this read-only property is to retrieve the value from the 841a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>ValueAnimator</code> during a call to 842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which 843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is called during each animation frame, immediately after the value is calculated. 844a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 845a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return animatedValue The value most recently calculated for the named property 846a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * by this <code>ValueAnimator</code>. 847a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 848a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public Object getAnimatedValue(String propertyName) { 849a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName); 850a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (valuesHolder != null) { 851a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return valuesHolder.getAnimatedValue(); 852a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 853a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // At least avoid crashing if called with bogus propertyName 854a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return null; 855a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 856a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 857a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 858a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 859a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets how many times the animation should be repeated. If the repeat 860a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * count is 0, the animation is never repeated. If the repeat count is 861a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * greater than 0 or {@link #INFINITE}, the repeat mode will be taken 862a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * into account. The repeat count is 0 by default. 863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param value the number of times the animation should be repeated 865a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 866a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setRepeatCount(int value) { 867a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mRepeatCount = value; 868a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 869a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 870a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Defines how many times the animation should repeat. The default value 871a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is 0. 872a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 873a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return the number of times the animation should repeat, or {@link #INFINITE} 874a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 875a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public int getRepeatCount() { 876a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mRepeatCount; 877a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 878a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 879a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 880a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Defines what this animation should do when it reaches the end. This 881a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * setting is applied only when the repeat count is either greater than 882a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. 883a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 884a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param value {@link #RESTART} or {@link #REVERSE} 885a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 886a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setRepeatMode(int value) { 887a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mRepeatMode = value; 888a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 889a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 891a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Defines what this animation should do when it reaches the end. 892a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 893a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return either one of {@link #REVERSE} or {@link #RESTART} 894a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 895a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public int getRepeatMode() { 896a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mRepeatMode; 897a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 898a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 899a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 900a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Adds a listener to the set of listeners that are sent update events through the life of 901a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * an animation. This method is called on all listeners for every frame of the animation, 902a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * after the values for the animation have been calculated. 903a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 904a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param listener the listener to be added to the current set of listeners for this animation. 905a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 906a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void addUpdateListener(AnimatorUpdateListener listener) { 907a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners == null) { 908a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); 909a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 910a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners.add(listener); 911a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 912a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 913a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 9143060421045d4d9e411797f91bb509824b03e33fbJim Miller * Removes all listeners from the set listening to frame updates for this animation. 9153060421045d4d9e411797f91bb509824b03e33fbJim Miller */ 9163060421045d4d9e411797f91bb509824b03e33fbJim Miller public void removeAllUpdateListeners() { 9173060421045d4d9e411797f91bb509824b03e33fbJim Miller if (mUpdateListeners == null) { 9183060421045d4d9e411797f91bb509824b03e33fbJim Miller return; 9193060421045d4d9e411797f91bb509824b03e33fbJim Miller } 9203060421045d4d9e411797f91bb509824b03e33fbJim Miller mUpdateListeners.clear(); 9213060421045d4d9e411797f91bb509824b03e33fbJim Miller mUpdateListeners = null; 9223060421045d4d9e411797f91bb509824b03e33fbJim Miller } 9233060421045d4d9e411797f91bb509824b03e33fbJim Miller 9243060421045d4d9e411797f91bb509824b03e33fbJim Miller /** 925a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Removes a listener from the set listening to frame updates for this animation. 926a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 927a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param listener the listener to be removed from the current set of update listeners 928a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * for this animation. 929a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 930a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void removeUpdateListener(AnimatorUpdateListener listener) { 931a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners == null) { 932a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return; 933a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 934a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners.remove(listener); 935a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners.size() == 0) { 936a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners = null; 937a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 938a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 939a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 940a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 941a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 942a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The time interpolator used in calculating the elapsed fraction of this animation. The 943a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * interpolator determines whether the animation runs with linear or non-linear motion, 944a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * such as acceleration and deceleration. The default value is 945a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link android.view.animation.AccelerateDecelerateInterpolator} 946a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 94727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * @param value the interpolator to be used by this animation. A value of <code>null</code> 94827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * will result in linear interpolation. 949a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 950a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 951e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase public void setInterpolator(TimeInterpolator value) { 952a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (value != null) { 953a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mInterpolator = value; 95427c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase } else { 95527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase mInterpolator = new LinearInterpolator(); 956a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 957a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 958a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 959a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 960a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Returns the timing interpolator that this ValueAnimator uses. 961a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 962a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return The timing interpolator for this ValueAnimator. 963a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 964430742f09063574271e6c4091de13b9b9e762514Chet Haase @Override 965e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase public TimeInterpolator getInterpolator() { 966a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mInterpolator; 967a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 968a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 969a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 970a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The type evaluator to be used when calculating the animated values of this animation. 971b2ab04ffb6894f399d5c9ceb15f64eb17b654426Chet Haase * The system will automatically assign a float or int evaluator based on the type 972a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values 973a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * are not one of these primitive types, or if different evaluation is desired (such as is 974a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * necessary with int values that represent colors), a custom evaluator needs to be assigned. 97553ee3316bcb3590ff156b3fd7108903c0817c35dChet Haase * For example, when running an animation on color values, the {@link ArgbEvaluator} 976a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be used to get correct RGB color interpolation. 977a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 978a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>If this ValueAnimator has only one set of values being animated between, this evaluator 979a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will be used for that set. If there are several sets of values being animated, which is 980fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase * the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator 981a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is assigned just to the first PropertyValuesHolder object.</p> 982a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 983a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param value the evaluator to be used this animation 984a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 985a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setEvaluator(TypeEvaluator value) { 986a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (value != null && mValues != null && mValues.length > 0) { 987a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues[0].setEvaluator(value); 988a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 989a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 990a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 99117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase private void notifyStartListeners() { 99217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase if (mListeners != null && !mStartListenersCalled) { 99317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase ArrayList<AnimatorListener> tmpListeners = 99417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 99517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase int numListeners = tmpListeners.size(); 99617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase for (int i = 0; i < numListeners; ++i) { 99717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase tmpListeners.get(i).onAnimationStart(this); 99817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase } 99917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase } 100017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase mStartListenersCalled = true; 100117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase } 100217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase 1003a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1004a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Start the animation playing. This version of start() takes a boolean flag that indicates 1005a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * whether the animation should play in reverse. The flag is usually false, but may be set 10062970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * to true if called from the reverse() method. 10072970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * 10082970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * <p>The animation started by calling this method will be run on the thread that called 10092970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * this method. This thread should have a Looper on it (a runtime exception will be thrown if 10102970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * this is not the case). Also, if the animation will animate 10112970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * properties of objects in the view hierarchy, then the calling thread should be the UI 10122970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * thread for that view hierarchy.</p> 1013a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1014a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param playBackwards Whether the ValueAnimator should start playing in reverse. 1015a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1016a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private void start(boolean playBackwards) { 10172970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase if (Looper.myLooper() == null) { 10182970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase throw new AndroidRuntimeException("Animators may only be run on Looper threads"); 10193060421045d4d9e411797f91bb509824b03e33fbJim Miller } 1020f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mReversing = playBackwards; 10212970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase mPlayingBackwards = playBackwards; 1022f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (playBackwards && mSeekFraction != -1) { 1023f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (mSeekFraction == 0 && mCurrentIteration == 0) { 1024f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase // special case: reversing from seek-to-0 should act as if not seeked at all 1025f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mSeekFraction = 0; 1026f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } else if (mRepeatCount == INFINITE) { 1027f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mSeekFraction = 1 - (mSeekFraction % 1); 1028f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } else { 1029f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mSeekFraction = 1 + mRepeatCount - (mCurrentIteration + mSeekFraction); 1030f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 1031f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mCurrentIteration = (int) mSeekFraction; 1032f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mSeekFraction = mSeekFraction % 1; 1033f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 1034f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (mCurrentIteration > 0 && mRepeatMode == REVERSE && 1035f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase (mCurrentIteration < (mRepeatCount + 1) || mRepeatCount == INFINITE)) { 1036f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase // if we were seeked to some other iteration in a reversing animator, 1037f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase // figure out the correct direction to start playing based on the iteration 1038f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (playBackwards) { 1039f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mPlayingBackwards = (mCurrentIteration % 2) == 0; 1040f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } else { 1041f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mPlayingBackwards = (mCurrentIteration % 2) != 0; 1042f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 1043f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 10440d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase int prevPlayingState = mPlayingState; 1045add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase mPlayingState = STOPPED; 10468b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase mStarted = true; 1047add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase mStartedDelay = false; 10488aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mPaused = false; 10497a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown updateScaledDuration(); // in case the scale factor has changed since creation time 10509c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler animationHandler = getOrCreateAnimationHandler(); 10519c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown animationHandler.mPendingAnimations.add(this); 10522970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase if (mStartDelay == 0) { 1053add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase // This sets the initial value of the animation, prior to actually starting it running 10540d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase if (prevPlayingState != SEEKED) { 10550d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase setCurrentPlayTime(0); 10560d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase } 1057154f14508a11627d5a995b6fe2a14a83d794a6feChet Haase mPlayingState = STOPPED; 10588b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase mRunning = true; 105917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase notifyStartListeners(); 1060a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 106120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown animationHandler.start(); 1062a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1063a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1064a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 1065a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void start() { 1066a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase start(false); 1067a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1068a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1069a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 1070a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void cancel() { 10712970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase // Only cancel if the animation is actually running or has been started and is about 10722970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase // to run 10739c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = getOrCreateAnimationHandler(); 10749c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown if (mPlayingState != STOPPED 10759c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown || handler.mPendingAnimations.contains(this) 10769c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown || handler.mDelayedAnims.contains(this)) { 1077b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase // Only notify listeners if the animator has actually started 107817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase if ((mStarted || mRunning) && mListeners != null) { 107917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase if (!mRunning) { 108017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase // If it's not yet running, then start listeners weren't called. Call them now. 108117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase notifyStartListeners(); 108217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase } 10837dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase ArrayList<AnimatorListener> tmpListeners = 10847dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 10857dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase for (AnimatorListener listener : tmpListeners) { 10867dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase listener.onAnimationCancel(this); 10877dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase } 10887dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase } 10899c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown endAnimation(handler); 10902970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase } 1091a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1092a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1093a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 1094a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void end() { 10959c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = getOrCreateAnimationHandler(); 10969c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown if (!handler.mAnimations.contains(this) && !handler.mPendingAnimations.contains(this)) { 1097a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Special case if the animation has not yet started; get it ready for ending 1098a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartedDelay = false; 10999c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown startAnimation(handler); 110017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase mStarted = true; 1101add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase } else if (!mInitialized) { 1102add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase initAnimation(); 1103e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase } 11044dd176864310e1d9519bf6b88918913e9927984fChet Haase animateValue(mPlayingBackwards ? 0f : 1f); 11059c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown endAnimation(handler); 1106a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1108a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 11098aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase public void resume() { 11108aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase if (mPaused) { 11118aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mResumed = true; 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() { 11288b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase return (mPlayingState == RUNNING || 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() { 1145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mPlayingBackwards = !mPlayingBackwards; 1146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mPlayingState == RUNNING) { 1147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 1148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long currentPlayTime = currentTime - mStartTime; 1149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long timeLeft = mDuration - currentPlayTime; 1150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartTime = currentTime - timeLeft; 1151f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mReversing = !mReversing; 1152f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase } else if (mStarted) { 1153f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase end(); 1154a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 1155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase start(true); 1156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 11607bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui * @hide 11617bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui */ 11627bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui @Override 11637bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui public boolean canReverse() { 11647bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui return true; 11657bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui } 11667bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui 11677bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui /** 1168a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Called internally to end an animation by removing it from the animations list. Must be 1169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * called on the UI thread. 1170d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * @hide 1171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1172d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck protected void endAnimation(AnimationHandler handler) { 11739c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mAnimations.remove(this); 11749c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mPendingAnimations.remove(this); 11759c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mDelayedAnims.remove(this); 1176a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mPlayingState = STOPPED; 11778aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mPaused = false; 117817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase if ((mStarted || mRunning) && mListeners != null) { 117917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase if (!mRunning) { 118017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase // If it's not yet running, then start listeners weren't called. Call them now. 118117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase notifyStartListeners(); 118217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase } 1183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorListener> tmpListeners = 1184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 11857c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = tmpListeners.size(); 11867c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 11877c608f25d494c8a0a671e7373efbb47ca635367eChet Haase tmpListeners.get(i).onAnimationEnd(this); 1188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 11908b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase mRunning = false; 1191b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase mStarted = false; 119217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase mStartListenersCalled = false; 1193caf46486f54cdc899e383dfc776ec33a81b089a1Chet Haase mPlayingBackwards = false; 1194f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mReversing = false; 1195f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mCurrentIteration = 0; 11969b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { 11979b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(), 11989b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase System.identityHashCode(this)); 11999b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase } 1200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Called internally to start an animation by adding it to the active animations list. Must be 1204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * called on the UI thread. 1205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 12069c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private void startAnimation(AnimationHandler handler) { 12079b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { 12089b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(), 12099b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase System.identityHashCode(this)); 12109b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase } 1211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase initAnimation(); 12129c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mAnimations.add(this); 1213b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase if (mStartDelay > 0 && mListeners != null) { 1214b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase // Listeners were already notified in start() if startDelay is 0; this is 1215b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase // just for delayed animations 121617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase notifyStartListeners(); 1217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1221fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase * Returns the name of this animator for debugging purposes. 1222fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase */ 1223fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase String getNameForTrace() { 1224fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase return "animator"; 1225fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase } 1226fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase 1227fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase 1228fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase /** 1229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Internal function called to process an animation frame on an animation that is currently 1230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * sleeping through its <code>startDelay</code> phase. The return value indicates whether it 1231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be woken up and put on the active animations queue. 1232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param currentTime The current animation time, used to calculate whether the animation 1234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * has exceeded its <code>startDelay</code> and should be started. 1235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return True if the animation's <code>startDelay</code> has been exceeded and the animation 1236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be added to the set of active animations. 1237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private boolean delayedAnimationFrame(long currentTime) { 1239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (!mStartedDelay) { 1240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartedDelay = true; 1241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mDelayStartTime = currentTime; 12422ed16ac6238227dab2687519268f3683f045e2acGeorge Mount } 12432ed16ac6238227dab2687519268f3683f045e2acGeorge Mount if (mPaused) { 12442ed16ac6238227dab2687519268f3683f045e2acGeorge Mount if (mPauseTime < 0) { 12452ed16ac6238227dab2687519268f3683f045e2acGeorge Mount mPauseTime = currentTime; 12468aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase } 12472ed16ac6238227dab2687519268f3683f045e2acGeorge Mount return false; 12482ed16ac6238227dab2687519268f3683f045e2acGeorge Mount } else if (mResumed) { 12492ed16ac6238227dab2687519268f3683f045e2acGeorge Mount mResumed = false; 12502ed16ac6238227dab2687519268f3683f045e2acGeorge Mount if (mPauseTime > 0) { 12512ed16ac6238227dab2687519268f3683f045e2acGeorge Mount // Offset by the duration that the animation was paused 12522ed16ac6238227dab2687519268f3683f045e2acGeorge Mount mDelayStartTime += (currentTime - mPauseTime); 1253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 12552ed16ac6238227dab2687519268f3683f045e2acGeorge Mount long deltaTime = currentTime - mDelayStartTime; 12562ed16ac6238227dab2687519268f3683f045e2acGeorge Mount if (deltaTime > mStartDelay) { 12572ed16ac6238227dab2687519268f3683f045e2acGeorge Mount // startDelay ended - start the anim and record the 12582ed16ac6238227dab2687519268f3683f045e2acGeorge Mount // mStartTime appropriately 12592ed16ac6238227dab2687519268f3683f045e2acGeorge Mount mStartTime = currentTime - (deltaTime - mStartDelay); 12602ed16ac6238227dab2687519268f3683f045e2acGeorge Mount mPlayingState = RUNNING; 12612ed16ac6238227dab2687519268f3683f045e2acGeorge Mount return true; 12622ed16ac6238227dab2687519268f3683f045e2acGeorge Mount } 1263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return false; 1264a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1266a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1267a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This internal function processes a single animation frame for a given animation. The 1268a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * currentTime parameter is the timing pulse sent by the handler, used to calculate the 1269a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * elapsed duration, and therefore 1270a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the elapsed fraction, of the animation. The return value indicates whether the animation 1271a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be ended (which happens when the elapsed time of the animation exceeds the 1272a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation's duration, including the repeatCount). 1273a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1274a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param currentTime The current time, as tracked by the static timing handler 1275a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return true if the animation's duration, including any repetitions due to 12768aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase * <code>repeatCount</code>, has been exceeded and the animation should be ended. 1277a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1278051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase boolean animationFrame(long currentTime) { 1279a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase boolean done = false; 1280a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase switch (mPlayingState) { 1281a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase case RUNNING: 1282a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase case SEEKED: 128370d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f; 1284f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (mDuration == 0 && mRepeatCount != INFINITE) { 1285f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase // Skip to the end 1286f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mCurrentIteration = mRepeatCount; 1287f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase if (!mReversing) { 1288f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mPlayingBackwards = false; 1289f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 1290f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase } 1291a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (fraction >= 1f) { 1292d15e94f0309a91d5a75d03a9ae165121e7f24907Chet Haase if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) { 1293a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Time to repeat 1294a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mListeners != null) { 12957c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = mListeners.size(); 12967c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 12977c608f25d494c8a0a671e7373efbb47ca635367eChet Haase mListeners.get(i).onAnimationRepeat(this); 1298a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1299a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1300a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mRepeatMode == REVERSE) { 130118772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy mPlayingBackwards = !mPlayingBackwards; 1302a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1303f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase mCurrentIteration += (int) fraction; 1304730666858692ea396f5ad779654b5d86ff90b6caChet Haase fraction = fraction % 1f; 1305a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartTime += mDuration; 1306a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 1307a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase done = true; 1308a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase fraction = Math.min(fraction, 1.0f); 1309a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1310a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1311a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mPlayingBackwards) { 1312a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase fraction = 1f - fraction; 1313a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1314a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase animateValue(fraction); 1315a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase break; 1316a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1317a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1318a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return done; 1319a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1320a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1321a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 132220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * Processes a frame of the animation, adjusting the start time if needed. 132320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * 132420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * @param frameTime The frame time. 132520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown * @return true if the animation has ended. 132620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown */ 132720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown final boolean doAnimationFrame(long frameTime) { 132820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown if (mPlayingState == STOPPED) { 132920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown mPlayingState = RUNNING; 13300d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase if (mSeekFraction < 0) { 133120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown mStartTime = frameTime; 133220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } else { 13330d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase long seekTime = (long) (mDuration * mSeekFraction); 13340d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase mStartTime = frameTime - seekTime; 13350d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase mSeekFraction = -1; 133620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } 133720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } 13388aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase if (mPaused) { 13398aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase if (mPauseTime < 0) { 13408aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mPauseTime = frameTime; 13418aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase } 13428aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase return false; 13438aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase } else if (mResumed) { 13448aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mResumed = false; 13458aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase if (mPauseTime > 0) { 13468aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase // Offset by the duration that the animation was paused 13478aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase mStartTime += (frameTime - mPauseTime); 13488aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase } 13498aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase } 135020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown // The frame time might be before the start time during the first frame of 135120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown // an animation. The "current time" must always be on or after the start 135220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown // time to avoid animating frames at negative time intervals. In practice, this 135320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown // is very rare and only happens when seeking backwards. 135420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown final long currentTime = Math.max(frameTime, mStartTime); 135520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown return animationFrame(currentTime); 135620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown } 135720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown 135820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown /** 1359a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * Returns the current animation fraction, which is the elapsed/interpolated fraction used in 1360a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * the most recent frame update on the animation. 1361a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * 1362a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * @return Elapsed/interpolated fraction of the animation. 1363a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase */ 1364a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase public float getAnimatedFraction() { 1365a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase return mCurrentFraction; 1366a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase } 1367a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase 1368a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase /** 1369a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This method is called with the elapsed fraction of the animation during every 1370a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation frame. This function turns the elapsed fraction into an interpolated fraction 1371a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * and then into an animated value (from the evaluator. The function is called mostly during 1372a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation updates, but it is also called when the <code>end()</code> 1373a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function is called, to set the final value on the property. 1374a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1375a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Overrides of this method must call the superclass to perform the calculation 1376a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of the animated value.</p> 1377a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1378a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param fraction The elapsed fraction of the animation. 1379a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1380c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye @CallSuper 1381a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase void animateValue(float fraction) { 1382a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase fraction = mInterpolator.getInterpolation(fraction); 1383a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase mCurrentFraction = fraction; 1384a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = mValues.length; 1385a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 1386a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues[i].calculateValue(fraction); 1387a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1388a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners != null) { 1389a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numListeners = mUpdateListeners.size(); 1390a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numListeners; ++i) { 1391a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners.get(i).onAnimationUpdate(this); 1392a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1393a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1394a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1395a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1396a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 1397a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public ValueAnimator clone() { 1398a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase final ValueAnimator anim = (ValueAnimator) super.clone(); 1399a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners != null) { 1400d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(mUpdateListeners); 1401a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 14020d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase anim.mSeekFraction = -1; 1403a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mPlayingBackwards = false; 1404f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase anim.mReversing = false; 1405a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mCurrentIteration = 0; 1406a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mInitialized = false; 1407a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mPlayingState = STOPPED; 1408a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mStartedDelay = false; 140926e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mStarted = false; 141026e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mRunning = false; 141126e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mPaused = false; 141226e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mResumed = false; 141326e9a19900bae56b012425a114685d42dfa2fde1ztenghui anim.mStartListenersCalled = false; 141426e9a19900bae56b012425a114685d42dfa2fde1ztenghui 1415a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder[] oldValues = mValues; 1416a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (oldValues != null) { 1417a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = oldValues.length; 1418a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mValues = new PropertyValuesHolder[numValues]; 1419a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); 1420a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 1421d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase PropertyValuesHolder newValuesHolder = oldValues[i].clone(); 1422d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase anim.mValues[i] = newValuesHolder; 1423d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder); 1424a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1425a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1426a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return anim; 1427a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1428a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1429a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1430a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Implementors of this interface can add themselves as update listeners 1431a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to an <code>ValueAnimator</code> instance to receive callbacks on every animation 1432a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frame, after the current frame's values have been calculated for that 1433a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>ValueAnimator</code>. 1434a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1435a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static interface AnimatorUpdateListener { 1436a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1437a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Notifies the occurrence of another frame of the animation.</p> 1438a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1439a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param animation The animation which was repeated. 1440a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1441a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase void onAnimationUpdate(ValueAnimator animation); 1442a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1443a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1444599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick 1445599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick /** 1446599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * Return the number of animations currently running. 1447599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * 14489c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * Used by StrictMode internally to annotate violations. 14499c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * May be called on arbitrary threads! 1450599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * 1451599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * @hide 1452599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick */ 1453599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick public static int getCurrentAnimationsCount() { 14549c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = sAnimationHandler.get(); 14559c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown return handler != null ? handler.mAnimations.size() : 0; 1456599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick } 14578901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy 14588901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy /** 14598901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy * Clear all animations on this thread, without canceling or ending them. 14608901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy * This should be used with caution. 14618901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy * 14628901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy * @hide 14638901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy */ 14648901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy public static void clearAllAnimations() { 14659c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = sAnimationHandler.get(); 14669c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown if (handler != null) { 14679c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mAnimations.clear(); 14689c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mPendingAnimations.clear(); 14699c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mDelayedAnims.clear(); 14709c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown } 14719c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown } 14729c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown 147318772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy private static AnimationHandler getOrCreateAnimationHandler() { 14749c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = sAnimationHandler.get(); 14759c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown if (handler == null) { 14769c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler = new AnimationHandler(); 14779c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown sAnimationHandler.set(handler); 14789c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown } 14799c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown return handler; 14808901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy } 1481e9140a72b1059574046a624b471b2c3a35806496Chet Haase 1482e9140a72b1059574046a624b471b2c3a35806496Chet Haase @Override 1483e9140a72b1059574046a624b471b2c3a35806496Chet Haase public String toString() { 1484e9140a72b1059574046a624b471b2c3a35806496Chet Haase String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode()); 1485e9140a72b1059574046a624b471b2c3a35806496Chet Haase if (mValues != null) { 1486e9140a72b1059574046a624b471b2c3a35806496Chet Haase for (int i = 0; i < mValues.length; ++i) { 1487e9140a72b1059574046a624b471b2c3a35806496Chet Haase returnVal += "\n " + mValues[i].toString(); 1488e9140a72b1059574046a624b471b2c3a35806496Chet Haase } 1489e9140a72b1059574046a624b471b2c3a35806496Chet Haase } 1490e9140a72b1059574046a624b471b2c3a35806496Chet Haase return returnVal; 1491e9140a72b1059574046a624b471b2c3a35806496Chet Haase } 1492d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck 1493d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck /** 1494d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <p>Whether or not the ValueAnimator is allowed to run asynchronously off of 1495d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * the UI thread. This is a hint that informs the ValueAnimator that it is 1496d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * OK to run the animation off-thread, however ValueAnimator may decide 1497d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * that it must run the animation on the UI thread anyway. For example if there 1498d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * is an {@link AnimatorUpdateListener} the animation will run on the UI thread, 1499d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * regardless of the value of this hint.</p> 1500d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * 1501d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <p>Regardless of whether or not the animation runs asynchronously, all 1502d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * listener callbacks will be called on the UI thread.</p> 1503d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * 1504d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <p>To be able to use this hint the following must be true:</p> 1505d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <ol> 1506d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <li>{@link #getAnimatedFraction()} is not needed (it will return undefined values).</li> 1507d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <li>The animator is immutable while {@link #isStarted()} is true. Requests 1508d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * to change values, duration, delay, etc... may be ignored.</li> 1509d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <li>Lifecycle callback events may be asynchronous. Events such as 1510d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or 1511d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed 1512d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * as they must be posted back to the UI thread, and any actions performed 1513d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * by those callbacks (such as starting new animations) will not happen 1514d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * in the same frame.</li> 1515d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...) 1516d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * may be asynchronous. It is guaranteed that all state changes that are 1517d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * performed on the UI thread in the same frame will be applied as a single 1518d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * atomic update, however that frame may be the current frame, 1519d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * the next frame, or some future frame. This will also impact the observed 1520d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * state of the Animator. For example, {@link #isStarted()} may still return true 1521d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * after a call to {@link #end()}. Using the lifecycle callbacks is preferred over 1522d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()} 1523d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * for this reason.</li> 1524d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * </ol> 1525d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck * @hide 1526d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck */ 1527c01bd1167a1b08d59557f214ddc48cf24d3b8d0aJohn Reck @Override 1528d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck public void setAllowRunningAsynchronously(boolean mayRunAsync) { 1529d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck // It is up to subclasses to support this, if they can. 1530d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck } 1531599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick} 1532