Tuesday, 9 December 2014

API Overview

You can find most of the property animation system's APIs in android.animation. Because the view animation system already defines many interpolators in android.view.animation, you can use those interpolators in the property animation system as well. The following tables describe the main components of the property animation system.
The Animator class provides the basic structure for creating animations. You normally do not use this class directly as it only provides minimal functionality that must be extended to fully support animating values. The following subclasses extend Animator:
Table 1. Animators
ClassDescription
ValueAnimatorThe main timing engine for property animation that also computes the values for the property to be animated. It has all of the core functionality that calculates animation values and contains the timing details of each animation, information about whether an animation repeats, listeners that receive update events, and the ability to set custom types to evaluate. There are two pieces to animating properties: calculating the animated values and setting those values on the object and property that is being animated. ValueAnimator does not carry out the second piece, so you must listen for updates to values calculated by the ValueAnimator and modify the objects that you want to animate with your own logic. See the section about Animating with ValueAnimator for more information.
ObjectAnimatorA subclass of ValueAnimator that allows you to set a target object and object property to animate. This class updates the property accordingly when it computes a new value for the animation. You want to use ObjectAnimator most of the time, because it makes the process of animating values on target objects much easier. However, you sometimes want to use ValueAnimator directly because ObjectAnimator has a few more restrictions, such as requiring specific acessor methods to be present on the target object.
AnimatorSetProvides a mechanism to group animations together so that they run in relation to one another. You can set animations to play together, sequentially, or after a specified delay. See the section about Choreographing multiple animations with Animator Sets for more information.
Evaluators tell the property animation system how to calculate values for a given property. They take the timing data that is provided by an Animator class, the animation's start and end value, and calculate the animated values of the property based on this data. The property animation system provides the following evaluators:
Table 2. Evaluators
Class/InterfaceDescription
IntEvaluatorThe default evaluator to calculate values for int properties.
FloatEvaluatorThe default evaluator to calculate values for float properties.
ArgbEvaluatorThe default evaluator to calculate values for color properties that are represented as hexidecimal values.
TypeEvaluatorAn interface that allows you to create your own evaluator. If you are animating an object property that is not an int, float, or color, you must implement the TypeEvaluator interface to specify how to compute the object property's animated values. You can also specify a custom TypeEvaluator for int, float, and color values as well, if you want to process those types differently than the default behavior. See the section about Using a TypeEvaluator for more information on how to write a custom evaluator.
A time interpolator defines how specific values in an animation are calculated as a function of time. For example, you can specify animations to happen linearly across the whole animation, meaning the animation moves evenly the entire time, or you can specify animations to use non-linear time, for example, accelerating at the beginning and decelerating at the end of the animation. Table 3 describes the interpolators that are contained in android.view.animation. If none of the provided interpolators suits your needs, implement the TimeInterpolator interface and create your own. See Using interpolators for more information on how to write a custom interpolator.
Table 3. Interpolators
Class/InterfaceDescription
AccelerateDecelerateInterpolatorAn interpolator whose rate of change starts and ends slowly but accelerates through the middle.
AccelerateInterpolatorAn interpolator whose rate of change starts out slowly and then accelerates.
AnticipateInterpolatorAn interpolator whose change starts backward then flings forward.
AnticipateOvershootInterpolatorAn interpolator whose change starts backward, flings forward and overshoots the target value, then finally goes back to the final value.
BounceInterpolatorAn interpolator whose change bounces at the end.
CycleInterpolatorAn interpolator whose animation repeats for a specified number of cycles.
DecelerateInterpolatorAn interpolator whose rate of change starts out quickly and and then decelerates.
LinearInterpolatorAn interpolator whose rate of change is constant.
OvershootInterpolatorAn interpolator whose change flings forward and overshoots the last value then comes back.
TimeInterpolatorAn interface that allows you to implement your own interpolator.

Animating with ValueAnimator


The ValueAnimator class lets you animate values of some type for the duration of an animation by specifying a set of int, float, or color values to animate through. You obtain a ValueAnimator by calling one of its factory methods: ofInt(), ofFloat(), or ofObject(). For example:
ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setDuration(1000);
animation.start();
In this code, the ValueAnimator starts calculating the values of the animation, between 0 and 1, for a duration of 1000 ms, when the start() method runs.
You can also specify a custom type to animate by doing the following:
ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);
animation.setDuration(1000);
animation.start();
In this code, the ValueAnimator starts calculating the values of the animation, between startPropertyValue and endPropertyValue using the logic supplied by MyTypeEvaluator for a duration of 1000 ms, when the start() method runs.
The previous code snippets, however, has no real effect on an object, because the ValueAnimator does not operate on objects or properties directly. The most likely thing that you want to do is modify the objects that you want to animate with these calculated values. You do this by defining listeners in the ValueAnimator to appropriately handle important events during the animation's lifespan, such as frame updates. When implementing the listeners, you can obtain the calculated value for that specific frame refresh by calling getAnimatedValue(). For more information on listeners, see the section about Animation Listeners.

No comments:

Post a Comment