AnimatorSet
public
final
class
AnimatorSet
extends Animator
java.lang.Object | ||
↳ | android.animation.Animator | |
↳ | android.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 AnimatorSet.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 |
Inherited constants |
---|
Public constructors | |
---|---|
AnimatorSet()
|
Public methods | |
---|---|
void
|
cancel()
Cancels the animation. Note that canceling a |
AnimatorSet
|
clone()
Creates and returns a copy of this object. |
void
|
end()
Ends the animation. Note that ending a |
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. |
TimeInterpolator
|
getInterpolator()
Returns the timing interpolator that this animation uses. |
long
|
getStartDelay()
The amount of time, in milliseconds, to delay starting the animation after
|
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 |
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(TimeInterpolator interpolator)
Sets the TimeInterpolator for all current |
void
|
setStartDelay(long startDelay)
The amount of time, in milliseconds, to delay starting the animation after
|
void
|
setTarget(Object target)
Sets the target object for all current |
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 |
String
|
toString()
Returns a string representation of the object. |
Inherited methods | |
---|---|
Public constructors
Public methods
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 ()
Creates and returns a copy of this object. The precise meaning
of "copy" may depend on the class of the object. The general
intent is that, for any object x
, the expression:
will be true, and that the expression: x.clone() != x
will be x.clone().getClass() == x.getClass()
true
, but these are not absolute requirements.
While it is typically the case that:
will be x.clone().equals(x)
true
, this is not an absolute requirement.
By convention, the returned object should be obtained by calling
super.clone
. If a class and all of its superclasses (except
Object
) obey this convention, it will be the case that
x.clone().getClass() == x.getClass()
.
By convention, the object returned by this method should be independent
of this object (which is being cloned). To achieve this independence,
it may be necessary to modify one or more fields of the object returned
by super.clone
before returning it. Typically, this means
copying any mutable objects that comprise the internal "deep structure"
of the object being cloned and replacing the references to these
objects with references to the copies. If a class contains only
primitive fields or references to immutable objects, then it is usually
the case that no fields in the object returned by super.clone
need to be modified.
Returns | |
---|---|
AnimatorSet |
a clone of this instance. |
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 |
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 TimeInterpolator getInterpolator ()
Returns the timing interpolator that this animation uses.
Returns | |
---|---|
TimeInterpolator |
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. Return DURATION_INFINITE
if the duration is infinite.
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. |
Returns | |
---|---|
AnimatorSet.Builder |
Builder The object that constructs the AnimatorSet based on the dependencies
outlined in the calls to play and the other methods in the
Builder |
playSequentially
public void playSequentially (List<Animator> items)
Sets up this AnimatorSet to play each of the supplied animations when the previous animation ends.
Parameters | |
---|---|
items |
List : The animations that will be started one after another. |
playSequentially
public void playSequentially (Animator... items)
Sets up this AnimatorSet to play each of the supplied animations when the previous animation ends.
Parameters | |
---|---|
items |
Animator : The animations that will be started one after another. |
playTogether
public void playTogether (Animator... items)
Sets up this AnimatorSet to play all of the supplied animations at the same time.
This is equivalent to calling play(android.animation.Animator)
with the first animator in the
set and then Builder.with(Animator)
with each of the other animators. Note that
an Animator with a startDelay
will not actually
start until that delay elapses, which means that if the first animator in the list
supplied to this constructor has a startDelay, none of the other animators will start
until that first animator's startDelay has elapsed.
Parameters | |
---|---|
items |
Animator : The animations that will be started simultaneously. |
playTogether
public void playTogether (Collection<Animator> items)
Sets up this AnimatorSet to play all of the supplied animations at the same time.
Parameters | |
---|---|
items |
Collection : The animations that will be started simultaneously. |
resume
public void resume ()
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.
reverse
public void reverse ()
Plays the AnimatorSet in reverse. If the animation has been seeked to a specific play time
using setCurrentPlayTime(long)
, it will play backwards from the point seeked when
reverse was called. Otherwise, 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.
Note: reverse is not supported for infinite AnimatorSet.
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.
On Build.VERSION_CODES.UPSIDE_DOWN_CAKE
and above, an AnimatorSet
that hasn't been start()
ed, will issue
Animator.AnimatorListener.onAnimationStart(Animator, boolean)
and Animator.AnimatorListener.onAnimationEnd(Animator, boolean)
events.
Parameters | |
---|---|
playTime |
long : The time, in milliseconds, to which the animation is advanced or rewound.
Unless the animation is reversing, the playtime is considered the time since
the end of the start delay of the AnimatorSet in a forward playing direction. |
setDuration
public AnimatorSet setDuration (long duration)
Sets the length of each of the current child animations of this AnimatorSet. By default, each child animation will use its own duration. If the duration is set on the AnimatorSet, then each child animation inherits this duration.
Parameters | |
---|---|
duration |
long : The length of the animation, in milliseconds, of each of the child
animations of this AnimatorSet. |
Returns | |
---|---|
AnimatorSet |
setInterpolator
public void setInterpolator (TimeInterpolator interpolator)
Sets the TimeInterpolator for all current child animations
of this AnimatorSet. The default value is null, which means that no interpolator
is set on this AnimatorSet. Setting the interpolator to any non-null value
will cause that interpolator to be set on the child animations
when the set is started.
Parameters | |
---|---|
interpolator |
TimeInterpolator : the interpolator to be used by each child animation of this AnimatorSet |
setStartDelay
public void setStartDelay (long startDelay)
The amount of time, in milliseconds, to delay starting the animation after
start()
is called. Note that the start delay should always be non-negative. Any
negative start delay will be clamped to 0 on N and above.
Parameters | |
---|---|
startDelay |
long : The amount of the delay, in milliseconds |
setTarget
public void setTarget (Object target)
Sets the target object for all current child animations
of this AnimatorSet that take targets (ObjectAnimator
and
AnimatorSet).
Parameters | |
---|---|
target |
Object : The object being animated |
setupEndValues
public void setupEndValues ()
This method tells the object to use appropriate information to extract ending values for the animation. For example, a AnimatorSet object will pass this call to its child objects to tell them to set up the values. A ObjectAnimator object will use the information it has about its target object and PropertyValuesHolder objects to get the start values for its properties. A ValueAnimator object will ignore the request since it does not have enough information (such as a target object) to gather these values.
setupStartValues
public void setupStartValues ()
This method tells the object to use appropriate information to extract starting values for the animation. For example, a AnimatorSet object will pass this call to its child objects to tell them to set up the values. A ObjectAnimator object will use the information it has about its target object and PropertyValuesHolder objects to get the start values for its properties. A ValueAnimator object will ignore the request since it does not have enough information (such as a target object) to gather these values.
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 to
AnimatorListener.onAnimationStart(Animator)
for any listeners of this animator.
The animation started by calling this method will be run on the thread that called this method. This thread should have a Looper on it (a runtime exception will be thrown if this is not the case). Also, if the animation will animate properties of objects in the view hierarchy, then the calling thread should be the UI thread for that view hierarchy.
Starting this AnimatorSet
will, in turn, start the animations for which
it is responsible. The details of when exactly those animations are started depends on
the dependency relationships that have been set up between the animations.
Note: Manipulating AnimatorSet's lifecycle in the child animators' listener callbacks
will lead to undefined behaviors. Also, AnimatorSet will ignore any seeking in the child
animators once start()
is called.
toString
public String toString ()
Returns a string representation of the object.
Returns | |
---|---|
String |
a string representation of the object. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-12-18 UTC.