ValueAnimator

public class ValueAnimator
extends Animator

java.lang.Object
   ↳ androidx.core.animation.Animator
     ↳ androidx.core.animation.ValueAnimator


This class provides a simple timing engine for running animations which calculate animated values and set them on target objects.

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 AccelerateDecelerateInterpolator class, which accelerates into and decelerates out of an animation. This behavior can be changed by calling setInterpolator(Interpolator).

Animators can be created from either code or resource files. Here is an example of a ValueAnimator resource file:

ValueAnimator also supports the use of 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.

Summary

Constants

int INFINITE

This value used used with the setRepeatCount(int) property to repeat the animation indefinitely.

int RESTART

When the animation reaches the end and repeatCount is INFINITE or a positive value, the animation restarts from the beginning.

int REVERSE

When the animation reaches the end and repeatCount is INFINITE or a positive value, the animation reverses direction on every iteration.

Inherited constants

Public constructors

ValueAnimator()

Creates a new ValueAnimator object.

Public methods

static boolean areAnimatorsEnabled()

Returns whether animators are currently enabled, system-wide.

void cancel()

Cancels the animation.

ValueAnimator clone()
void end()

Ends the animation.

float getAnimatedFraction()

Returns the current animation fraction, which is the elapsed/interpolated fraction used in the most recent frame update on the animation.

Object getAnimatedValue()

The most recent value calculated by this ValueAnimator when there is just one property being animated.

Object getAnimatedValue(String propertyName)

The most recent value calculated by this ValueAnimator for propertyName.

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.

long getDuration()

Gets the length of the animation.

static long getFrameDelay()

The amount of time, in milliseconds, between each frame of the animation.

Interpolator getInterpolator()

Returns the timing interpolator that this ValueAnimator uses.

String getNameForTrace()

Returns the name of this animator for debugging purposes.

int getRepeatCount()

Defines how many times the animation should repeat.

int getRepeatMode()

Defines what this animation should do when it reaches the end.

long getStartDelay()

The amount of time, in milliseconds, to delay starting the animation after start() is called.

long getTotalDuration()

Gets the total duration of the animation, accounting for animation sequences, start delay, and repeating.

PropertyValuesHolder[] getValues()

Returns the values that this ValueAnimator animates between.

boolean isRunning()

Returns whether this Animator is currently running (having been started and gone past any initial startDelay period and not yet ended).

boolean isStarted()

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 ValueAnimator ofObject(TypeEvaluator evaluator, Object... values)

Constructs and returns a ValueAnimator that animates between Object values.

static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values)

Constructs and returns a ValueAnimator that animates between the values specified in the PropertyValuesHolder objects.

void pause()

Pauses a running 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.

ValueAnimator 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 setFloatValues(float... values)

Sets float values that will be animated between.

static void setFrameDelay(long frameDelay)

The amount of time, in milliseconds, between each frame of the animation.

void setIntValues(int... values)

Sets int values that will be animated between.

void setInterpolator(Interpolator value)

The interpolator used in calculating the elapsed fraction of this animation.

void setNameForTrace(String animationName)

Sets a name for the animation to show up in the systrace.

void setObjectValues(Object... values)

Sets the values to animate between for this 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 starting the animation after start() is called.

void setValues(PropertyValuesHolder... values)

Sets the values, per property, being animated between.

void start()

Starts this animation.

String toString()

Inherited methods

Constants

INFINITE

public static final int INFINITE

This value used used with the setRepeatCount(int) property to repeat the animation indefinitely.

Constant Value: -1 (0xffffffff)

RESTART

public static final int RESTART

When the animation reaches the end and repeatCount is INFINITE or a positive value, the animation restarts from the beginning.

Constant Value: 1 (0x00000001)

REVERSE

public static final int REVERSE

When the animation reaches the end and repeatCount is INFINITE or a positive value, the animation reverses direction on every iteration.

Constant Value: 2 (0x00000002)

Public constructors

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.

Public methods

areAnimatorsEnabled

public static boolean areAnimatorsEnabled ()

Returns whether animators are currently enabled, system-wide. By default, all animators are enabled. This can change if either the user sets a Developer Option to set the animator duration scale to 0 or by Battery Savery mode being enabled (which disables all animations).

Developers should not typically need to call this method, but should an app wish to show a different experience when animators are disabled, this return value can be used as a decider of which experience to offer.

Returns
boolean boolean Whether animators are currently enabled. The default value is true.

cancel

public void cancel ()

Cancels the animation. Unlike end(), cancel() causes the animation to stop in its tracks, sending an Animator.AnimatorListener.onAnimationCancel(Animator) to its listeners, followed by an Animator.AnimatorListener.onAnimationEnd(Animator) message.

This method must be called on the thread that is running the animation.

clone

public ValueAnimator clone ()

Returns
ValueAnimator

end

public void end ()

Ends the animation. This causes the animation to assign the end value of the property being animated, then calling the Animator.AnimatorListener.onAnimationEnd(Animator) method on its listeners.

This method must be called on the thread that is running the animation.

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
float Elapsed/interpolated fraction of the animation.

getAnimatedValue

public Object getAnimatedValue ()

The most recent value calculated by this ValueAnimator 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 the ValueAnimator during a call to Animator.AnimatorUpdateListener.onAnimationUpdate(Animator), which is called during each animation frame, immediately after the value is calculated.

Returns
Object 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.

getAnimatedValue

public Object getAnimatedValue (String propertyName)

The most recent value calculated by this ValueAnimator for propertyName. The main purpose for this read-only property is to retrieve the value from the ValueAnimator during a call to Animator.AnimatorUpdateListener.onAnimationUpdate(Animator), which is called during each animation frame, immediately after the value is calculated.

Parameters
propertyName String

Returns
Object animatedValue The value most recently calculated for the named property by this ValueAnimator.

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 has its play time set via setCurrentPlayTime(long) or setCurrentFraction(float), in which case it will return the time that was set.

Returns
long The current position in time of the animation.

getDuration

public long getDuration ()

Gets the length of the animation. The default duration is 300 milliseconds.

Returns
long The length of the animation, in milliseconds.

getFrameDelay

public static long getFrameDelay ()

The amount of time, in milliseconds, between each frame of the animation. This is a requested time that the animation will attempt to honor, but the actual delay between frames may be different, depending on system load and capabilities. Thi