Property Animation笔记

本笔记的原文本链接

Property Animation Overview 属性动画总览

The property animation system is a robust framework that allows you to animate almost anything.
属性动画系统是一个健壮的框架,它允许你为任何东西创建动画。
You can define an animation to change any object property over time, regardless of whether it draws to the screen or not.
你可以定义一个随时间改变任何对象属性的动画,无论他是否绘制在屏幕上。
A property animation changes a property's (a field in an object) value over a specified length of time.
属性动画会在指定的时间长度内更改属性的(对象中的字段)值。
To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.
为某个东西创建动画,你需要指定你想要创建动画的这个对象的属性,例如一个对象在屏幕上的位置,你想要为它动画多久,和你想要在什么值之间动画。
The property animation system lets you define the following characteristics of an animation:
属性动画系统允许你定义动画以下的这些的特性:

  • Duration: You can specify the duration of an animation. The default length is 300 ms.
  • 持续时间:你可以指定动画的持续时间。默认的长度是300ms
  • Time interpolation: You can specify how the values for the property are calculated as a function of the animation's current elapsed time.
  • 时间插值:您可以指定属性的值如何作为动画当前流逝时间的函数进行计算的。
  • Repeat count and behavior: You can specify whether or not to have an animation repeat when it reaches the end of a duration and how many times to repeat the animation. You can also specify whether you want the animation to play back in reverse. Setting it to reverse plays the animation forwards then backwards repeatedly, until the number of repeats is reached.
  • 重复计数和行为:当动画到达持续时间的终端时,你可以定义动画是否重复和重复次数。你还可以指定是否想要动画反向播放。设置为反向播放动画向前,然后向后重复,直到达到重复的数量。设置它反向向前播放这个动画,然后向后重复,直到重复次数到达为止。
  • Animator sets: You can group animations into logical sets that play together or sequentially or after specified delays.
  • 动画集:你可以将动画分组为逻辑集合,它们在一起或按顺序或在指定的延迟之后播放。
  • Frame refresh delay: You can specify how often to refresh frames of your animation. The default is set to refresh every 10 ms, but the speed in which your application can refresh frames is ultimately dependent on how busy the system is overall and how fast the system can service the underlying timer.
  • 帧刷新延迟:你可以指定刷新动画帧的频率。默认设置为每10毫秒刷新一次,但应用程序可以刷新帧的速度最终取决于整个系统的繁忙程度以及系统能为底层定时器服务的速度。

How Property Animation Works 属性动画是如何工作的

First, let's go over how an animation works with a simple example.
首先,让我们通过一个简单的示例走一遍动画是如何工作的。
Figure 1 depicts a hypothetical object that is animated with its x property, which represents its horizontal location on a screen. The duration of the animation is set to 40 ms and the distance to travel is 40 pixels.
图1描述了一个假想对象,其对象是x属性,表示它在屏幕上的水平位置。动画的持续时间设置为40毫秒,移动距离为40像素。
Every 10 ms, which is the default frame refresh rate, the object moves horizontally by 10 pixels.
每10ms,这是默认的帧刷新率,这个对象水平移动10像素。这个40ms的终点。
At the end of 40ms, the animation stops, and the object ends at horizontal position 40.
在40ms,动画停止,同时这个对象停止在水平位置40的地方。
This is an example of an animation with linear interpolation, meaning the object moves at a constant speed.
这是一个具有水平线性插值的动画示例,意味着这个对象以恒定的速度移动。


Figure 1. Example of a linear animation

You can also specify animations to have a non-linear interpolation.
你也可以指定具有非线性插值的动画。
Figure 2 illustrates a hypothetical object that accelerates at the beginning of the animation, and decelerates at the end of the animation.
图2说明了一个假设的对象,在动画开始时加速,然后在动画结束时减速。
The object still moves 40 pixels in 40 ms, but non-linearly. In the beginning, this animation accelerates up to the halfway point then decelerates from the halfway point until the end of the animation.
这个对象仍然在40ms移动40像素,但是是非线性的。一开始,这个动画加速直到中间点。然后,从中间点减速直到动画结束。
As Figure 2 shows, the distance traveled at the beginning and end of the animation is less than in the middle.
如图2所示,动画开始和结束的一段时间内所走的距离小于中间一段时间的距离。


Figure 2. Example of a non-linear animation

Let's take a detailed look at how the important components of the property animation system would calculate animations like the ones illustrated above.
让我们详细看看属性动画系统的重要组件如何计算上面提到的动画。
Figure 3 depicts how the main classes work with one another.
图3描述了主要的类是如何彼此工作的。


Figure 3. How animations are calculated

The ValueAnimator object keeps track of your animation's timing, such as how long the animation has been running, and the current value of the property that it is animating.
ValueAnimator对象保持追踪你的动画的时间,例如:这个动画已经运行了多久,和它正在动画的属性当前的值。
The ValueAnimator encapsulates a TimeInterpolator, which defines animation interpolation, and a TypeEvaluator, which defines how to calculate values for the property being animated.
ValueAnimator封装了一个TimeInterpolator,它定义了动画插值,和一个TypeEvaluator,它定义了如何计算将被动画的属性的值。
For example, in Figure 2, the TimeInterpolator used would be AccelerateDecelerateInterpolator and the TypeEvaluator would be IntEvaluator.
例如,在图2中,使用的时间插值器将会是AccelerateDecelerateInterpolator,和类型计算器就将会是IntEvaluator。
To start an animation, create a ValueAnimator and give it the starting and ending values for the property that you want to animate, along with the duration of the animation.
为了开启一个动画,创建一个ValueAnimator和赋予它的属性你想要的动画的开始和结束的值,以及动画的持续时间。
When you call start() the animation begins.

当你调用start()方法,动画开启。
During the whole animation, the ValueAnimator calculates an elapsed fraction between 0 and 1, based on the duration of the animation and how much time has elapsed.
在整个动画中,ValueAnimator计算在0和1之间的过去的分数,基于动画的持续时间和经过了多少时间。
The elapsed fraction represents the percentage of time that the animation has completed, 0 meaning 0% and 1 meaning 100%.
这个过去的分数表现了动画完成的时间百分比,0意味着0%和1意味着100%。
For example, in Figure 1, the elapsed fraction at t = 10 ms would be .25 because the total duration is t = 40 ms.
例如,在图1中,过去的分数在t=10ms时将会是0.25,因为总共持续时间的时为t=40ms。
When the ValueAnimator is done calculating an elapsed fraction, it calls the TimeInterpolator that is currently set, to calculate an interpolated fraction.
当ValueAnimator完成计算过去的分数时,它将会调用当前设置的TimeInterpolator,去计算插值分数。
An interpolated fraction maps the elapsed fraction to a new fraction that takes into account the time interpolation that is set.
内插分数将流逝分数映射到考虑了设置的时间内插的新分数。
For example, in Figure 2, because the animation slowly accelerates, the interpolated fraction, about .15, is less than the elapsed fraction, .25, at t = 10 ms.
例如,在图2中,由于动画缓慢加速,因此在t = 10 ms时,插值分数约为0.15,小于流逝分数.25。 在图1中,内插分数总是与流逝分数相同。
例如,在图2中,由于动画缓慢加速,在t=10ms时刻,插值分数约为0.15,小于流逝分数0.25。
In Figure 1, the interpolated fraction is always the same as the elapsed fraction.
在图1中,插值分数总是和消逝分数相等。
When the interpolated fraction is calculated, ValueAnimator calls the appropriate TypeEvaluator, to calculate the value of the property that you are animating, based on the interpolated fraction, the starting value, and the ending value of the animation.
当插值分数被计算出来,ValueAnimator根据插值分数、动画开始和结束的值,调用合适的TypeEvaluator去计算你正在动画的属性的值。
For example, in Figure 2, the interpolated fraction was .15 at t = 10 ms, so the value for the property at that time would be .15 X (40 - 0), or 6.
例如,在图2中,在t=10ms时,插值分数是0.15,所以此时属性将会是0.15*(40-0)即6.
The com.example.android.apis.animation package in the API Demos sample project provides many examples on how to use the property animation system.
API Demos示例项目中的com.example.android.apis.animation包提供了许多有关如何使用属性动画系统的示例。

How Property Animation Differs from View Animation 属性动画与View动画的差别

The view animation system provides the capability to only animate View objects, so if you wanted to animate non-View objects, you have to implement your own code to do so.
View动画系统提供了仅对View对象进行动画处理的功能。因此如果你想为非View对象设置动画效果,你不得不实现自己的代码来执行操作。
The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotation of a View but not the background color, for instance.
View动画也受限于这样一个事实,它仅暴露了View对象的一些方面,例如缩放和旋转一个View,但是不包括背景颜色。
Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself.
视图动画系统的另一个缺点是只能在视图被绘制的地方进行修改,而不是实际的视图本身。
For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.
例如,如果你使按钮动画的穿过屏幕,按钮被正确的绘制,但是事实上你点击按钮的位置却没有改变,所以你必须实现自己的逻辑来处理这个问题。
With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified.
使用属性动画系统,这些约束被完全删除,并且可以为任何对象(视图和非视图)的任何属性设置动画,并且对象本身实际上被修改。
The property animation system is also more robust in the way it carries out animation.
动画属性系统在执行动画时也更加强壮。
At a high level, you assign animators to the properties that you want to animate, such as color, position, or size and can define aspects of the animation such as interpolation and synchronization of multiple animators.
从较高层次上讲,你可以分配动画到你想要动画的属性上,例如颜色、位置、或大小和可以定义动画的方面如:插值器和多个同步动画。
The view animation system, however, takes less time to setup and requires less code to write.
然而,View动画系统,花费更少的时间去设置和需要写更少的代码。
If view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system.
如果动画完成了你需要执行的所有事,或者你已有的代码已经按照你想要的方式运行,则无需使用属性动画系统。
It also might make sense to use both animation systems for different situations if the use case arises.
如果出现用例,那么在不同情况下使用这两种动画系统也是有意义的。

API Overview API总览

Class Description
ValueAnimator The 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.
ObjectAnimator A 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.
AnimatorSet Provides 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.

You can find most of the property animation system's APIs in android.animation.
你可以在android.animation中找到大部分属性动画系统的API。
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.
由于View动画系统在android.view.animation中已经定义了许多插值器,同样你也可以在属性动画中使用这些插值器。
The following tables describe the main components of the property animation system.
接下来的这些介绍了属性动画系统的主要的组件。
The Animator class provides the basic structure for creating animations.
Animator类提供了创建动画的基础结构。
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:
以下的子类继承自Animator:
Table 1. Animators

Class Description
ValueAnimator The 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.
ObjectAnimator A 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.
AnimatorSet Provides 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.
Class/Interface Description
IntEvaluator The default evaluator to calculate values for int properties.
FloatEvaluator The default evaluator to calculate values for float properties.
ArgbEvaluator The default evaluator to calculate values for color properties that are represented as hexidecimal values.
TypeEvaluator An 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.

Evaluators tell the property animation system how to calculate values for a given property.
Evaluators告诉属性动画系统如何为给定的属性计算值。
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.
他们获取Animator类提供的时间数据,动画开始和终止的值,然后基于这些数据计算这个属性的动画值。
The property animation system provides the following evaluators:
动画属性系统提供一下Evaluators:
Table 2. Evaluators

Class/Interface Description
IntEvaluator The default evaluator to calculate values for int properties.
FloatEvaluator The default evaluator to calculate values for float properties.
ArgbEvaluator The default evaluator to calculate values for color properties that are represented as hexidecimal values.
TypeEvaluator An 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.
Class/Interface Description
AccelerateDecelerateInterpolator An interpolator whose rate of change starts and ends slowly but accelerates through the middle.
AccelerateInterpolator An interpolator whose rate of change starts out slowly and then accelerates.
AnticipateInterpolator An interpolator whose change starts backward then flings forward.一个插值器,其变化开始后向前闪烁。
AnticipateOvershootInterpolator An interpolator whose change starts backward, flings forward and overshoots the target value, then finally goes back to the final value.
BounceInterpolator An interpolator whose change bounces at the end.内插器的变化在最后反弹。
CycleInterpolator An interpolator whose animation repeats for a specified number of cycles.
DecelerateInterpolator An interpolator whose rate of change starts out quickly and then decelerates.
LinearInterpolator An interpolator whose rate of change is constant.
OvershootInterpolator An interpolator whose change flings forward and overshoots the last value then comes back.
TimeInterpolator An interface that allows you to implement your own interpolator.

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.
表3说明了包含在android.view.animation中的插值器。
If none of the provided interpolators suits your needs, implement the TimeInterpolator interface and create your own.
如果提供的插值器没有一个适合你使用,创建一个你自己的实现了TimeInterpolator接口插值器。
See Using interpolators for more information on how to write a custom interpolator.
查看使用插值器获取更多关于如何写一个自定义插值器的信息。(有关如何编写自定义插补器的更多信息,请参见使用插补器。 备注的是谷歌翻译的,谷歌翻译就是牛逼)。
Table 3. Interpolators

Class/Interface Description
AccelerateDecelerateInterpolator An interpolator whose rate of change starts and ends slowly but accelerates through the middle.
AccelerateInterpolator An interpolator whose rate of change starts out slowly and then accelerates.
AnticipateInterpolator An interpolator whose change starts backward then flings forward.一个插值器,其变化开始后向前闪烁。
AnticipateOvershootInterpolator An interpolator whose change starts backward, flings forward and overshoots the target value, then finally goes back to the final value.
BounceInterpolator An interpolator whose change bounces at the end.内插器的变化在最后反弹。
CycleInterpolator An interpolator whose animation repeats for a specified number of cycles.
DecelerateInterpolator An interpolator whose rate of change starts out quickly and then decelerates.
LinearInterpolator An interpolator whose rate of change is constant.
OvershootInterpolator An interpolator whose change flings forward and overshoots the last value then comes back.
TimeInterpolator An interface that allows you to implement your own interpolator.

Animating with ValueAnimator 使用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.
通过指定一组int值,浮点值或颜色值来设置动画效果,ValueAnimator类可以为动画的持续时间设置某种类型的值的动画。
You obtain a ValueAnimator by calling one of its factory methods: ofInt(), ofFloat(), or ofObject(). For example:
通过调用它的任一工厂方法:onInt()、ofFloat()、或者ofObject(),你可以获取一个ValueAnimator。例如:

ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
animation.setDuration(1000);
animation.start();

In this code, the ValueAnimator starts calculating the values of the animation, between 0 and 100, for a duration of 1000 ms,when the start() method runs.
在这份代码中,当start()方法运行时,ValueAnimator开始计算在0与100之间,持续时间为1000ms的动画的值。
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.
在这份代码中,ValueAnimator开始使用MyTypeEvaluator提供的逻辑在startPropertyValue和endPropertyValue以及持续时间1000ms的条件下计算这个动画的值。

You can use the values of the animation by adding an AnimatorUpdateListener to the ValueAnimator object, as shown in the following code:
你可以使用这个动画的值通过给ValueAnimator来添加一个AnimatorUpdateListener,如以下代码展示的这样。

animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator updatedAnimation) {
        // You can use the animated value in a property that uses the
        // same type as the animation. In this case, you can use the
        // float value in the translationX property.
        float animatedValue = (float)updatedAnimation.getAnimatedValue();
        textView.setTranslationX(animatedValue);
    }
});

In the onAnimationUpdate() method you can access the updated animation value and use it in a property of one of your views. For more information on listeners, see the section about Animation Listeners
在onAnimationUpdate()方法中,你可以访问更新后的动画值,并将其用于其中一个视图的属性中。

Animating with ObjectAnimator 使用ObjectAnimator创建动画

The ObjectAnimator is a subclass of the ValueAnimator (discussed in the previous section) and combines the timing engine and value computation of ValueAnimator with the ability to animate a named property of a target object.
ObjectAnimator是ValueAnimator的一个子类(在上一节中讨论过),它将ValueAnimator的时序引擎和值计算与对目标对象的命名属性进行动画处理的功能相结合。
This makes animating any object much easier, as you no longer need to implement the ValueAnimator.AnimatorUpdateListener, because the animated property updates automatically.
这使得任何对象的动画更容易,因为你不再需要实现ValueAnimator.AnimatorUpdateListener,因为动画属性会自动更新。
Instantiating an ObjectAnimator is similar to a ValueAnimator, but you also specify the object and the name of that object's property (as a String) along with the values to animate between:
实例化一个ObjectAnimator类似于一个ValueAnimator,但是你还需要指定该对象的属性名称(作为字符串)以及值之间进行动画处理:

ObjectAnimator animation = ObjectAnimator.ofFloat(textView, "translationX", 100f);
animation.setDuration(1000);
animation.start();

To have the ObjectAnimator update properties correctly, you must do the following:
要正确的更新ObjectAnimator属性,你必须执行以下操作:

  • The object property that you are animating must have a setter function (in camel case) in the form of set<PropertyName>(). Because the ObjectAnimator automatically updates the property during animation, it must be able to access the property with this setter method. For example, if the property name is foo, you need to have a setFoo() method.
    你正在设置动画的对象属性必须以set()的形式具有setter函数(骆驼大小写)。 由于ObjectAnimator在动画期间自动更新属性,因此必须能够使用此setter方法访问属性。 例如,如果属性名称是foo,则需要有一个setFoo()方法。
    If this setter method does not exist, you have three options:
    如果setter方法不存在,你拥有以下三个选择:
    • Add the setter method to the class if you have the rights to do so.在这个类中添加setter方法,如果你有权限这样做。
    • Use a wrapper class that you have rights to change and have that wrapper receive the value with a valid setter method and forward it to the original object.使用你有权修改的包装类,并让该包装接收有效的setter方法的值并将其转发给原始对象。
    • Use ValueAnimator instead.使用ValueAnimator替代
  • If you specify only one value for the values... parameter in one of the ObjectAnimator factory methods, it is assumed to be the ending value of the animation. Therefore, the object property that you are animating must have a getter function that is used to obtain the starting value of the animation. The getter function must be in the form of get<PropertyName>(). For example, if the property name is foo, you need to have a getFoo() method.如果你在其中一个ObjectAnimator工厂方法中为values ...参数指定了仅一个值,则它被假定为动画的结束值。 因此,你正在动画的对象属性必须具有用于获取动画起始值的getter函数。 getter函数必须采用get <PropertyName>()的形式。 例如,如果属性名称是foo,则需要有一个getFoo()方法。
  • The getter (if needed) and setter methods of the property that you are animating must operate on the same type as the starting and ending values that you specify to ObjectAnimator. For example, you must have targetObject.setPropName(float) and targetObject.getPropName(float) if you construct the following ObjectAnimator:你要动画的属性的getter(如果需要)和setter方法必须以与指定给ObjectAnimator的开始和结束值相同的类型进行操作。 例如,如果构造以下ObjectAnimator,则必须具有targetObject.setPropName(float)和targetObject.getPropName(float):
ObjectAnimator.ofFloat(targetObject, "propName", 1f)
  • Depending on what property or object you are animating, you might need to call the invalidate() method on a View to force the screen to redraw itself with the updated animated values. You do this in the onAnimationUpdate() callback. For example, animating the color property of a Drawable object only causes updates to the screen when that object redraws itself. All of the property setters on View, such as setAlpha() and setTranslationX() invalidate the View properly, so you do not need to invalidate the View when calling these methods with new values. For more information on listeners, see the section about Animation Listeners.根据你要动画的属性或对象,你可能需要在视图上调用invalidate()方法以强制屏幕使用更新的动画值重绘本身。 你可以在onAnimationUpdate()回调中执行此操作。 例如,对Drawable对象的颜色属性进行动画处理只会导致该对象重绘时自动更新屏幕。View所有的属性设置器,例如setAlpha()和setTranslationX()都会正确的废止View,因此当调用这些方法时,你不需要使View无效。有关侦听器的更多信息,请参阅关于动画侦听器的部分。

Choreographing Multiple Animations with AnimatorSet 用AnimatorSet编排多个动画

In many cases, you want to play an animation that depends on when another animation starts or finishes.
在许多情况下,你想播放取决于另外一个动画的开始或结束的动画。
The Android system lets you bundle animations together into an AnimatorSet, so that you can specify whether to start animations simultaneously, sequentially, or after a specified delay. You can also nest AnimatorSet objects within each other.
Android系统让你将动画捆绑到一个AnimatorSet中,以便你指定动画立刻、按照顺序、或者在一个特定的延迟开始播放。你也可以将AnimatorSet对象嵌套在对方内。
The following sample code taken from the Bouncing Balls sample (modified for simplicity) plays the following Animator objects in the following manner:
接下来摘自Bouncing Balls示例(简单修改)的示例代码按以下方式播放以下Animator对象:

  1. Plays bounceAnim.
  2. Plays squashAnim1, squashAnim2, stretchAnim1, and stretchAnim2 at the same time.
  3. Plays bounceBackAnim.
  4. Plays fadeAnim.
AnimatorSet bouncer = new AnimatorSet();
bouncer.play(bounceAnim).before(squashAnim1);
bouncer.play(squashAnim1).with(squashAnim2);
bouncer.play(squashAnim1).with(stretchAnim1);
bouncer.play(squashAnim1).with(stretchAnim2);
bouncer.play(bounceBackAnim).after(stretchAnim2);
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(bouncer).before(fadeAnim);
animatorSet.start();

For a more complete example on how to use animator sets, see the Bouncing Balls sample in APIDemos.
关于如何使用animator集合更完整的示例,请查看在APIDemos中的Bouncing Balls示例。

Animation Listeners 动画侦听者

You can listen for important events during an animation's duration with the listeners described below.
在动画持续时间内,你可以侦听以下描述的侦听器中的重要事件。

  • Animator.AnimatorListener
    • onAnimationStart() - Called when the animation starts.
    • onAnimationEnd() - Called when the animation ends.
    • onAnimationRepeat() - Called when the animation repeats itself.
    • onAnimationCancel() - Called when the animation is canceled. A cancelled animation also calls onAnimationEnd(), regardless of how they were ended.
  • ValueAnimator.AnimatorUpdateListener
    • onAnimationUpdate() - called on every frame of the animation. Listen to this event to use the calculated values generated by ValueAnimator during an animation. To use the value, query the ValueAnimator object passed into the event to get the current animated value with the getAnimatedValue() method. Implementing this listener is required if you use ValueAnimator.
      Depending on what property or object you are animating, you might need to call invalidate() on a View to force that area of the screen to redraw itself with the new animated values. For example, animating the color property of a Drawable object only cause updates to the screen when that object redraws itself. All of the property setters on View, such as setAlpha() and setTranslationX() invalidate the View properly, so you do not need to invalidate the View when calling these methods with new values.

Animating Layout Changes to ViewGroups 动画布局更改为ViewGroups

The property animation system provides the capability to animate changes to ViewGroup objects as well as provide an easy way to animate View objects themselves.
属性动画系统提供了对ViewGroup对象进行动画变化的功能,并提供了一种简单的方法来为View对象本身制作动画。
You can animate layout changes within a ViewGroup with the LayoutTransition class.
你可以使用LayoutTransition类对ViewGroup中的布局更改进行动画处理。
Views inside a ViewGroup can go through an appearing and disappearing animation when you add them to or remove them from a ViewGroup or when you call a View's setVisibility() method with VISIBLE, INVISIBLE, or GONE.
ViewGroup中的视图可以在将ViewGroup添加到ViewGroup中或从ViewGroup中删除它们时,或者当你使用VISIBLE,INVISIBLE或GONE调用View的setVisibility()方法时,经过出现和消失的动画。
The remaining Views in the ViewGroup can also animate into their new positions when you add or remove Views.
当添加或删除视图时,ViewGroup中的其余视图也可以动画到他们的新位置。
You can define the following animations in a LayoutTransition object by calling setAnimator() and passing in an Animator object with one of the following LayoutTransition constants:
你可以通过调用setAnimator()并使用下列LayoutTransition常量之一传入Animator对象来在LayoutTransition对象中定义以下动画:

  • APPEARING - A flag indicating the animation that runs on items that are appearing in the container.
    CHANGE_APPEARING - A flag indicating the animation that runs on items that are changing due to a new item appearing in the container.
  • DISAPPEARING - A flag indicating the animation that runs on items that are disappearing from the container.
  • CHANGE_DISAPPEARING - A flag indicating the animation that runs on items that are changing due to an item disappearing from the container.

You can define your own custom animations for these four types of events to customize the look of your layout transitions or just tell the animation system to use the default animations.
你可以为这四种类型的事件定义自己的自定义动画,以自定义布局转换的外观,或者仅告诉动画系统使用默认动画。

The LayoutAnimations sample in API Demos shows you how to define animations for layout transitions and then set the animations on the View objects that you want to animate.
API演示中的LayoutAnimations示例向你展示了如何为布局转换定义动画,然后在你想要制作动画的视图对象上设置动画。
The LayoutAnimationsByDefault and its corresponding layout_animations_by_default.xml layout resource file show you how to enable the default layout transitions for ViewGroups in XML. The only thing that you need to do is to set the android:animateLayoutchanges attribute to true for the ViewGroup. For example:
LayoutAnimationsByDefault及其相应的layout_animations_by_default.xml布局资源文件显示如何为XML中的ViewGroups启用默认布局转换。 唯一需要做的就是将ViewGroup的android:animateLayoutchanges属性设置为true。 例如:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/verticalContainer"
    android:animateLayoutChanges="true" />

Setting this attribute to true automatically animates Views that are added or removed from the ViewGroup as well as the remaining Views in the ViewGroup.
将此属性设置为true会自动生成从ViewGroup添加或删除的视图以及ViewGroup中剩余的视图。

Using StateListAnimator to animate view state changes 使用StateListAnimator创建视图状态变更动画

The StateListAnimator class lets you define animators that run when the state of a view changes.
StateListAnimator类允许你定义在View的状态变化时运行的动画。
This object behaves as a wrapper for an Animator object, calling that animation whenever the specified view state (such as "pressed" or "focused") changes.
该对象充当动画对象的包装器,每当指定的视图状态(如“按下”或“焦点”)更改时调用该动画。
The StateListAnimator can be defined in an XML resource with a root <selector> element and child <item> elements that each specify a different view state defined by the StateListAnimator class.
StateListAnimator可以在具有根<selector>元素和子元素<item>的XML资源中定义,每个子元素指定由StateListAnimator类定义的不同视图状态。
Each <item> contains the definition for a property animation set.
每一个<item>包含这个属性动画集合的定义。
For example, the following file creates a state list animator that changes the x and y scale of the view when it's pressed:
举个栗子,下面的文件创建了一个状态列表动画器,用于当视图被按压时改变它的x和y的比例。
res/xml/animate_scale.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- the pressed state; increase x and y size to 150% -->
    <item android:state_pressed="true">
        <set>
            <objectAnimator android:propertyName="scaleX"
                android:duration="@android:integer/config_shortAnimTime"
                android:valueTo="1.5"
                android:valueType="floatType"/>
            <objectAnimator android:propertyName="scaleY"
                android:duration="@android:integer/config_shortAnimTime"
                android:valueTo="1.5"
                android:valueType="floatType"/>
        </set>
    </item>
    <!-- the default, non-pressed state; set x and y size to 100% -->
    <item android:state_pressed="false">
        <set>
            <objectAnimator android:propertyName="scaleX"
                android:duration="@android:integer/config_shortAnimTime"
                android:valueTo="1"
                android:valueType="floatType"/>
            <objectAnimator android:propertyName="scaleY"
                android:duration="@android:integer/config_shortAnimTime"
                android:valueTo="1"
                android:valueType="floatType"/>
        </set>
    </item>
</selector>

To attach the state list animator to a view, add the android:stateListAnimator attribute as follows:
要将状态列表动画添加到视图,请按如下所示添加android:stateListAnimator属性:

<Button android:stateListAnimator="@xml/animate_scale"
        ... />

Now the animations defined in animate_scale.xml are used when this button's state changes.
当这个按钮的状态改变,当前定义在animate_scale.xml动画被使用。
Or to instead assign a state list animator to a view in your code, use the AnimatorInflater.loadStateListAnimator() method, and assign the animator to your view with the View.setStateListAnimator() method.
或者改为将状态列表动画器分配给代码中的视图,请使用AnimatorInflater.loadStateListAnimator()方法,并使用View.setStateListAnimator()方法将动画器分配给视图。
Or instead of animating properties of the view, you can play a drawable animation between state changes, using AnimatedStateListDrawable.
或者,你可以使用AnimatedStateListDrawable在状态更改之间播放可绘制动画,而不是动画视图的属性。
Some of the system widgets in Android 5.0 use these animations by default. The following example shows how to define an AnimatedStateListDrawable as an XML resource:
Android 5.0中的一些系统小部件默认使用这些动画。 以下示例显示如何将AnimatedStateListDrawable定义为XML资源:

<!-- res/drawable/myanimstatedrawable.xml -->
<animated-selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- provide a different drawable for each state-->
    <item android:id="@+id/pressed" android:drawable="@drawable/drawableP"
        android:state_pressed="true"/>
    <item android:id="@+id/focused" android:drawable="@drawable/drawableF"
        android:state_focused="true"/>
    <item android:id="@id/default"
        android:drawable="@drawable/drawableD"/>

    <!-- specify a transition -->
    <transition android:fromId="@+id/default" android:toId="@+id/pressed">
        <animation-list>
            <item android:duration="15" android:drawable="@drawable/dt1"/>
            <item android:duration="15" android:drawable="@drawable/dt2"/>
            ...
        </animation-list>
    </transition>
    ...
</animated-selector>

Using a TypeEvaluator 使用TypeEvaluator

If you want to animate a type that is unknown to the Android system, you can create your own evaluator by implementing the TypeEvaluator interface.
如果你为一个Android系统不知道的类型创建动画,你可以通过实现TypeEvaluator接口创建你自己的Evaluator。
The types that are known by the Android system are int, float, or a color, which are supported by the IntEvaluator, FloatEvaluator, and ArgbEvaluator type evaluators.
Android系统所熟知的类型是int,float或color,它们通过IntEvaluator,FloatEvaluator和ArgbEvaluator类型Evaluator。
There is only one method to implement in the TypeEvaluator interface, the evaluate() method.
在TypeEvaluator接口中只有一个方法需要执行evaluate()方法。
This allows the animator that you are using to return an appropriate value for your animated property at the current point of the animation.
这允许你正在使用的动画在动画当前点为你的动画属性返回适当的值。
The FloatEvaluator class demonstrates how to do this:
FloatEvaluator类演示了如何执行此操作:

public class FloatEvaluator implements TypeEvaluator {

    public Object evaluate(float fraction, Object startValue, Object endValue) {
        float startFloat = ((Number) startValue).floatValue();
        return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);
    }
}

Note: When ValueAnimator (or ObjectAnimator) runs, it calculates a current elapsed fraction of the animation (a value between 0 and 1) and then calculates an interpolated version of that depending on what interpolator that you are using. The interpolated fraction is what your TypeEvaluator receives through the fraction parameter, so you do not have to take into account the interpolator when calculating animated values.
注意:当ValueAnimator(或ObjectAnimator)运行时,它会计算动画当前流逝的部分(介于0和1之间的一个值),然后根据你使用的插值器计算插值版本。内插分数是TypeEvaluator通过分数参数接收的值,因此计算动画值时不必考虑插值器。

Using Interpolators 使用插值器

An interpolator define 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, using acceleration or deceleration at the beginning or end of the animation.
举个例子,你可以指定动画在整个动画中线性发生,意味着动画整个时间内平均的移动,或者你可以指定动画使用非线性时间,例如,在动画开始时加速和结束时减速。
Interpolators in the animation system receive a fraction from Animators that represent the elapsed time of the animation.
动画系统中的插值器会接收来自动画器的分数,表示动画已用时间。
Interpolators modify this fraction to coincide with the type of animation that it aims to provide.
插值器修改此分数以符合它旨在提供的动画类型。
The Android system provides a set of common interpolators in the android.view.animation package.
Android系统在android.view.animation包中提供一组常用的插值算法。
If none of these suit your needs, you can implement the TimeInterpolator interface and create your own.
如果这些都不符合你的需求,你可以实现TimeInterpolator接口并创建你自己的。
As an example, how the default interpolator AccelerateDecelerateInterpolator and the LinearInterpolator calculate interpolated fractions are compared below.
例如,下面比较默认插值器AccelerateDecelerateInterpolator和LinearInterpolator计算插值分数的方式。
The LinearInterpolator has no effect on the elapsed fraction. The AccelerateDecelerateInterpolator accelerates into the animation and decelerates out of it. The following methods define the logic for these interpolators:
LinearInterpolator对经过的分数没有影响。 AccelerateDecelerateInterpolator加速进入动画并减速。 以下方法定义了这些内插器的逻辑:
AccelerateDecelerateInterpolator

public float getInterpolation(float input) {
    return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
}

LinearInterpolator

public float getInterpolation(float input) {
    return input;
}
ms elapsed Elapsed fraction/Interpolated fraction (Linear) Interpolated fraction (Accelerate/Decelerate)
0 0 0
200 .2 .1
400 .4 .345
600 .6 .8
800 .8 .9
1000 1 1

The following table represents the approximate values that are calculated by these interpolators for an animation that lasts 1000ms:
下表表示这些插补器对持续1000毫秒的动画计算的近似值:

ms elapsed Elapsed fraction/Interpolated fraction (Linear) Interpolated fraction (Accelerate/Decelerate)
0 0 0
200 .2 .1
400 .4 .345
600 .6 .8
800 .8 .9
1000 1 1

As the table shows, the LinearInterpolator changes the values at the same speed, .2 for every 200ms that passes.
如表所示,LinearInterpolator以相同的速度更改值,每200ms传递一次。
The AccelerateDecelerateInterpolator changes the values faster than LinearInterpolator between 200ms and 600ms and slower between 600ms and 1000ms.
AccelerateDecelerateInterpolator在200ms和600ms之间比LinearInterpolator更快地改值,在600ms和1000ms之间慢一些。

Specifying Keyframes 指定关键帧

A Keyframe object consists of a time/value pair that lets you define a specific state at a specific time of an animation.
一个Keyframe对象由一个时间/值对组成,它允许你在动画的特定时间定义特定的状态。
Each keyframe can also have its own interpolator to control the behavior of the animation in the interval between the previous keyframe's time and the time of this keyframe.
每个关键帧还可以有自己的插补器,以控制前一个关键帧时间与该关键帧时间之间的间隔内的动画行为。
To instantiate a Keyframe object, you must use one of the factory methods, ofInt(), ofFloat(), or ofObject() to obtain the appropriate type of Keyframe.
要实例化一个Keyframe对象,必须使用工厂方法之一的Int(),ofFloat()或ofObject()来获取适当类型的Keyframe。
You then call the ofKeyframe() factory method to obtain a PropertyValuesHolder object.
然后调用ofKeyframe()工厂方法来获取PropertyValuesHolder对象。
Once you have the object, you can obtain an animator by passing in the PropertyValuesHolder object and the object to animate.
获得对象后,可以通过传递PropertyValuesHolder对象和对象来获取动画。
The following code snippet demonstrates how to do this:
以下代码片段演示了如何执行此操作:

Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
Keyframe kf1 = Keyframe.ofFloat(.5f, 360f);
Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation)
rotationAnim.setDuration(5000ms);

For a more complete example on how to use keyframes, see the MultiPropertyAnimation sample in APIDemos.

Animating Views

The property animation system allow streamlined animation of View objects and offers a few advantages over the view animation system.
属性动画系统允许View对象的简化动画,并且与视图动画系统相比具有一些优点。
The view animation system transformed View objects by changing the way that they were drawn.
视图动画系统通过改变它们被绘制的方式来转换视图对象。
This was handled in the container of each View, because the View itself had no properties to manipulate.
这是在每个视图的容器中处理的,因为视图本身没有可操作的属性。
This resulted in the View being animated, but caused no change in the View object itself. This led to behavior such as an object still existing in its original location, even though it was drawn on a different location on the screen.
这导致视图被动画,但在视图对象本身中没有改变。 这导致行为,例如对象仍然存在于其原始位置,即使它是在屏幕上的不同位置绘制的。
In Android 3.0, new properties and the corresponding getter and setter methods were added to eliminate this drawback.
在Android 3.0中,添加了新的属性和相应的getter和setter方法来消除这个缺点。
The property animation system can animate Views on the screen by changing the actual properties in the View objects.
属性动画系统可以通过改变视图对象中的实际属性来在屏幕上动画视图。
In addition, Views also automatically call the invalidate() method to refresh the screen whenever its properties are changed.
另外,视图也会自动调用invalidate()方法刷新屏幕,只要其属性发生更改。
The new properties in the View class that facilitate property animations are:
有助于属性动画的View类中的新属性是:

  • translationX and translationY: These properties control where the View is located as a delta from its left and top coordinates which are set by its layout container.
  • rotation, rotationX, and rotationY: These properties control the rotation in 2D (rotation property) and 3D around the pivot point.
  • scaleX and scaleY: These properties control the 2D scaling of a View around its pivot point.
  • pivotX and pivotY: These properties control the location of the pivot point, around which the rotation and scaling transforms occur. By default, the pivot point is located at the center of the object.
  • x and y: These are simple utility properties to describe the final location of the View in its container, as a sum of the left and top values and translationX and translationY values.
  • alpha: Represents the alpha transparency on the View. This value is 1 (opaque) by default, with a value of 0 representing full transparency (not visible).

To animate a property of a View object, such as its color or rotation value, all you need to do is create a property animator and specify the View property that you want to animate. For example:
要为View对象的属性(例如其颜色或旋转值)设置动画效果,你只需创建属性动画器并指定要设置动画效果的View属性即可。举个栗子:

ObjectAnimator.ofFloat(myView, "rotation", 0f, 360f);

For more information on creating animators, see the sections on animating with ValueAnimator and ObjectAnimator.
有关创建动画的更多信息,请参阅关于使用ValueAnimatorObjectAnimator进行动画制作的章节。

Animating with ViewPropertyAnimator 通过ViewPropertyAnimator创建动画

The ViewPropertyAnimator provides a simple way to animate several properties of a View in parallel, using a single underlying Animator object.
ViewPropertyAnimator提供了一种简单的方法,可以使用单个基础Animator对象并行地对View的几个属性进行动画处理。
It behaves much like an ObjectAnimator, because it modifies the actual values of the view's properties, but is more efficient when animating many properties at once.
它的行为与ObjectAnimator非常相似,因为它修改了视图属性的实际值,但在同时动画多个属性时效率更高。
In addition, the code for using the ViewPropertyAnimator is much more concise and easier to read.
此外,使用ViewPropertyAnimator的代码更加简洁易读。
The following code snippets show the differences in using multiple ObjectAnimator objects, a single ObjectAnimator, and the ViewPropertyAnimator when simultaneously animating the x and y property of a view.
以下代码片段显示了在同时为视图的x和y属性设置动画时,使用多个ObjectAnimator对象,单个ObjectAnimator和ViewPropertyAnimator的差异。
Multiple ObjectAnimator objects

ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();

One ObjectAnimator

PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();

ViewPropertyAnimator

myView.animate().x(50f).y(100f);

For more detailed information about ViewPropertyAnimator, see the corresponding Android Developers blog post.

Declaring Animations in XML 在XML中声明动画

The property animation system lets you declare property animations with XML instead of doing it programmatically.
属性动画系统允许你用XML声明属性动画,而不是以编程方式进行。
By defining your animations in XML, you can easily reuse your animations in multiple activities and more easily edit the animation sequence.
通过用XML定义动画,你可以轻松地在多个活动中重复使用动画,并更轻松地编辑动画序列。
To distinguish animation files that use the new property animation APIs from those that use the legacy view animation framework, starting with Android 3.1, you should save the XML files for property animations in the res/animator/ directory.
为了将使用新属性动画API的动画文件与使用旧视图动画框架的动画文件区分开来,从Android 3.1开始,应该将属性动画的XML文件保存在res/animator/目录中。
The following property animation classes have XML declaration support with the following XML tags:
以下属性动画类使用以下XML标记支持XML声明:

  • ValueAnimator - <animator>
  • ObjectAnimator - <objectAnimator>
  • AnimatorSet - <set>
    To find the attributes that you can use in your XML declaration, see Animation Resources.
    为了找到你可以在XML中使用的属性,请查看Animation Resources
    The following example plays the two sets of object animations sequentially, with the first nested set playing two object animations together:
    接下来的例子顺序的播放了两个对象动画组合,第一个嵌套集合一起播放两个对象动画:
<set android:ordering="sequentially">
    <set>
        <objectAnimator
            android:propertyName="x"
            android:duration="500"
            android:valueTo="400"
            android:valueType="intType"/>
        <objectAnimator
            android:propertyName="y"
            android:duration="500"
            android:valueTo="300"
            android:valueType="intType"/>
    </set>
    <objectAnimator
        android:propertyName="alpha"
        android:duration="500"
        android:valueTo="1f"/>
</set>

In order to run this animation, you must inflate the XML resources in your code to an AnimatorSet object, and then set the target objects for all of the animations before starting the animation set.
为了运行此动画,你必须将代码中的XML资源充入AnimatorSet对象,然后在开始动画设置之前为所有动画设置目标对象。
Calling setTarget() sets a single target object for all children of the AnimatorSet as a convenience. The following code shows how to do this:
为方便起见,调用setTarget()为AnimatorSet的所有子节点设置一个目标对象。 以下代码显示了如何执行此操作:

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
    R.anim.property_animator);
set.setTarget(myObject);
set.start();

You can also declare a ValueAnimator in XML, as shown in the following example:
你也可以定ValueAnimator在XML中,如以下示例所示:

<animator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:valueType="floatType"
    android:valueFrom="0f"
    android:valueTo="-100f" />

To use the previous ValueAnimator in your code, you must inflate the object, add an AnimatorUpdateListener, get the updated animation value, and use it in a property of one of your views, as shown in the following code:
要在代码中使用以前的ValueAnimator,你必须膨胀对象,添加AnimatorUpdateListener,获取更新后的动画值,并将其用于你的某个视图的属性中,如以下代码所示:

ValueAnimator xmlAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this,
        R.animator.animator);
xmlAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator updatedAnimation) {
        float animatedValue = (float)updatedAnimation.getAnimatedValue();
        textView.setTranslationX(animatedValue);
    }
});

xmlAnimator.start();

For information about the XML syntax for defining property animations, see Animation Resources .
有关定义属性动画的XML语法的信息,请参阅动画资源。

Implications for UI performance 对UI性能的影响

Animators that update the UI cause extra rendering work for every frame in which the animation runs. For this reason, using resource intensive animations can negatively impact the performance of your app.
更新UI的动画器会为动画运行的每个帧导致额外的渲染工作。 因此,使用资源密集型动画可能会对应用程序的性能产生负面影响。

Work required to animate your UI is added to the animation stage of the rendering pipeline. You can find out if your animations impact the performance of your app by enabling Profile GPU Rendering and monitoring the animation stage. For more information, see Profile GPU Rendering Walkthrough.
将渲染UI的动画添加到渲染管道的动画阶段。 你可以通过启用Profile GPU渲染和监控动画阶段来了解你的动画是否会影响应用的性能。 有关更多信息,请参阅配置文件GPU渲染演练。

类说明android.animation

These classes provide functionality for the property animation system, which allows you to animate object properties of any type.
这些类为属性动画系统提供了功能,它允许你为任何类型的对象属性创建动画。
int, float, and hexadecimal color values are supported by default.
默认情况下支持整形、浮点和十六进制颜色值。
You can animate any other type by telling the system how to calculate the values for that given type with a custom TypeEvaluator.
你可以通过告诉系统如何使用自定义的TypeEvaluator计算该给定类型的值来动画化任何其他类型。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,482评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,377评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,762评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,273评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,289评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,046评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,351评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,988评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,476评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,948评论 2 324
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,064评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,712评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,261评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,264评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,486评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,511评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,802评论 2 345

推荐阅读更多精彩内容