AnimatorSet

public final class AnimatorSet
extends Animator

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


This class plays a set of Animator objects in the specified order. Animations can be set up to play together, in sequence, or after a specified delay.

There are two different approaches to adding animations to a AnimatorSet: either the playTogether() or playSequentially() methods can be called to add a set of animations all at once, or the play(Animator) can be used in conjunction with methods in the Builder class to add animations one by one.

It is possible to set up a AnimatorSet with circular dependencies between its animations. For example, an animation a1 could be set up to start before animation a2, a2 before a3, and a3 before a1. The results of this configuration are undefined, but will typically result in none of the affected animations being played. Because of this (and because circular dependencies do not make logical sense anyway), circular dependencies should be avoided, and the dependency flow of animations should only be in one direction.

Developer Guides

For more information about animating with AnimatorSet, read the Property Animation developer guide.

Summary

Nested classes

class AnimatorSet.Builder

The Builder object is a utility class to facilitate adding animations to a AnimatorSet along with the relationships between the various animations. 

Inherited constants

Public constructors

AnimatorSet()

Public methods

boolean canReverse()

AnimatorSet is only reversible when the set contains no infinitely-repeating animation (e.g.

void cancel()

Cancels the animation.

Note that canceling a AnimatorSet also cancels all of the animations that it is responsible for.

AnimatorSet clone()
void end()

Ends the animation.

Note that ending a AnimatorSet also ends all of the animations that it is responsible for.

ArrayList<Animator> getChildAnimations()

Returns the current list of child Animator objects controlled by this AnimatorSet.

long getCurrentPlayTime()

Returns the milliseconds elapsed since the start of the animation.

long getDuration()

Gets the length of each of the child animations of this AnimatorSet.

Interpolator getInterpolator()

Returns the timing interpolator that this animation uses.

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.

boolean isRunning()

Returns true if any of the child animations of this AnimatorSet have been started and have not yet ended.

boolean isStarted()

Returns whether this Animator has been started and not yet ended.

void pause()

Pauses a running animation.

AnimatorSet.Builder play(Animator anim)

This method creates a Builder object, which is used to set up playing constraints.

void playSequentially(List<Animator> items)

Sets up this AnimatorSet to play each of the supplied animations when the previous animation ends.

void playSequentially(Animator... items)

Sets up this AnimatorSet to play each of the supplied animations when the previous animation ends.

void playTogether(Animator... items)

Sets up this AnimatorSet to play all of the supplied animations at the same time.

void playTogether(Collection<Animator> items)

Sets up this AnimatorSet to play all of the supplied animations at the same time.

void resume()

Resumes a paused animation, causing the animator to pick up where it left off when it was paused.

void reverse()

Plays the AnimatorSet in reverse.

void setCurrentPlayTime(long playTime)

Sets the position of the animation to the specified point in time.

AnimatorSet setDuration(long duration)

Sets the length of each of the current child animations of this AnimatorSet.

void setInterpolator(Interpolator interpolator)

Sets the Interpolator for all current child animations of this AnimatorSet.

void setStartDelay(long startDelay)

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

void setTarget(Object target)

Sets the target object for all current child animations of this AnimatorSet that take targets (ObjectAnimator and AnimatorSet).

void setupEndValues()

This method tells the object to use appropriate information to extract ending values for the animation.

void setupStartValues()

This method tells the object to use appropriate information to extract starting values for the animation.

void start()

Starts this animation.

Starting this AnimatorSet will, in turn, start the animations for which it is responsible.

String toString()

Inherited methods

Public constructors

AnimatorSet

public AnimatorSet ()

Public methods

canReverse

public boolean canReverse ()

AnimatorSet is only reversible when the set contains no infinitely-repeating animation (e.g. a child animation with repeatCount = ValueAnimator.INFINITE

Returns
boolean true if the animator set is reversible, false otherwise.

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.

Note that canceling a AnimatorSet also cancels all of the animations that it is responsible for.

clone

public AnimatorSet clone ()

Returns
AnimatorSet

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.

Note that ending a AnimatorSet also ends all of the animations that it is responsible for.

getChildAnimations

public ArrayList<Animator> getChildAnimations ()

Returns the current list of child Animator objects controlled by this AnimatorSet. This is a copy of the internal list; modifications to the returned list will not affect the AnimatorSet, although changes to the underlying Animator objects will affect those objects being managed by the AnimatorSet.

Returns
ArrayList<Animator> ArrayList The list of child animations of this AnimatorSet.

getCurrentPlayTime

public long getCurrentPlayTime ()

Returns the milliseconds elapsed since the start of the animation.

For ongoing animations, this method returns the current progress of the animation in terms of play time. For an animation that has not yet been started: if the animation has been seeked to a certain time via setCurrentPlayTime(long), the seeked play time will be returned; otherwise, this method will return 0.

Returns
long the current position in time of the animation in milliseconds

getDuration

public long getDuration ()

Gets the length of each of the child animations of this AnimatorSet. This value may be less than 0, which indicates that no duration has been set on this AnimatorSet and each of the child animations will use their own duration.

Returns
long The length of the animation, in milliseconds, of each of the child animations of this AnimatorSet.

getInterpolator

public Interpolator getInterpolator ()

Returns the timing interpolator that this animation uses.

Returns
Interpolator The timing interpolator for this animation.

getStartDelay

public long getStartDelay ()

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

Returns
long the number of milliseconds to delay running the animation

getTotalDuration

public long getTotalDuration ()

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

When the animation repeats infinite times, or when any of the child animators does (via ValueAnimator.setRepeatCount(int)} to ValueAnimator.INFINITE), the total duration will be DURATION_INFINITE. Otherwise, the total duration is the sum of start delay and animation running time (i.e. duration of one iteration multiplied by the number of iterations).

Returns
long Total time an animation takes to finish, starting from the time start() is called. DURATION_INFINITE will be returned if the animation or any child animation repeats infinite times.

isRunning

public boolean isRunning ()

Returns true if any of the child animations of this AnimatorSet have been started and have not yet ended. Child animations will not be started until the AnimatorSet has gone past its initial delay set through setStartDelay(long).

Returns
boolean Whether this AnimatorSet has gone past the initial delay, and at least one child animation has been started 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 produced by createCircularReveal()), this state is a superset of isRunning(), because an Animator with a nonzero startDelay will return true for isStarted() during the delay phase, whereas isRunning() 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.

Returns
boolean Whether the Animator has been started and not yet ended.

pause

public void pause ()

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 been started or has since ended, then the call is ignored. Paused animations can be resumed by calling resume().

play

public AnimatorSet.Builder play (Animator anim)

This method creates a Builder object, which is used to set up playing constraints. This initial play() method tells the Builder the animation that is the dependency for the succeeding commands to the Builder. For example, calling play(a1).with(a2) sets up the AnimatorSet to play a1 and a2 at the same time, play(a1).before(a2) sets up the AnimatorSet to play a1 first, followed by a2, and play(a1).after(a2) sets up the AnimatorSet to play a2 first, followed by a1.

Note that play() is the only way to tell the Builder the animation upon which the dependency is created, so successive calls to the various functions in Builder will all refer to the initial parameter supplied in play() as the dependency of the other animations. For example, calling play(a1).before(a2).before(a3) will play both a2 and a3 when a1 ends; it does not set up a dependency between a2 and a3.

Parameters
anim Animator: The animation that is the dependency used in later calls to the methods in the returned Builder object. A null parameter will result in a null Builder return value.

<