public abstract class Animator implements Cloneable

Known direct subclasses
AnimatorSet

This class plays a set of Animator objects in the specified order.

ValueAnimator

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

Known indirect subclasses
ObjectAnimator

This subclass of ValueAnimator provides support for animating properties on target objects.

TimeAnimator

This class provides a simple callback mechanism to listeners that is synchronized with all other animators in the system.


This is the superclass for classes which provide basic support for animations which can be started, ended, and have AnimatorListeners added to them.

Summary

Nested types

public interface Animator.AnimatorListener

An animation listener receives notifications from an animation.

A pause listener receives notifications from an animation when the animation is paused or resumed.

Implementors of this interface can add themselves as update listeners to an ValueAnimator instance to receive callbacks on every animation frame, after the current frame's values have been calculated for that ValueAnimator.

Constants

static final long

The value used to indicate infinite duration (e.g. when Animators repeat infinitely).

Public constructors

Public methods

void

Adds a listener to the set of listeners that are sent events through the life of an animation, such as start, repeat, and end.

void

Adds a pause listener to this animator.

void

Adds a listener to the set of listeners that are sent update events through the life of an animation.

void

Cancels the animation.

@NonNull Animator
void
end()

Ends the animation.

abstract long

Gets the duration of the animation.

@Nullable Interpolator

Returns the timing interpolator that this animation uses.

abstract long

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

long

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

boolean

Returns whether this animator is currently in a paused state.

abstract 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.

void

Pauses a running animation.

void

Removes all listeners and pauseListeners from this object.

void

Removes all listeners from the set listening to frame updates for this animation.

void

Removes a listener from the set listening to this animation.

void

Removes a pause listener from the set listening to this animation.

void

Removes a listener from the set listening to frame updates for this animation.

void

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

abstract @NonNull Animator
setDuration(@IntRange(from = 0) long duration)

Sets the duration of the animation.

abstract void

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

abstract void
setStartDelay(@IntRange(from = 0) long startDelay)

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

void

Sets the target object whose property will be animated by this animation.

void

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

void

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

void

Starts this animation.

Constants

DURATION_INFINITE

public static final long DURATION_INFINITE = -1

The value used to indicate infinite duration (e.g. when Animators repeat infinitely).

Public constructors

Animator

public Animator()

Public methods

addListener

public void addListener(@NonNull Animator.AnimatorListener listener)

Adds a listener to the set of listeners that are sent events through the life of an animation, such as start, repeat, and end.

Parameters
@NonNull Animator.AnimatorListener listener

the listener to be added to the current set of listeners for this animation.

addPauseListener

public void addPauseListener(@NonNull Animator.AnimatorPauseListener listener)

Adds a pause listener to this animator.

Parameters
@NonNull Animator.AnimatorPauseListener listener

the listener to be added to the current set of pause listeners for this animation.

addUpdateListener

public void addUpdateListener(@NonNull Animator.AnimatorUpdateListener listener)

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
@NonNull Animator.AnimatorUpdateListener listener

the listener to be added to the current set of listeners for this animation.

cancel

public void cancel()

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

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

clone

public @NonNull Animator clone()

end

public void end()

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

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

getDuration

public abstract long getDuration()

Gets the duration of the animation.

Returns
long

The length of the animation, in milliseconds.

getInterpolator

public @Nullable Interpolator getInterpolator()

Returns the timing interpolator that this animation uses.

Returns
@Nullable Interpolator

The timing interpolator for this animation.

getStartDelay

public abstract long getStartDelay()

The amount of time, in milliseconds, to delay processing 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 setRepeatCount} to 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.

isPaused

public boolean isPaused()

Returns whether this animator is currently in a paused state.

Returns
boolean

True if the animator is currently paused, false otherwise.

See also
pause
resume

isRunning

public abstract boolean isRunning()

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

Returns
boolean

Whether the Animator is running.

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.

removeAllListeners

public void removeAllListeners()

Removes all listeners and pauseListeners from this object.

removeAllUpdateListeners

public void removeAllUpdateListeners()

Removes all listeners from the set listening to frame updates for this animation.

removeListener

public void removeListener(@NonNull Animator.AnimatorListener listener)

Removes a listener from the set listening to this animation.

Parameters
@NonNull Animator.AnimatorListener listener

the listener to be removed from the current set of listeners for this animation.

removePauseListener

public void removePauseListener(@NonNull Animator.AnimatorPauseListener listener)

Removes a pause listener from the set listening to this animation.

Parameters
@NonNull Animator.AnimatorPauseListener listener

the listener to be removed from the current set of pause listeners for this animation.

removeUpdateListener

public void removeUpdateListener(@NonNull Animator.AnimatorUpdateListener listener)

Removes a listener from the set listening to frame updates for this animation.

Parameters
@NonNull Animator.AnimatorUpdateListener listener

the listener to be removed from the current set of update listeners for this animation.

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.

setDuration

public abstract @NonNull Animator setDuration(@IntRange(from = 0) long duration)

Sets the duration of the animation.

Parameters
@IntRange(from = 0) long duration

The length of the animation, in milliseconds.

setInterpolator

public abstract void setInterpolator(@Nullable Interpolator value)

The 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 is androidx.core.animation.AccelerateDecelerateInterpolator.

Parameters
@Nullable Interpolator value

the interpolator to be used by this animation, or null to use the default one.

setStartDelay

public abstract void setStartDelay(@IntRange(from = 0) long startDelay)

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

Parameters
@IntRange(from = 0) long startDelay

The amount of the delay, in milliseconds

setTarget

public void setTarget(@Nullable Object target)

Sets the target object whose property will be animated by this animation. Not all subclasses operate on target objects (for example, ValueAnimator, but this method is on the superclass for the convenience of dealing generically with those subclasses that do handle targets.

Note: The target is stored as a weak reference internally to avoid leaking resources by having animators directly reference old targets. Therefore, you should ensure that animator targets always have a hard reference elsewhere.

Parameters
@Nullable Object target

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 onAnimationStart 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.