ObjectAnimator


public final class ObjectAnimator extends ValueAnimator


This subclass of ValueAnimator provides support for animating properties on target objects. The constructors of this class take parameters to define the target object that will be animated as well as the name of the property that will be animated. Appropriate set/get functions are then determined internally and the animation will call these functions as necessary to animate the property.

ObjectAnimator supports the use of PropertyValuesHolder and Keyframe in resource files to create more complex animations. Using PropertyValuesHolders allows animators to animate several properties in parallel.

Using Keyframes allows animations to follow more complex paths from the start to the end values. 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. Also, a keyframe with no value will derive its value from the target object when the animator starts, just like animators with only one value specified. In addition, an optional interpolator can be specified. The interpolator will be applied on the interval between the keyframe that the interpolator is set on and the previous keyframe. When no interpolator is supplied, the default AccelerateDecelerateInterpolator will be used.

See also
setPropertyName

Summary

Public constructors

Creates a new ObjectAnimator object.

Public methods

@NonNull ObjectAnimator
@NonNull String

Returns the name of this animator for debugging purposes.

@NonNull String

Gets the name of the property that will be animated.

@Nullable Object

The target object whose property will be animated by this animation

static @NonNull ObjectAnimator
ofArgb(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull int[] values
)

Constructs and returns an ObjectAnimator that animates between color values.

static @NonNull ObjectAnimator
<T> ofArgb(
    @NonNull T target,
    @NonNull Property<T, Integer> property,
    @NonNull int[] values
)

Constructs and returns an ObjectAnimator that animates between color values.

static @NonNull ObjectAnimator
ofFloat(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull float[] values
)

Constructs and returns an ObjectAnimator that animates between float values.

static @NonNull ObjectAnimator
<T> ofFloat(
    @NonNull T target,
    @NonNull Property<T, Float> property,
    @NonNull float[] values
)

Constructs and returns an ObjectAnimator that animates between float values.

static @NonNull ObjectAnimator
ofFloat(
    @NonNull Object target,
    @Nullable String xPropertyName,
    @Nullable String yPropertyName,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates coordinates along a Path using two properties.

static @NonNull ObjectAnimator
<T> ofFloat(
    @NonNull T target,
    @Nullable Property<T, Float> xProperty,
    @Nullable Property<T, Float> yProperty,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates coordinates along a Path using two properties.

static @NonNull ObjectAnimator
ofInt(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull int[] values
)

Constructs and returns an ObjectAnimator that animates between int values.

static @NonNull ObjectAnimator
<T> ofInt(
    @NonNull T target,
    @NonNull Property<T, Integer> property,
    @NonNull int[] values
)

Constructs and returns an ObjectAnimator that animates between int values.

static @NonNull ObjectAnimator
ofInt(
    @NonNull Object target,
    @NonNull String xPropertyName,
    @NonNull String yPropertyName,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates coordinates along a Path using two properties.

static @NonNull ObjectAnimator
<T> ofInt(
    @NonNull T target,
    @Nullable Property<T, Integer> xProperty,
    @Nullable Property<T, Integer> yProperty,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates coordinates along a Path using two properties.

static @NonNull ObjectAnimator
ofMultiFloat(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates the target using a multi-float setter along the given Path.

static @NonNull ObjectAnimator
ofMultiFloat(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull float[][] values
)

Constructs and returns an ObjectAnimator that animates over float values for a multiple parameters setter.

static @NonNull ObjectAnimator
@SafeVarargs
<T> ofMultiFloat(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull TypeConverter<T, float[]> converter,
    @NonNull TypeEvaluator<T> evaluator,
    @NonNull T[] values
)

Constructs and returns an ObjectAnimator that animates over values for a multiple float parameters setter.

static @NonNull ObjectAnimator
ofMultiInt(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates the target using a multi-int setter along the given Path.

static @NonNull ObjectAnimator
ofMultiInt(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull int[][] values
)

Constructs and returns an ObjectAnimator that animates over int values for a multiple parameters setter.

static @NonNull ObjectAnimator
@SafeVarargs
<T> ofMultiInt(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull TypeConverter<T, int[]> converter,
    @NonNull TypeEvaluator<T> evaluator,
    @NonNull T[] values
)

Constructs and returns an ObjectAnimator that animates over values for a multiple int parameters setter.

static @NonNull ObjectAnimator
ofObject(
    @NonNull Object target,
    @NonNull String propertyName,
    @Nullable TypeConverter<PointFObject> converter,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates a property along a Path.

static @NonNull ObjectAnimator
ofObject(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull TypeEvaluator evaluator,
    @NonNull Object[] values
)

Constructs and returns an ObjectAnimator that animates between Object values.

static @NonNull ObjectAnimator
<T, V> ofObject(
    @NonNull T target,
    @NonNull Property<T, V> property,
    @Nullable TypeConverter<PointF, V> converter,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates a property along a Path.

static @NonNull ObjectAnimator
@SafeVarargs
<T, V> ofObject(
    @NonNull T target,
    @NonNull Property<T, V> property,
    @NonNull TypeEvaluator<V> evaluator,
    @NonNull V[] values
)

Constructs and returns an ObjectAnimator that animates between Object values.

static @NonNull ObjectAnimator
@SafeVarargs
<T, V, P> ofObject(
    @NonNull T target,
    @NonNull Property<T, P> property,
    @NonNull TypeConverter<V, P> converter,
    @NonNull TypeEvaluator<V> evaluator,
    @NonNull V[] values
)

Constructs and returns an ObjectAnimator that animates between Object values.

static @NonNull ObjectAnimator
ofPropertyValuesHolder(
    @NonNull Object target,
    @NonNull PropertyValuesHolder[] values
)

Constructs and returns an ObjectAnimator that animates between the sets of values specified in PropertyValueHolder objects.

void
setAutoCancel(boolean cancel)

autoCancel controls whether an ObjectAnimator will be canceled automatically when any other ObjectAnimator with the same target and properties is started.

@NonNull ObjectAnimator
setDuration(long duration)

Sets the length of the animation.

void
setFloatValues(@NonNull float[] values)

Sets float values that will be animated between.

void
setIntValues(@NonNull int[] values)

Sets int values that will be animated between.

void
setObjectValues(@NonNull Object[] values)

Sets the values to animate between for this animation.

void

Sets the property that will be animated.

void

Sets the name of the property that will be animated.

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.

@NonNull String

Inherited Constants

From androidx.core.animation.Animator
static final long

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

From androidx.core.animation.ValueAnimator
static final int

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

static final int

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

static final int

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

Inherited methods

From androidx.core.animation.Animator
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.

@NonNull Animator
boolean

Returns whether this animator is currently in a paused state.

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.

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

Sets the duration 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.

From androidx.core.animation.ValueAnimator
static boolean

Returns whether animators are currently enabled, system-wide.

void

Cancels the animation.

void
end()

Ends the animation.

float

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

@NonNull Object

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

@Nullable Object

The most recent value calculated by this ValueAnimator for propertyName.

long

Gets the current position of the animation in time, which is equal to the current time minus the time that the animation started.

long

Gets the length of the animation.

static long

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

@Nullable Interpolator

Returns the timing interpolator that this ValueAnimator uses.

int

Defines how many times the animation should repeat.

int

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

long

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

long

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

@NonNull PropertyValuesHolder[]

Returns the values that this ValueAnimator animates between.

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.

static @NonNull ValueAnimator
ofArgb(@NonNull int[] values)

Constructs and returns a ValueAnimator that animates between color values.

static @NonNull ValueAnimator
ofFloat(@NonNull float[] values)

Constructs and returns a ValueAnimator that animates between float values.

static @NonNull ValueAnimator
ofInt(@NonNull int[] values)

Constructs and returns a ValueAnimator that animates between int values.

static @NonNull ValueAnimator
ofObject(@NonNull TypeEvaluator evaluator, @NonNull Object[] values)

Constructs and returns a ValueAnimator that animates between Object values.

static @NonNull ValueAnimator
ofPropertyValuesHolder(@NonNull PropertyValuesHolder[] values)

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

void

Pauses a running animation.

void

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

void

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.

void

The type evaluator to be used when calculating the animated values of this animation.

static void
setFrameDelay(long frameDelay)

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

void

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

void
setNameForTrace(@NonNull String animationName)

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

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(@NonNull PropertyValuesHolder[] values)

Sets the values, per property, being animated between.

Public constructors

ObjectAnimator

public ObjectAnimator()

Creates a new ObjectAnimator object. This default constructor is primarily for use internally; the other constructors which take parameters are more generally useful.

Public methods

clone

public @NonNull ObjectAnimator clone()

getNameForTrace

public @NonNull String getNameForTrace()

Returns the name of this animator for debugging purposes.

getPropertyName

public @NonNull String getPropertyName()

Gets the name of the property that will be animated. This name will be used to derive a setter function that will be called to set animated values. For example, a property name of foo will result in a call to the function setFoo() on the target object. If either valueFrom or valueTo is null, then a getter function will also be derived and called.

If this animator was created with a Property object instead of the string name of a property, then this method will return the name of that Property object instead. If this animator was created with one or more PropertyValuesHolder objects, then this method will return the name of that object (if there was just one) or a comma-separated list of all of the names (if there are more than one).

getTarget

public @Nullable Object getTarget()

The target object whose property will be animated by this animation

Returns
@Nullable Object

The object being animated

ofArgb

public static @NonNull ObjectAnimator ofArgb(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull int[] values
)

Constructs and returns an ObjectAnimator that animates between color values. A single value implies that that value is the one being animated to, in which case the start value will be derived from the property being animated and the target object when start is called for the first time. Two values imply starting and ending values. More than two values imply a starting value, values to animate through along the way, and an ending value (these values will be distributed evenly across the duration of the animation).

Parameters
@NonNull Object target

The object whose property is to be animated. This object should have a public method on it called setName(), where name is the value of the propertyName parameter.

@NonNull String propertyName

The name of the property being animated.

@NonNull int[] values

A set of values that the animation will animate between over time.

Returns
@NonNull ObjectAnimator

An ObjectAnimator object that is set up to animate between the given values.

ofArgb

public static @NonNull ObjectAnimator <T> ofArgb(
    @NonNull T target,
    @NonNull Property<T, Integer> property,
    @NonNull int[] values
)

Constructs and returns an ObjectAnimator that animates between color values. A single value implies that that value is the one being animated to, in which case the start value will be derived from the property being animated and the target object when start is called for the first time. Two values imply starting and ending values. More than two values imply a starting value, values to animate through along the way, and an ending value (these values will be distributed evenly across the duration of the animation).

Parameters
@NonNull T target

The object whose property is to be animated.

@NonNull Property<T, Integer> property

The property being animated.

@NonNull int[] values

A set of values that the animation will animate between over time.

Returns
@NonNull ObjectAnimator

An ObjectAnimator object that is set up to animate between the given values.

ofFloat

public static @NonNull ObjectAnimator ofFloat(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull float[] values
)

Constructs and returns an ObjectAnimator that animates between float values. A single value implies that that value is the one being animated to, in which case the start value will be derived from the property being animated and the target object when start is called for the first time. Two values imply starting and ending values. More than two values imply a starting value, values to animate through along the way, and an ending value (these values will be distributed evenly across the duration of the animation).

Parameters
@NonNull Object target

The object whose property is to be animated. This object should have a public method on it called setName(), where name is the value of the propertyName parameter.

@NonNull String propertyName

The name of the property being animated.

@NonNull float[] values

A set of values that the animation will animate between over time.

Returns
@NonNull ObjectAnimator

An ObjectAnimator object that is set up to animate between the given values.

ofFloat

public static @NonNull ObjectAnimator <T> ofFloat(
    @NonNull T target,
    @NonNull Property<T, Float> property,
    @NonNull float[] values
)

Constructs and returns an ObjectAnimator that animates between float values. A single value implies that that value is the one being animated to, in which case the start value will be derived from the property being animated and the target object when start is called for the first time. Two values imply starting and ending values. More than two values imply a starting value, values to animate through along the way, and an ending value (these values will be distributed evenly across the duration of the animation).

Parameters
@NonNull T target

The object whose property is to be animated.

@NonNull Property<T, Float> property

The property being animated.

@NonNull float[] values

A set of values that the animation will animate between over time.

Returns
@NonNull ObjectAnimator

An ObjectAnimator object that is set up to animate between the given values.

ofFloat

public static @NonNull ObjectAnimator ofFloat(
    @NonNull Object target,
    @Nullable String xPropertyName,
    @Nullable String yPropertyName,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates coordinates along a Path using two properties. A Path animation moves in two dimensions, animating coordinates (x, y) together to follow the line. In this variation, the coordinates are floats that are set to separate properties designated by xPropertyName and yPropertyName.

Parameters
@NonNull Object target

The object whose properties are to be animated. This object should have public methods on it called setNameX() and setNameY, where nameX and nameYare the value of the xPropertyName and yPropertyNameparameters, respectively.

@Nullable String xPropertyName

The name of the property for the x coordinate being animated.

@Nullable String yPropertyName

The name of the property for the y coordinate being animated.

@NonNull Path path

The Path to animate values along.

Returns
@NonNull ObjectAnimator

An ObjectAnimator object that is set up to animate along path.

ofFloat

public static @NonNull ObjectAnimator <T> ofFloat(
    @NonNull T target,
    @Nullable Property<T, Float> xProperty,
    @Nullable Property<T, Float> yProperty,
    @NonNull Path path
)

Constructs and returns an ObjectAnimator that animates coordinates along a Path using two properties. A Path animation moves in two dimensions, animating coordinates (x, y) together to follow the line. In this variation, the coordinates are floats that are set to separate properties, xProperty and yProperty.

Parameters
@NonNull T target

The object whose properties are to be animated.

@Nullable Property<T, Float> xProperty

The property for the x coordinate being animated.

@Nullable Property<T, Float> yProperty

The property for the y coordinate being animated.

@NonNull Path path

The Path to animate values along.

Returns
@NonNull ObjectAnimator

An ObjectAnimator object that is set up to animate along path.

ofInt

public static @NonNull ObjectAnimator ofInt(
    @NonNull Object target,
    @NonNull String propertyName,
    @NonNull int[] values
)

Constructs and returns an ObjectAnimator that animates between int values. A single value implies that that value is the one being animated to, in which case the start value will be derived from the property being animated and the target object when start is called for the first time. Two values imply starting and ending values. More than two values imply a starting value, values to animate through along the way, and an ending value (these values will be distributed evenly across the duration of the animation).