to top
Android APIs
public final class

ObjectAnimator

extends ValueAnimator
java.lang.Object
   ↳ android.animation.Animator
     ↳ android.animation.ValueAnimator
       ↳ android.animation.ObjectAnimator

Class Overview

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.

Developer Guides

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

Summary

[Expand]
Inherited Constants
From class android.animation.ValueAnimator
Public Constructors
ObjectAnimator()
Creates a new ObjectAnimator object.
Public Methods
ObjectAnimator clone()
Creates and returns a copy of this Object.
String getPropertyName()
Gets the name of the property that will be animated.
Object getTarget()
The target object whose property will be animated by this animation
static <T> ObjectAnimator ofFloat(T target, Property<T, Float> property, float... values)
Constructs and returns an ObjectAnimator that animates between float values.
static ObjectAnimator ofFloat(Object target, String propertyName, float... values)
Constructs and returns an ObjectAnimator that animates between float values.
static <T> ObjectAnimator ofInt(T target, Property<T, Integer> property, int... values)
Constructs and returns an ObjectAnimator that animates between int values.
static ObjectAnimator ofInt(Object target, String propertyName, int... values)
Constructs and returns an ObjectAnimator that animates between int values.
static <T, V> ObjectAnimator ofObject(T target, Property<T, V> property, TypeEvaluator<V> evaluator, V... values)
Constructs and returns an ObjectAnimator that animates between Object values.
static ObjectAnimator ofObject(Object target, String propertyName, TypeEvaluator evaluator, Object... values)
Constructs and returns an ObjectAnimator that animates between Object values.
static ObjectAnimator ofPropertyValuesHolder(Object target, 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.
ObjectAnimator setDuration(long duration)
Sets the length of the animation.
void setFloatValues(float... values)
Sets float values that will be animated between.
void setIntValues(int... values)
Sets int values that will be animated between.
void setObjectValues(Object... values)
Sets the values to animate between for this animation.
void setProperty(Property property)
Sets the property that will be animated.
void setPropertyName(String propertyName)
Sets the name of the property that will be animated.
void setTarget(Object target)
Sets the target object whose property will be animated by this animation
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.
String toString()
Returns a string containing a concise, human-readable description of this object.
[Expand]
Inherited Methods
From class android.animation.ValueAnimator
From class android.animation.Animator
From class java.lang.Object

Public Constructors

public ObjectAnimator ()

Added in API level 11

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

public ObjectAnimator clone ()

Added in API level 11

Creates and returns a copy of this Object. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should call super.clone() to create the new instance and then create deep copies of the nested, mutable objects.

Returns
  • a copy of this object.

public String getPropertyName ()

Added in API level 11

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

public Object getTarget ()

Added in API level 11

The target object whose property will be animated by this animation

Returns
  • The object being animated

public static ObjectAnimator ofFloat (T target, Property<T, Float> property, float... values)

Added in API level 14

Constructs and returns an ObjectAnimator that animates between float values. A single value implies that that value is the one being animated to. Two values imply a 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
target The object whose property is to be animated.
property The property being animated.
values A set of values that the animation will animate between over time.
Returns
  • An ObjectAnimator object that is set up to animate between the given values.

public static ObjectAnimator ofFloat (Object target, String propertyName, float... values)

Added in API level 11

Constructs and returns an ObjectAnimator that animates between float values. A single value implies that that value is the one being animated to. Two values imply a 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
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.
propertyName The name of the property being animated.
values A set of values that the animation will animate between over time.
Returns
  • An ObjectAnimator object that is set up to animate between the given values.

public static ObjectAnimator ofInt (T target, Property<T, Integer> property, int... values)

Added in API level 14

Constructs and returns an ObjectAnimator that animates between int values. A single value implies that that value is the one being animated to. Two values imply a 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
target The object whose property is to be animated.
property The property being animated.
values A set of values that the animation will animate between over time.
Returns
  • An ObjectAnimator object that is set up to animate between the given values.

public static ObjectAnimator ofInt (Object target, String propertyName, int... values)

Added in API level 11

Constructs and returns an ObjectAnimator that animates between int values. A single value implies that that value is the one being animated to. Two values imply a 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
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.
propertyName The name of the property being animated.
values A set of values that the animation will animate between over time.
Returns
  • An ObjectAnimator object that is set up to animate between the given values.

public static ObjectAnimator ofObject (T target, Property<T, V> property, TypeEvaluator<V> evaluator, V... values)

Added in API level 14

Constructs and returns an ObjectAnimator that animates between Object values. A single value implies that that value is the one being animated to. Two values imply a 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
target The object whose property is to be animated.
property The property being animated.
evaluator A TypeEvaluator that will be called on each animation frame to provide the necessary interpolation between the Object values to derive the animated value.
values A set of values that the animation will animate between over time.
Returns
  • An ObjectAnimator object that is set up to animate between the given values.

public static ObjectAnimator ofObject (Object target, String propertyName, TypeEvaluator evaluator, Object... values)

Added in API level 11

Constructs and returns an ObjectAnimator that animates between Object values. A single value implies that that value is the one being animated to. Two values imply a 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
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.
propertyName The name of the property being animated.
evaluator A TypeEvaluator that will be called on each animation frame to provide the necessary interpolation between the Object values to derive the animated value.
values A set of values that the animation will animate between over time.
Returns
  • An ObjectAnimator object that is set up to animate between the given values.

public static ObjectAnimator ofPropertyValuesHolder (Object target, PropertyValuesHolder... values)

Added in API level 11

Constructs and returns an ObjectAnimator that animates between the sets of values specified in PropertyValueHolder objects. This variant should be used when animating several properties at once with the same ObjectAnimator, since PropertyValuesHolder allows you to associate a set of animation values with a property name.

Parameters
target The object whose property is to be animated. Depending on how the PropertyValuesObjects were constructed, the target object should either have the Property objects used to construct the PropertyValuesHolder objects or (if the PropertyValuesHOlder objects were created with property names) the target object should have public methods on it called setName(), where name is the name of the property passed in as the propertyName parameter for each of the PropertyValuesHolder objects.
values A set of PropertyValuesHolder objects whose values will be animated between over time.
Returns
  • An ObjectAnimator object that is set up to animate between the given values.

public void setAutoCancel (boolean cancel)

Added in API level 18

autoCancel controls whether an ObjectAnimator will be canceled automatically when any other ObjectAnimator with the same target and properties is started. Setting this flag may make it easier to run different animators on the same target object without having to keep track of whether there are conflicting animators that need to be manually canceled. Canceling animators must have the same exact set of target properties, in the same order.

Parameters
cancel Whether future ObjectAnimators with the same target and properties as this ObjectAnimator will cause this ObjectAnimator to be canceled.

public ObjectAnimator setDuration (long duration)

Added in API level 11

Sets the length of the animation. The default duration is 300 milliseconds.

Parameters
duration The length of the animation, in milliseconds.
Returns
  • ObjectAnimator The object called with setDuration(). This return value makes it easier to compose statements together that construct and then set the duration, as in ObjectAnimator.ofInt(target, propertyName, 0, 10).setDuration(500).start().

public void setFloatValues (float... values)

Added in API level 11

Sets float values that will be animated between. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more values.

If there are already multiple sets of values defined for this ValueAnimator via more than one PropertyValuesHolder object, this method will set the values for the first of those objects.

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

public void setIntValues (int... values)

Added in API level 11

Sets int values that will be animated between. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more values.

If there are already multiple sets of values defined for this ValueAnimator via more than one PropertyValuesHolder object, this method will set the values for the first of those objects.

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

public void setObjectValues (Object... values)

Added in API level 11

Sets the values to animate between for this animation. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more values.

If there are already multiple sets of values defined for this ValueAnimator via more than one PropertyValuesHolder object, this method will set the values for the first of those objects.

There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate between these value objects. ValueAnimator only knows how to interpolate between the primitive types specified in the other setValues() methods.

Parameters
values The set of values to animate between.

public void setProperty (Property property)

Added in API level 14

Sets the property that will be animated. Property objects will take precedence over properties specified by the setPropertyName(String) method. Animations should be set up to use one or the other, not both.

Parameters
property The property being animated. Should not be null.

public void setPropertyName (String propertyName)

Added in API level 11

Sets the name of the property that will be animated. This name is 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.

For best performance of the mechanism that calls the setter function determined by the name of the property being animated, use float or int typed values, and make the setter function for those properties have a void return value. This will cause the code to take an optimized path for these constrained circumstances. Other property types and return types will work, but will have more overhead in processing the requests due to normal reflection mechanisms.

Note that the setter function derived from this property name must take the same parameter type as the valueFrom and valueTo properties, otherwise the call to the setter function will fail.

If this ObjectAnimator has been set up to animate several properties together, using more than one PropertyValuesHolder objects, then setting the propertyName simply sets the propertyName in the first of those PropertyValuesHolder objects.

Parameters
propertyName The name of the property being animated. Should not be null.

public void setTarget (Object target)

Added in API level 11

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

Parameters
target The object being animated

public void setupEndValues ()

Added in API level 11

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.

public void setupStartValues ()

Added in API level 11

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.

public void start ()

Added in API level 11

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

public String toString ()

Added in API level 11

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.