Class ObjectAnimator

All Implemented Interfaces:
AnimationHandler.FrameCallback, Cloneable

public final class ObjectAnimator extends ValueAnimator
ObjectAnimator 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.

Use PropertyValuesHolder and Keyframe can 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 TimeInterpolator.ACCELERATE_DECELERATE will be used.

  • Constructor Details

    • 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.
  • Method Details

    • ofInt

      @Nonnull public static <T> ObjectAnimator ofInt(@Nullable T target, @Nonnull IntProperty<T> property, @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).
      Parameters:
      target - The object whose property is to be animated.
      property - The property being animated.
      values - A set of values (at least 1) that the animation will animate between over time.
      Returns:
      An ObjectAnimator object that is set up to animate between the given values.
    • ofArgb

      @Nonnull public static <T> ObjectAnimator ofArgb(@Nullable T target, @Nonnull IntProperty<T> 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:
      target - The object whose property is to be animated.
      property - The property being animated.
      values - A set of values (at least 1) that the animation will animate between over time.
      Returns:
      An ObjectAnimator object that is set up to animate between the given values.
    • ofFloat

      @Nonnull public static <T> ObjectAnimator ofFloat(@Nullable T target, @Nonnull FloatProperty<T> 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:
      target - The object whose property is to be animated.
      property - The property being animated.
      values - A set of values (at least 1) that the animation will animate between over time.
      Returns:
      An ObjectAnimator object that is set up to animate between the given values.
    • ofObject

      @Nonnull @SafeVarargs public static <T, V> ObjectAnimator ofObject(@Nullable T target, @Nonnull Property<T,V> property, @Nonnull TypeEvaluator<V> evaluator, @Nonnull V... values)
      Constructs and returns an ObjectAnimator that animates between Object 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).

      Note: The values are stored as references to the original objects, which means that changes to those objects after this method is called will affect the values on the animator. If the objects will be mutated externally after this method is called, callers should pass a copy of those objects instead.

      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 (at least 1) that the animation will animate between over time.
      Returns:
      An ObjectAnimator object that is set up to animate between the given values.
    • ofObject

      @Nonnull @SafeVarargs public static <T, V, P> ObjectAnimator ofObject(@Nullable 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. 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). This variant supplies a TypeConverter to convert from the animated values to the type of the property. If only one value is supplied, the TypeConverter must be a BidirectionalTypeConverter to retrieve the current value.

      Note: The values are stored as references to the original objects, which means that changes to those objects after this method is called will affect the values on the animator. If the objects will be mutated externally after this method is called, callers should pass a copy of those objects instead.

      Parameters:
      target - The object whose property is to be animated.
      property - The property being animated.
      converter - Converts the animated object to the Property type.
      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 (at least 1) that the animation will animate between over time.
      Returns:
      An ObjectAnimator object that is set up to animate between the given values.
    • ofPropertyValuesHolder

      @Nonnull public static ObjectAnimator ofPropertyValuesHolder(@Nullable Object target, @Nonnull PropertyValuesHolder... values)
      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. Must not null, but can be empty.
      Returns:
      An ObjectAnimator object that is set up to animate between the given values.
    • setAutoCancel

      public 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. 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.
    • 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, boolean) for any listeners of this animator.
      Overrides:
      start in class ValueAnimator
    • clone

      public ObjectAnimator clone()
      Overrides:
      clone in class ValueAnimator
    • setupStartValues

      public void setupStartValues()
      Description copied from class: Animator
      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.
      Overrides:
      setupStartValues in class Animator
    • setupEndValues

      public void setupEndValues()
      Description copied from class: Animator
      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.
      Overrides:
      setupEndValues in class Animator
    • setDuration

      public ObjectAnimator setDuration(long duration)
      Sets the length of the animation. The default duration is 300 milliseconds.
      Overrides:
      setDuration in class ValueAnimator
      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().
    • getTarget

      @Nullable public Object getTarget()
      The target object whose property will be animated by this animation
      Returns:
      The object being animated
    • setTarget

      public void setTarget(@Nullable Object target)
      Description copied from class: Animator
      Sets the target object whose property will be animated by this animation. Not all subclasses operate on target objects, 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.

      Overrides:
      setTarget in class Animator
      Parameters:
      target - The object being animated