Class AnimatorSet
- All Implemented Interfaces:
AnimationHandler.FrameCallback,Cloneable
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassTheBuilderobject is a utility class to facilitate adding animations to aAnimatorSetalong with the relationships between the various animations. -
Field Summary
Fields inherited from class icyllis.modernui.animation.Animator
DURATION_INFINITE, MARKER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Cancels the animation.booleanAnimatorSet is only reversible when the set contains no sequential animation, and no child animators have a start delay.clone()voidcommitAnimationFrame(long frameTime) booleandoAnimationFrame(long frameTime) Run animation based on the frame time.voidend()Ends the animation.Returns the current list of child Animator objects controlled by this AnimatorSet.longReturns the milliseconds elapsed since the start of the animation.longGets the length of each of the child animations of this AnimatorSet.Returns the timing interpolator that this animation uses.longThe amount of time, in milliseconds, to delay starting the animation afterstart()is called.longGets the total duration of the animation, accounting for animation sequences, start delay, and repeating.booleanReturns true if any of the child animations of this AnimatorSet have been started and have not yet ended.booleanReturns whether this Animator has been started and not yet ended.voidpause()Pauses a running animation.This method creates aBuilderobject, which is used to set up playing constraints.voidplaySequentially(Animator... items) Sets up this AnimatorSet to play each of the supplied animations when the previous animation ends.voidplaySequentially(List<Animator> items) Sets up this AnimatorSet to play each of the supplied animations when the previous animation ends.voidplayTogether(Animator... items) Sets up this AnimatorSet to play all of the supplied animations at the same time.voidplayTogether(Collection<Animator> items) Sets up this AnimatorSet to play all of the supplied animations at the same time.voidresume()Resumes a paused animation, causing the animator to pick up where it left off when it was paused.voidreverse()Plays the AnimatorSet in reverse.voidsetCurrentPlayTime(long playTime) Sets the position of the animation to the specified point in time.setDuration(long duration) Sets the length of each of the current child animations of this AnimatorSet.voidsetInterpolator(TimeInterpolator interpolator) Sets the TimeInterpolator for all currentchild animationsof this AnimatorSet.voidsetStartDelay(long startDelay) The amount of time, in milliseconds, to delay starting the animation afterstart()is called.voidSets the target object for all currentchild animationsof this AnimatorSet that take targets (ObjectAnimatorand AnimatorSet).voidThis method tells the object to use appropriate information to extract ending values for the animation.voidThis method tells the object to use appropriate information to extract starting values for the animation.booleanvoidstart()Starts this animation.toString()Methods inherited from class icyllis.modernui.animation.Animator
addListener, getListeners, isPaused, removeAllListeners, removeListener
-
Constructor Details
-
AnimatorSet
public AnimatorSet()
-
-
Method Details
-
playTogether
Sets up this AnimatorSet to play all of the supplied animations at the same time. This is equivalent to callingplay(Animator)with the first animator in the set and thenAnimatorSet.Builder.with(Animator)with each of the other animators. Note that an Animator with astartDelaywill 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- The animations that will be started simultaneously.
-
playTogether
Sets up this AnimatorSet to play all of the supplied animations at the same time.- Parameters:
items- The animations that will be started simultaneously.
-
playSequentially
Sets up this AnimatorSet to play each of the supplied animations when the previous animation ends.- Parameters:
items- The animations that will be started one after another.
-
playSequentially
Sets up this AnimatorSet to play each of the supplied animations when the previous animation ends.- Parameters:
items- The animations that will be started one after another.
-
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:
- The list of child animations of this AnimatorSet.
-
setTarget
Sets the target object for all currentchild animationsof this AnimatorSet that take targets (ObjectAnimatorand AnimatorSet). -
setInterpolator
Sets the TimeInterpolator for all currentchild animationsof 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.- Specified by:
setInterpolatorin classAnimator- Parameters:
interpolator- the interpolator to be used by each child animation of this AnimatorSet
-
getInterpolator
Description copied from class:AnimatorReturns the timing interpolator that this animation uses.- Specified by:
getInterpolatorin classAnimator- Returns:
- The timing interpolator for this animation.
-
play
This method creates aBuilderobject, which is used to set up playing constraints. This initialplay()method tells theBuilderthe animation that is the dependency for the succeeding commands to theBuilder. For example, callingplay(a1).with(a2)sets up the AnimatorSet to playa1anda2at the same time,play(a1).before(a2)sets up the AnimatorSet to playa1first, followed bya2, andplay(a1).after(a2)sets up the AnimatorSet to playa2first, followed bya1.Note that
play()is the only way to tell theBuilderthe animation upon which the dependency is created, so successive calls to the various functions inBuilderwill all refer to the initial parameter supplied inplay()as the dependency of the other animations. For example, callingplay(a1).before(a2).before(a3)will play botha2anda3when a1 ends; it does not set up a dependency betweena2anda3.- Parameters:
anim- The animation that is the dependency used in later calls to the methods in the returnedBuilderobject. A null parameter will result in a nullBuilderreturn value.- Returns:
- Builder The object that constructs the AnimatorSet based on the dependencies
outlined in the calls to
playand the other methods in theBuilderobject.
-
cancel
public void cancel()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.
Note that canceling a
AnimatorSetalso cancels all of the animations that it is responsible for. -
end
public void end()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.
Note that ending a
AnimatorSetalso ends all of the animations that it is responsible for. -
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 throughsetStartDelay(long). -
isStarted
public boolean isStarted()Description copied from class:AnimatorReturns 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 ofAnimator.isRunning(), because an Animator with a nonzeroAnimator.getStartDelay()will return true during the delay phase, whereasAnimator.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. -
getStartDelay
public long getStartDelay()The amount of time, in milliseconds, to delay starting the animation afterstart()is called.- Specified by:
getStartDelayin classAnimator- Returns:
- the number of milliseconds to delay running the animation
-
setStartDelay
public void setStartDelay(long startDelay) The amount of time, in milliseconds, to delay starting the animation afterstart()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.- Specified by:
setStartDelayin classAnimator- Parameters:
startDelay- The amount of the delay, 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.- Specified by:
getDurationin classAnimator- Returns:
- The length of the animation, in milliseconds, of each of the child animations of this AnimatorSet.
-
setDuration
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.- Specified by:
setDurationin classAnimator- Parameters:
duration- The length of the animation, in milliseconds, of each of the child animations of this AnimatorSet.
-
setupStartValues
public void setupStartValues()Description copied from class:AnimatorThis 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.- Overrides:
setupStartValuesin classAnimator
-
setupEndValues
public void setupEndValues()Description copied from class:AnimatorThis 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.- Overrides:
setupEndValuesin classAnimator
-
pause
public void pause()Description copied from class:AnimatorPauses 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 beenstartedor has since ended, then the call is ignored. Paused animations can be resumed by callingAnimator.resume(). -
resume
public void resume()Description copied from class:AnimatorResumes 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. -
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.The animation started by calling this method will be run on the thread that called this method.
Starting this
AnimatorSetwill, 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 oncestart()is called. -
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. 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.
-
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:
- the current position in time of the animation in milliseconds
-
doAnimationFrame
@Internal public boolean doAnimationFrame(long frameTime) Description copied from interface:AnimationHandler.FrameCallbackRun animation based on the frame time.- Specified by:
doAnimationFramein interfaceAnimationHandler.FrameCallback- Parameters:
frameTime- The frame start time, in theCore.timeMillis()time base- Returns:
- if the animation has finished.
-
commitAnimationFrame
@Internal public void commitAnimationFrame(long frameTime) -
clone
-
canReverse
@Internal public boolean canReverse()AnimatorSet is only reversible when the set contains no sequential animation, and no child animators have a start delay.- Overrides:
canReversein classAnimator
-
reverse
public void reverse()Plays the AnimatorSet in reverse. If the animation has been seeked to a specific play time usingsetCurrentPlayTime(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.
-
toString
-
shouldPlayTogether
public boolean shouldPlayTogether()- Returns:
- whether all the animators in the set are supposed to play together
-
getTotalDuration
public long getTotalDuration()Description copied from class:AnimatorGets the total duration of the animation, accounting for animation sequences, start delay, and repeating. ReturnAnimator.DURATION_INFINITEif the duration is infinite.- Specified by:
getTotalDurationin classAnimator- Returns:
- Total time an animation takes to finish, starting from the time
Animator.start()is called.Animator.DURATION_INFINITEwill be returned if the animation or any child animation repeats infinite times.
-