Class ValueAnimator
- All Implemented Interfaces:
AnimationHandler.FrameCallback
,Cloneable
- Direct Known Subclasses:
ObjectAnimator
There is a single timing pulse that all animations use. It runs in a custom handler to ensure that property changes happen on the UI thread.
By default, ValueAnimator uses non-linear time interpolation, via the
TimeInterpolator.ACCELERATE_DECELERATE
, which accelerates into and decelerates
out of an animation. This behavior can be changed by calling
setInterpolator(TimeInterpolator)
.
Animators can be created from either code or resource files. Here is an example of a ValueAnimator resource file:
Starting from API 23, it is also possible to use a combination of PropertyValuesHolder
and Keyframe
resource tags to create a multi-step animation.
Note that you can specify explicit fractional values (from 0 to 1) for
each keyframe to determine when, in the overall duration, the animation should arrive at that
value. Alternatively, you can leave the fractions off and the keyframes will be equally
distributed within the total duration:
Developer Guides
For more information about animating with ValueAnimator
, read the
Property
Animation developer guide.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Implementors of this interface can add themselves as update listeners to anValueAnimator
instance to receive callbacks on every animation frame, after the current frame's values have been calculated for thatValueAnimator
.static @interface
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
This value used with thesetRepeatCount(int)
property to repeat the animation indefinitely.static final int
When the animation reaches the end andrepeatCount
is INFINITE or a positive value, the animation restarts from the beginning.static final int
When the animation reaches the end andrepeatCount
is INFINITE or a positive value, the animation reverses direction on every iteration.static float
Internal usage, global config value.Fields inherited from class icyllis.modernui.animation.Animator
DURATION_INFINITE, MARKER
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a listener to the set of listeners that are sent update events through the life of an animation.void
cancel()
Cancels the animation.boolean
clone()
void
commitAnimationFrame
(long frameTime) Applies an adjustment to the animation to compensate for jank between when the animation first ran and when the frame was drawn.final boolean
doAnimationFrame
(long frameTime) Processes a frame of the animation, adjusting the start time if needed.void
end()
Ends the animation.float
Returns the current animation fraction, which is the elapsed/interpolated fraction used in the most recent frame update on the animation.The most recent value calculated by thisValueAnimator
when there is just one property being animated.long
Gets the current position of the animation in time, which is equal to the current time minus the time that the animation started.long
Gets the length of the animation.Returns the timing interpolator that this animation uses.int
Defines how many times the animation should repeat.int
Defines what this animation should do when it reaches the end.long
The amount of time, in milliseconds, to delay starting the animation afterstart()
is called.long
Gets the total duration of the animation, accounting for animation sequences, start delay, and repeating.Returns the values that this ValueAnimator animates between.boolean
Returns whether this Animator is currently running (having been started and gone past any initial startDelay period and not yet ended).boolean
Returns whether this Animator has been started and not yet ended.static ValueAnimator
ofArgb
(int... values) Constructs and returns a ValueAnimator that animates between color values.static ValueAnimator
ofFloat
(float... values) Constructs and returns a ValueAnimator that animates between float values.static ValueAnimator
ofInt
(int... values) Constructs and returns a ValueAnimator that animates between int values.static <V> ValueAnimator
ofObject
(TypeEvaluator<V> evaluator, V... values) Constructs and returns a ValueAnimator that animates between Object values.void
pause()
Pauses a running animation.void
Removes all listeners from the set listening to frame updates for this animation.void
Removes a listener from the set listening to frame updates for this animation.void
resume()
Resumes a paused animation, causing the animator to pick up where it left off when it was paused.void
reverse()
Plays the ValueAnimator in reverse.void
setCurrentFraction
(float fraction) Sets the position of the animation to the specified fraction.void
setCurrentPlayTime
(long playTime) Sets the position of the animation to the specified point in time.setDuration
(long duration) Sets the length of the animation.void
setEvaluator
(TypeEvaluator<?> value) The type evaluator to be used when calculating the animated values of this animation.void
setInterpolator
(TimeInterpolator value) The time interpolator used in calculating the elapsed fraction of the animation.void
setRepeatCount
(int value) Sets how many times the animation should be repeated.void
setRepeatMode
(int value) Defines what this animation should do when it reaches the end.void
setStartDelay
(long startDelay) The amount of time, in milliseconds, to delay processing the animation afterAnimator.start()
is called.void
setValues
(PropertyValuesHolder... values) Sets the values, per property, being animated between.void
start()
Starts this animation.Methods inherited from class icyllis.modernui.animation.Animator
addListener, getListeners, isPaused, removeAllListeners, removeListener, setTarget, setupEndValues, setupStartValues
-
Field Details
-
sDurationScale
@Internal public static volatile float sDurationScaleInternal usage, global config value. -
RESTART
public static final int RESTARTWhen the animation reaches the end andrepeatCount
is INFINITE or a positive value, the animation restarts from the beginning.- See Also:
-
REVERSE
public static final int REVERSEWhen the animation reaches the end andrepeatCount
is INFINITE or a positive value, the animation reverses direction on every iteration.- See Also:
-
INFINITE
public static final int INFINITEThis value used with thesetRepeatCount(int)
property to repeat the animation indefinitely.- See Also:
-
-
Constructor Details
-
ValueAnimator
public ValueAnimator()Creates a new ValueAnimator object. This default constructor is primarily for use internally; the factory methods which take parameters are more generally useful.
-
-
Method Details
-
ofInt
Constructs and returns a ValueAnimator that animates between int values. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more values.- Parameters:
values
- A set of values that the animation will animate between over time.- Returns:
- A ValueAnimator object that is set up to animate between the given values.
-
ofArgb
Constructs and returns a ValueAnimator that animates between color values. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more values.- Parameters:
values
- A set of values that the animation will animate between over time.- Returns:
- A ValueAnimator object that is set up to animate between the given values.
-
ofFloat
Constructs and returns a ValueAnimator that animates between float values. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more values.- Parameters:
values
- A set of values that the animation will animate between over time.- Returns:
- A ValueAnimator object that is set up to animate between the given values.
-
ofObject
@Nonnull @SafeVarargs public static <V> ValueAnimator ofObject(@Nonnull TypeEvaluator<V> evaluator, @Nonnull V... values) Constructs and returns a ValueAnimator that animates between Object values. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more values.Note: The Object values are stored as references to the original objects, which means that changes to those objects after this method is called will affect the values on the animator. If the objects will be mutated externally after this method is called, callers should pass a copy of those objects instead.
Since ValueAnimator does not know how to animate between arbitrary Objects, this factory method also takes a TypeEvaluator object that the ValueAnimator will use to perform that interpolation.
- Parameters:
evaluator
- A TypeEvaluator that will be called on each animation frame to provide the necessary interpolation between the Object values to derive the animated value.values
- A set of values that the animation will animate between over time.- Returns:
- A ValueAnimator object that is set up to animate between the given values.
-
setValues
Sets the values, per property, being animated between. This function is called internally by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can be constructed without values and this method can be called to set the values manually instead.- Parameters:
values
- The set of values, per property, being animated between.
-
getValues
Returns the values that this ValueAnimator animates between. These values are stored in PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list of value objects instead.- Returns:
- The array of PropertyValuesHolder objects which hold the values, per property, that define the animation.
-
setDuration
Sets the length of the animation. The default duration is 300 milliseconds.- Specified by:
setDuration
in classAnimator
- Parameters:
duration
- The length of the animation, in milliseconds. This value cannot be negative.- Returns:
- ValueAnimator The object called with setDuration(). This return
value makes it easier to compose statements together that construct and then set the
duration, as in
ValueAnimator.ofInt(0, 10).setDuration(500).start()
.
-
getDuration
public long getDuration()Gets the length of the animation. The default duration is 300 milliseconds.- Specified by:
getDuration
in classAnimator
- Returns:
- The length of the animation, in milliseconds.
-
getTotalDuration
public long getTotalDuration()Gets the total duration of the animation, accounting for animation sequences, start delay, and repeating. ReturnAnimator.DURATION_INFINITE
if the duration is infinite.- Specified by:
getTotalDuration
in classAnimator
- Returns:
- Total time an animation takes to finish, starting from the time
start()
is called.Animator.DURATION_INFINITE
will be returned if the animation or any child animation repeats infinite times.
-
setCurrentPlayTime
public void setCurrentPlayTime(long playTime) Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point.- Parameters:
playTime
- The time, in milliseconds, to which the animation is advanced or rewound.
-
setCurrentFraction
public void setCurrentFraction(float fraction) Sets the position of the animation to the specified fraction. This fraction should be between 0 and the total fraction of the animation, including any repetition. That is, a fraction of 0 will position the animation at the beginning, a value of 1 at the end, and a value of 2 at the end of a reversing animator that repeats once. If the animation has not yet been started, then it will not advance forward after it is set to this fraction; it will simply set the fraction to this value and perform any appropriate actions based on that fraction. If the animation is already running, then setCurrentFraction() will set the current fraction to this value and continue playing from that point.AnimatorListener
events are not called due to changing the fraction; those events are only processed while the animation is running.- Parameters:
fraction
- The fraction to which the animation is advanced or rewound. Values outside the range of 0 to the maximum fraction for the animator will be clamped to the correct range.
-
getCurrentPlayTime
public long getCurrentPlayTime()Gets the current position of the animation in time, which is equal to the current time minus the time that the animation started. An animation that is not yet started will return a value of zero, unless the animation has its play time set viasetCurrentPlayTime(long)
orsetCurrentFraction(float)
, in which case it will return the time that was set.- Returns:
- The current position in time of the animation.
-
setStartDelay
public void setStartDelay(long startDelay) Description copied from class:Animator
The amount of time, in milliseconds, to delay processing the animation afterAnimator.start()
is called.- Specified by:
setStartDelay
in classAnimator
- Parameters:
startDelay
- The amount of the delay, in milliseconds
-
getStartDelay
public long getStartDelay()The amount of time, in milliseconds, to delay starting the animation afterstart()
is called.- Specified by:
getStartDelay
in classAnimator
- Returns:
- the number of milliseconds to delay running the animation
-
getAnimatedValue
The most recent value calculated by thisValueAnimator
when there is just one property being animated. This value is only sensible while the animation is running. The main purpose for this read-only property is to retrieve the value from theValueAnimator
during a call toValueAnimator.AnimatorUpdateListener.onAnimationUpdate(ValueAnimator)
, which is called during each animation frame, immediately after the value is calculated.- Returns:
- animatedValue The value most recently calculated by this
ValueAnimator
for the single property being animated. If there are several properties being animated (specified by several PropertyValuesHolder objects in the constructor), this function returns the animated value for the first of those objects.
-
setRepeatCount
public void setRepeatCount(int value) Sets how many times the animation should be repeated. If the repeat count is 0, the animation is never repeated. If the repeat count is greater than 0 orINFINITE
, the repeat mode will be taken into account. The repeat count is 0 by default.- Parameters:
value
- the number of times the animation should be repeated
-
getRepeatCount
public int getRepeatCount()Defines how many times the animation should repeat. The default value is 0.- Returns:
- the number of times the animation should repeat, or
INFINITE
-
setRepeatMode
public void setRepeatMode(int value) -
getRepeatMode
public int getRepeatMode()Defines what this animation should do when it reaches the end. -
addUpdateListener
Adds a listener to the set of listeners that are sent update events through the life of an animation. This method is called on all listeners for every frame of the animation, after the values for the animation have been calculated.- Parameters:
listener
- the listener to be added to the current set of listeners for this animation.
-
removeAllUpdateListeners
public void removeAllUpdateListeners()Removes all listeners from the set listening to frame updates for this animation. -
removeUpdateListener
Removes a listener from the set listening to frame updates for this animation.- Parameters:
listener
- the listener to be removed from the current set of update listeners for this animation.
-
setInterpolator
The time interpolator used in calculating the elapsed fraction of the animation. The interpolator determines whether the animation runs with linear or non-linear motion, such as acceleration and deceleration. The default value isTimeInterpolator.ACCELERATE_DECELERATE
.- Specified by:
setInterpolator
in classAnimator
- Parameters:
value
- the interpolator to be used by this animation. A value ofnull
will result in linear interpolation.
-
getInterpolator
Returns the timing interpolator that this animation uses.- Specified by:
getInterpolator
in classAnimator
- Returns:
- The timing interpolator for this animation.
-
setEvaluator
The type evaluator to be used when calculating the animated values of this animation. The system will automatically assign a float or int evaluator based on the type ofstartValue
andendValue
in the constructor. But if these values are not one of these primitive types, or if different evaluation is desired (such as is necessary with int values that represent colors), a custom evaluator needs to be assigned. For example, when running an animation on color values, theColorEvaluator
should be used to get correct RGB color interpolation.If this ValueAnimator has only one set of values being animated between, this evaluator will be used for that set. If there are several sets of values being animated, which is the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator is assigned just to the first PropertyValuesHolder object.
- Parameters:
value
- the evaluator to be used this animation
-
start
public void start()Starts this animation. If the animation has a nonzero startDelay, the animation will start running after that delay elapses. A non-delayed animation will have its initial value(s) set immediately, followed by calls toAnimatorListener.onAnimationStart(Animator, boolean)
for any listeners of this animator. -
cancel
public void cancel()Description copied from class:Animator
Cancels the animation. UnlikeAnimator.end()
,cancel()
causes the animation to stop in its tracks, sending anAnimatorListener.onAnimationCancel(Animator)
to its listeners, followed by anAnimatorListener.onAnimationEnd(Animator, boolean)
message.This method must be called on the thread that is running the animation.
-
end
public void end()Description copied from class:Animator
Ends the animation. This causes the animation to assign the end value of the property being animated, then calling theAnimatorListener.onAnimationEnd(Animator, boolean)
method on its listeners.This method must be called on the thread that is running the animation.
-
resume
public void resume()Description copied from class:Animator
Resumes a paused animation, causing the animator to pick up where it left off when it was paused. This method should only be called on the same thread on which the animation was started. Calls to resume() on an animator that is not currently paused will be ignored. -
pause
public void pause()Description copied from class:Animator
Pauses a running animation. This method should only be called on the same thread on which the animation was started. If the animation has not yet beenstarted
or has since ended, then the call is ignored. Paused animations can be resumed by callingAnimator.resume()
. -
isRunning
public boolean isRunning()Returns whether this Animator is currently running (having been started and gone past any initial startDelay period and not yet ended). -
isStarted
public boolean isStarted()Returns whether this Animator has been started and not yet ended. For reusable Animators (which most Animators are, apart from the one-shot animator), this state is a superset ofisRunning()
, because an Animator with a nonzerogetStartDelay()
will return true during the delay phase, whereasisRunning()
will return true only after the delay phase is complete. Non-reusable animators will always return true after they have been started, because they cannot return to a non-started state. -
reverse
public void reverse()Plays the ValueAnimator in reverse. If the animation is already running, it will stop itself and play backwards from the point reached when reverse was called. If the animation is not currently running, then it will start from the end and play backwards. This behavior is only set for the current animation; future playing of the animation will use the default behavior of playing forward. -
canReverse
public boolean canReverse()- Overrides:
canReverse
in classAnimator
-
commitAnimationFrame
public void commitAnimationFrame(long frameTime) Applies an adjustment to the animation to compensate for jank between when the animation first ran and when the frame was drawn. -
doAnimationFrame
public final boolean doAnimationFrame(long frameTime) Processes a frame of the animation, adjusting the start time if needed.- Specified by:
doAnimationFrame
in interfaceAnimationHandler.FrameCallback
- Parameters:
frameTime
- The frame time.- Returns:
- true if the animation has ended.
-
getAnimatedFraction
public float getAnimatedFraction()Returns the current animation fraction, which is the elapsed/interpolated fraction used in the most recent frame update on the animation.- Returns:
- Elapsed/interpolated fraction of the animation.
-
clone
-