`

【翻译】(75)属性动画

 
阅读更多

【翻译】(75)属性动画

 

see

http://developer.android.com/guide/topics/graphics/prop-animation.html

 

原文见

http://developer.android.com/guide/topics/graphics/prop-animation.html

 

-------------------------------

 

Property Animation

 

属性动画

 

-------------------------------

 

In this document

 

本文目录

 

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

* Animating with ValueAnimator 使用ValueAnimator的动画化

* Animating with ObjectAnimator 使用ObjectAnimator的动画化

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

* Animation Listeners 动画监听器

* Using a TypeEvaluator 使用一个TypeEvaluator

* Using Interpolators 使用插值器

* Specifying Keyframes 指定关键帧

* Animating Layout Changes to ViewGroups 动画化对视图组的布局改变

* Animating Views 动画化视图

* ViewPropertyAnimator

* Declaring Animations in XML 在XML中声明动画

 

Key classes 

 

关键类

 

ValueAnimator

ObjectAnimator

TypeEvaluator

 

Related samples

 

相关

 

API Demos API演示

 

-------------------------------

 

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.

 

* 持续时间:你可以指定一个动画的持续时间。默认长度为300毫秒。

 

* 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. Every 10 ms, which is the default frame refresh rate, the object moves horizontally by 10 pixels. At the end of 40ms, the animation stops, and the object ends at horizontal position 40. This is an example of an animation with linear interpolation, meaning the object moves at a constant speed.

 

首先,让我们用一个简单的示例来整体地浏览一个动画如何工作。图1描绘一个假想的对象,用它的x属性动画化它,而x属性代表它在屏幕上的水平位置。动画的持续时间被设置为40毫秒而途经的距离为40像素。每隔10毫秒,默认的帧刷新速率,对象水平地移动10像素。在40毫秒的结束时,动画停止,而对象在水平位置40处结束。这是一个带线性插值的动画示例,意思是对象以一个常量速度移动。

 

-------------------------------

 

(图1略:

 

上:

x = 0

t = 0ms

 

x = 10

t = 10ms

 

x = 20

t = 20ms

 

x = 30

t = 30ms

 

x = 40

t = 40ms

 

下:

duration = 40ms

 

Figure 1. Example of a linear animation

 

图1. 线性动画示例

 

-------------------------------

 

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. 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. As Figure 2 shows, the distance traveled at the beginning and end of the animation is less than in the middle.

 

你还可以指定动画以拥有一个非线性的插值。图2描绘一个假定的对象,它在动画开始时加速,而在动画结束时减速。该对象仍然每隔40毫秒移动40像素,但不是线性地。在开始的时候,这个动画加速到中途点,然后从中途点减速直至动画的结束。正如图2所示,在动画开始和结束所途径的距离小于中间。

 

-------------------------------

 

(图2略:

 

上:

x = 0

t = 0ms

 

x = 6

t = 10ms

 

x = 20

t = 20ms

 

x = 34

t = 30ms

 

x = 40

t = 40ms

 

下:

duration = 40ms

 

Figure 2. Example of a non-linear animation

 

图2. 一个非线性动画示例

 

-------------------------------

 

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描述主要的类如何相互地工作。

 

-------------------------------

 

(图3略:

 

1. ValueAnimatior

TimeInterpolator

TypeEvaluator

int duration

int startPropertyValue

int endPropertyValue

start()

 

2. ValueAnimator.Animator UpdateListener

onAnimationUpdate()

 

3. ValueAnimator.getAnimatedValue()

 

4. myAnimatedObject

property = getAnimatedValue()

 

1-2->3->4->2

 

Figure 3. How animations are calculated

 

图3. 动画如何被计算

 

-------------------------------

 

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. For example, in Figure 2, the TimeInterpolator used would be AccelerateDecelerateInterpolator and the TypeEvaluator would be IntEvaluator.

 

ValueAnimator封装了一个定义动画插值的TimeInterpolator,以及一个定义如何计算正在被动画化的属性的值的TypeEvaluator。例如,在图2中,被使用的TimeInterpolator将是AccelerateDecelerateInterpolator,而被使用的TypeEvaluator将是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. When you call start() the animation begins. 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. The elapsed fraction represents the percentage of time that the animation has completed, 0 meaning 0% and 1 meaning 100%. For example, in Figure 1, the elapsed fraction at t = 10 ms would be .25 because the total duration is t = 40 ms.

 

为了开始一个动画,创建一个ValueAnimator并给它你希望动画化的属性的开始和结束值,以及动画的持续时间。当你调用start()时动画开始。在整个动画期间,ValueAnimator计算一个逝去分数在0和1之间,基于动画的持续时间以及逝去多长时间。逝去分数代表动画已经完成的时间的百分比,0意味着0%而1意味着100%。例如,在图1中,在t=10毫秒时逝去分数将是0.25,因为总的持续时间为t=40毫秒。

 

When the ValueAnimator is done calculating an elapsed fraction, it calls the TimeInterpolator that is currently set, to calculate an interpolated fraction. 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. In Figure 1, the interpolated fraction is always the same as the elapsed fraction.

 

当ValueAnimator完成计算一个逝去分数时,它调用当前设置的TimeInterpolator,以计算一个被插值分数。一个被插值分数映射逝去分数到一个新的分数,它考虑了被设置的时间插值。例如,在图2中,因为动画缓慢地加速,插值分数,关于0.15,小于逝去分数0.25,在t=10毫秒时。在图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. 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.

 

当被插值分数被计算时,ValueAnimator调用合适的TypeEvaluator,来计算你正在动画化的属性的值,基于被插值分数,动画的开始值,以及动画的结束值。例如,在图2中,在t=10毫秒时被插值分数为.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演示示例工程中的com.example.android.apis.animation包提供关于如何使用属性动画系统的许多示例。

 

-------------------------------

 

API Overview

 

API概览

 

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.

 

你可以在android.animation中发现大多数属性动画系统的API。因为视图动画系统已经在android.view.animation中定义了许多插值器,你也可以在属性动画系统中使用那些插值器。以下表格描述属性动画系统的主要组件。

 

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:

 

Animator类为创建动画提供基本结构。你通常不直接使用这个类,因为它只提供最小功能,它必须被扩展以完全支持正在动画化的值。以下子类扩展了Animator:

 

Table 1. Animators

 

表1. 动画器

 

-------------------------------

 

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

 

* ValueAnimator 用于属性动画的主要计时引擎,它还为被动画化的属性计算值。它拥有计算动画值的所有核心功能并且包含每个动画的计时细节,关于一个动画是否重复的信息,接收更新事件的监听器,以及设置要求值的自定义类型的能力。对于正在动画的属性有两块问题:计算被动画的值,以及在正在被动画化的对象和属性上设置那些值。ValueAnimator不解决第二块问题,所以你必须监听被ValueAnimator计算的值的更新,并且使用你自己的逻辑来修改你希望动画化的对象。参见关于使用ValueAnimator来动画化的章节以获得更多信息。

 

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

 

* ObjectAnimator ValueAnimator的一个子类,它允许你设置一个目标对象和要动画化的对象属性。此类相应地更新属性,当它为动画计算一个新值时。大多数时间你希望使用ObjectAnimator,因为它使动画化目标对象上的值的过程变得更为简单。然而,你有时希望直接地使用ValueAnimator,因为ObjectAnimator有较多一点的限制,诸如需要特定的访问器方法(注:此处acessor应为accessor)在目标对象上是存在的。

 

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

 

* AnimatorSet 提供一个机制以分组多个动画在一起以使它们相互关联地运行。你可以设置动画一起、依次、或在一个特定延迟后播放。参见关于用动画器集合编排多个动画的章节以获得更多信息。

 

-------------------------------

 

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:

 

求值器告诉属性动画系统如何为一个给定的属性计算值。它们传入由一个Animator类提供的计时数据,动画的开始和结束值,并且基于这些数据计算属性的被动画化的值。属性动画系统提供以下求值器:

 

Table 2. Evaluators

 

表2. 求值器

 

-------------------------------

 

* Class/Interface Description

 

* 类/接口 描述

 

* IntEvaluator The default evaluator to calculate values for int properties.

 

* IntEvaluator 用于计算int型属性值的默认求值器。

 

* FloatEvaluator The default evaluator to calculate values for float properties.

 

* FloatEvaluator 用于计算float型属性值的默认求值器。

 

* ArgbEvaluator The default evaluator to calculate values for color properties that are represented as hexidecimal values.

 

* ArgbEvaluator 用于计算表现为十六进制值的颜色属性值的默认求值器。

 

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

 

* TypeEvaluator 一个接口,它允许你创建你自己的求值器。如果你正在动画化一个对象属性,它不是int型,float型,或颜色,你必须实现TypeEvaluator接口以指定如何计算对象属性的被动画化的值。你也可以为int型,float型,和颜色值指定一个自定义TypeEvaluator,如果你希望不同于默认行为地处理那些类型。参见关于使用一个TypeEvaluator的章节以获得关于如何编写一个自定义求值器的更多信息。

 

-------------------------------

 

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.

 

一个时间插值器定义如何把一个动画中的特定值计算为一个时间函数。例如,你可以指定动画在整个动画过程中线性地发生,意味着动画在整个时间内均匀地移动,或者你可以指定动画使用非线性时间,例如,在动画开始时加速和在动画结束时减速。表3描述在android.view.animation中包含的插值器。如果提供的插值器都不适合你的需要,请实现TimeInterpolator接口并创建你自己的插值器。参见使用插值器以获得关于如何编写一个自定义插值器的更多信息。

 

Table 3. Interpolators

 

表3. 插值器

 

-------------------------------

 

* Class/Interface Description

 

* 类/接口 描述

 

AccelerateDecelerateInterpolator An interpolator whose rate of change starts and ends slowly but accelerates through the middle.

 

AccelerateDecelerateInterpolator 一种插值器,它的改变速率缓慢地开始和结束但通过中间时加速。

 

AccelerateInterpolator An interpolator whose rate of change starts out slowly and then accelerates.

 

AccelerateInterpolator 一种插值器,它的改变速率缓慢地出发然后加速。

 

AnticipateInterpolator An interpolator whose change starts backward then flings forward.

 

AnticipateInterpolator 一种插值器,它的改变向后开始然后抛向前。(注:Anticipate是预期、提前的意思)

 

AnticipateOvershootInterpolator An interpolator whose change starts backward, flings forward and overshoots the target value, then finally goes back to the final value.

 

AnticipateOvershootInterpolator 一种插值器,它的改变向后开始,抛向前,并且超过目标值,然后最终转回到最终值。

 

BounceInterpolator An interpolator whose change bounces at the end.

 

BounceInterpolator 一种插值器,它的改变在结束时反弹。

 

CycleInterpolator An interpolator whose animation repeats for a specified number of cycles.

 

CycleInterpolator 一种插值器,它的动画重复指定数量的循环。

 

DecelerateInterpolator An interpolator whose rate of change starts out quickly and and then decelerates.

 

DecelerateInterpolator 一种插值器,它的改变速率快速地出发然后减速。

 

LinearInterpolator An interpolator whose rate of change is constant.

 

LinearInterpolator 一种插值器,它的改变速率是不变的。

 

OvershootInterpolator An interpolator whose change flings forward and overshoots the last value then comes back.

 

OvershootInterpolator 一种插值器,它的改变抛向前,冲过最后值,然后回来。

 

TimeInterpolator An interface that allows you to implement your own interpolator.

 

TimeInterpolator 一个接口,允许你实现你自己的插值器。

 

-------------------------------

 

-------------------------------

 

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. You obtain a ValueAnimator by calling one of its factory methods: ofInt(), ofFloat(), or ofObject(). For example:

 

ValueAnimator类让你在一个动画的持续时间内动画化一些类型的值,通过指定一个int型,float型,或颜色值集合,贯穿它来动画化。你通过调用它的其中一个工厂方法来取出一个ValueAnimator。

 

-------------------------------

 

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.

 

在这段代码中,当start()方法运行时,ValueAnimator开始计算动画的值,在0和1之间,持续时间为1000毫秒。

 

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.

 

在这段代码中,当start()方法运行时,ValueAnimator开始计算动画的值,在startPropertyValue和endPropertyValue之间,使用由MyTypeEvaluator提供的逻辑,持续时间为1000毫秒。

 

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.

 

然而,前面的代码片段并没有实际影响一个对象,因为ValueAnimator并不直接地操作在对象或属性上。你希望做的最可能的事情是用这些被计算的值来修改你希望动画化的对象。你通过在ValueAnimator中定义监听器来做这件事,以正确地处理动画寿命期间的重要事件,诸如帧更新。当实现监听器时,你可以通过调用getAnimatedValue(),为那个特定帧的刷新取出被计算的值。想获得关于监听器的更多信息,参见关于动画监听器的章节。

 

-------------------------------

 

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. This makes animating any object much easier, as you no longer need to implement the ValueAnimator.AnimatorUpdateListener, because the animated property updates automatically.

 

ObjectAnimator是ValueAnimator(在前面章节中讨论过)的一个子类并且组合计时引擎和ValueAnimator的值计算,它(注:应该指ValueAnimator)有动画化一个目标对象的一个具名属性的能力。这致使动画化任何对象变得更加容易,因为由于被动画化属性是自动更新的,你不再需要实现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,但你还指定对象以及那个对象的属性的名称(作为一个String)以及要动画化范围的值。

 

-------------------------------

 

ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);

anim.setDuration(1000);

anim.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. If this setter method does not exist, you have three options:

 

* 你正在动画化的对象属性必须拥有一个set函数(驼峰大小写),形式为set<属性值>()。因为ObjectAnimator在动画期间自动地更新属性,所以它必须能够用这个set方法访问属性。例如,如果属性值为foo,你需要拥有一个setFoo()方法。如果这个set方法不存在,你有三个选择:

 

* Add the setter method to the class if you have the rights to do so.

 

* 添加set方法到类,如果你有权做这件事。

 

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

 

* 使用一个你有权改变的封装器类,并且让那个封装器用一个可用的set方法接收值并且定向它至原来的对象。

 

* 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...参数只指定一个值,那么它被假定为动画的结束值。因此你正在动画化的对象属性必须拥有一个get函数,它被用于取出动画的开始值。get函数必须是get<属性名>()形式的。例如,如果属性名为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:

 

* 你正在动画化的属性的get方法(如果需要的话)和set方法必须操作在与你对ObjectAnimator指定的开始和结束值的相同类型上。例如,你必须拥有一个targetObject.setPropName(float)和targetObject.getPropName(float)如果你构造以下ObjectAnimator:

 

-------------------------------

 

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 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 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. For more information on listeners, see the section about Animation Listeners.

 

* 依赖于你正在动画化什么属性,你可能需要在一个View上调用invalidate()方法以使用已更新的被动画化的值来强制屏幕重绘它自身。你在onAnimationUpdate()回调中做这件事。例如,动画化一个Drawable对象的color属性仅当对象重绘它自身时才导致对屏幕的更新。在View上所有属性set方法,诸如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:

 

以下示例代码取自跳跃球示例(出于简化而被修改)用以下方式播放以下Animator对象:

 

1. Plays bounceAnim.

 

1. 播放bounceAnim。

 

2. Plays squashAnim1, squashAnim2, stretchAnim1, and stretchAnim2 at the same time.

 

2. 同时播放squashAnim1,squashAnim2,stretchAnim1,和stretchAnim2。(注:squash意思是挤压,stretch意思是拉伸)

 

3. Plays bounceBackAnim.

 

3. 播放bounceBackAnim。

 

4. Plays fadeAnim.

 

4. 播放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.

 

想获得关于如何使用动画器集合的一个更复杂示例,请参见API演示中的跳跃球示例。

 

-------------------------------

 

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.

 

* onAnimationStart()——当动画开始时被调用

 

* onAnimationEnd() - Called when the animation ends.

 

* onAnimationEnd()——当动画结束时被调用。

 

* onAnimationRepeat() - Called when the animation repeats itself.

 

* onAnimationRepeat()——当动画重复它自身时被调用

 

* onAnimationCancel() - Called when the animation is canceled. A cancelled animation also calls onAnimationEnd(), regardless of how they were ended.

 

* onAnimationCancel()——当动画被取消时被调用。一个被取消动画还调用onAnimationEnd(),不管它们如何被结束。

 

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

 

* onAnimationUpdate()——在动画的每一帧上被调用。监听这个事件以使用一个动画期间由ValueAnimator计算的值。为了使用该值,使用getAnimatedValue()方法查询被传递进事件的ValueAnimator以获得当前被动画化的值。实现此监听器是必需的,如果你使用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.

 

依赖于你正在动画化是什么属性或对象,你可能需要在一个View上调用invalidate()以强制屏幕区域使用新的被动画化的值来重绘它自身。在View上的所有属性的set方法,诸如setAlpha()和setTranslationX()正确地无效化View,所以你在用新值调用这些方法时不需要无效化View。

 

You can extend the AnimatorListenerAdapter class instead of implementing the Animator.AnimatorListener interface, if you do not want to implement all of the methods of the Animator.AnimatorListener interface. The AnimatorListenerAdapter class provides empty implementations of the methods that you can choose to override.

 

你可以扩展AnimatorListenerAdapter类而非实现Animator.AnimatorListener接口,如果你不希望实现Animator.AnimatorListener接口的所有方法。AnimatorListenerAdapter类提供方法的空实现,你可以选择它们来覆盖。

 

For example, the Bouncing Balls sample in the API demos creates an AnimatorListenerAdapter for just the onAnimationEnd() callback:

 

例如,API演示中的跳跃球示例创建一个只用于onAnimationEnd()回调的AnimatorListenerAdapter。

 

-------------------------------

 

ValueAnimatorAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);

fadeAnim.setDuration(250);

fadeAnim.addListener(new AnimatorListenerAdapter() {

public void onAnimationEnd(Animator animation) {

    balls.remove(((ObjectAnimator)animation).getTarget());

}

 

-------------------------------

 

-------------------------------

 

Animating Layout Changes to 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. 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, android.view.View#INVISIBLE}, or GONE. The remaining Views in the ViewGroup can also animate into their new positions when you add or remove Views. 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:

 

你可以使用LayoutTransition类动画化一个ViewGroup中的布局改变。在一个ViewGroup内的View可以通过一个显示和消失动画,当你添加它们到一个ViewGroup或从一个ViewGroup中移除它们时,或者当你用VISIBLE,INVISIBLE, 或GONE(注:此处乱码了,都是View类的静态常量,分别代表:可见,不可见但占空间,不可见且不占空间)来调用setVisibility()。在ViewGroup中其余的View还动画化进新的位置,当你添加或移除View。你可以通过调用setAnimator()在一个LayoutTransition对象中定义以下动画并且把以下LayoutTransition的其中一个常量传递进一个Animator对象:

 

* APPEARING - A flag indicating the animation that runs on items that are appearing in the container.

 

* APPEARING——一个标志,指示运行在正在出现在容器中的条目上的动画。

 

* CHANGE_APPEARING - A flag indicating the animation that runs on items that are changing due to a new item appearing in the container.

 

* CHANGE_APPEARING——一个标志,指示运行在因为一个新条目出现在容器中而正在改变的条目上的动画。

 

* DISAPPEARING - A flag indicating the animation that runs on items that are disappearing from the container.

 

* DISAPPEARING——一个标志,指示运行在正在从容器中消失的条目上的动画。

 

* CHANGE_DISAPPEARING - A flag indicating the animation that runs on items that are changing due to an item disappearing from the container.

 

* CHANGE_DISAPPEARING——一个标志,指示运行在因为一个从容器中消失的条目而正在改变的条目上的动画。

 

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示例向你展示如何为布局过渡定义动画,然后在你希望动画化的View对象上设置该动画。

 

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中移除的View,以及在ViewGroup中其余的View。

 

-------------------------------

 

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. 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系统来说未知的类型,你可以通过实现TypeEvaluator接口来创建你自己的求值器。被Android系统认知的类型有int,float,或一个颜色,它们分别被IntEvaluator、FloatEvaluator和ArgbEvaluator类型求值器支持。

 

There is only one method to implement in the TypeEvaluator interface, the evaluate() method. 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:

 

在TypeEvaluator接口中只有一个要实现的方法,evaluate()方法。这允许你正在使用的动画器返回一个合适的值用于在动画当前时间点上你的被动画化属性。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之间的值)然后计算一个被插值版本(注:应该是fraction的笔误),它依赖于你正在使用什么插值器。被插值分数是你的TypeEvaluator通过fraction参数接收的东西,所以你不必考虑插值器什么时候计算被动画化的值。

 

-------------------------------

 

-------------------------------

 

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. If none of these suit your needs, you can implement the TimeInterpolator interface and create your own.

 

在动画系统中插值器从代表动画的逝去时间的Animator中接收一个分数。插值器修改这个分数以符合它打算提供的动画类型。Android系统在android.view.animation包中提供一组通用插值器。如果这些都不符合你的需要,你可以实现TimeInterpolator接口并创建你自己的插值器。

 

As an example, how the default interpolator AccelerateDecelerateInterpolator and the LinearInterpolator calculate interpolated fractions are compared below. 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:

 

作为一个示例,默认插值器AccelerateDecelerateInterpolator和LinearInterpolator如何计算被插值分数在下文中比较。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;

}

 

-------------------------------

 

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. The AccelerateDecelerateInterpolator changes the values faster than LinearInterpolator between 200ms and 600ms and slower between 600ms and 1000ms.

 

正如该表所示,LinearInterpolator以相同的速度改变值,每过去200毫秒为.2。AccelerateDecelerateInterpolator改变值在200毫秒和600毫秒之间比LinearInterpolator快,而在600毫秒和1000毫秒之间比LinearInterpolator慢。

 

-------------------------------

 

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

 

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. You then call the ofKeyframe() factory method to obtain a PropertyValuesHolder object. Once you have the object, you can obtain an animator by passing in the PropertyValuesHolder object and the object to animate. The following code snippet demonstrates how to do this:

 

为了实例化一个Keyframe对象,你必须使用其中一个工厂方法,ofInt(),ofFloat(),或ofObject()以取出合适类型的Keyframe。然后你调用ofKeyframe()工厂方法以取出一个PropertyValuesHolder对象。一旦你拥有该对象,你可以通过传递进PropertyValuesHolder对象和要动画化的对象来取出一个动画器。以下代码片段演示如何做这件事。

 

-------------------------------

 

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

 

(注:上面有点笔误,应该是:

rotationAnim.setDuration(5000);

 

-------------------------------

 

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

 

想获得关于如何使用关键帧的更完整示例,参见API演示中的MultiPropertyAnimation示例。

 

-------------------------------

 

Animating Views

 

动画化视图

 

The property animation system allow streamlined animation of View objects and offerse a few advantages over the view animation system. 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.

 

属性动画系统允许View对象的被精简动画以及提供超越视图动画系统之上的少量优点。视图动画系统通过改变View对象被绘画的方式来变换它们。它在每个View的容器中被处理,因为View自身不操纵任何属性。这导致View被动画化,但在View对象自身内并没有导致改变。这导致行为诸如一个对象仍然存在它的原始位置,即便它被绘画在屏幕上的一个不同的位置上。在Android 3.0中,新的属性和相应的get和set方法被添加以消除这个缺陷。

 

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. The new properties in the View class that facilitate property animations are:

 

属性动画系统可以通过改变View对象的实际属性来动画化屏幕上的View。此外,View还自动地调用invalidate()方法以刷新屏幕,不管它的属性何时被改变。在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.

 

* translationX和translationY:这些属性控制该View定位在它的布局容器所设置的left和top坐标的偏移(注:相对位置)的地方。

 

* rotation, rotationX, and rotationY: These properties control the rotation in 2D (rotation property) and 3D around the pivot point.

 

* rotation,rotationX,和rotationY:这些属性控制在围绕枢纽点的二维(rotation属性)和三维中的旋转。

 

* scaleX and scaleY: These properties control the 2D scaling of a View around its pivot point.

 

* scaleX和scaleY:这些属性控制一个View围绕它的枢纽点的二维缩放。

 

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

 

* pivotX和pivotY:这些属性控制枢纽点的位置,旋转和缩放变换围绕它而发生。默认,枢纽点位于对象的中心。

 

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

 

* x和y:这些是是简单的工具属性,以描述View在它的容器中的最终位置,作为一个left、top值和translationX、translationY值的和。

 

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

 

* alpha:代表该View上的alpha透明度。此值默认是1(不透明),而0值代表全透明(不可见)。

 

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.

 

关于创建动画器的更多信息,请参见关于使用ValueAnimator和ObjectAnimator的动画化的章节。

 

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. 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. In addition, the code for using the ViewPropertyAnimator is much more concise and easier to read. 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.

 

ViewPropertyAnimator提供一种简单的方式来并行地动画化一个View的几个属性,使用一个单一底层的Animator对象。它的行为非常像一个ObjectAnimator,因为它修改该视图的属性的实际值,但当马上动画化许多属性时会更高效。此外,用于使用ViewPropertyAnimator的代码更加简练而且更易于阅读。以下代码片段展示当同时动画化一个视图的x和y属性时使用多个ObjectAnimator对象,一个单一ObjectAnimator,以及ViewPropertyAnimator的区别。

 

Multiple ObjectAnimator objects

 

多个ObjectAnimator对象

 

-------------------------------

 

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

 

一个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.

 

想获得关于ViewPropertyAnimator的更详细信息,请参见相应的Android开发者博客文章。

 

-------------------------------

 

Declaring Animations in XML

 

在XML中声明动画

 

The property animation system lets you declare property animations with XML instead of doing it programmatically. By defining your animations in XML, you can easily reuse your animations in multiple activities and more easily edit the animation sequence.

 

属性动画系统让你使用XML声明属性动画而非用编程的方式来做到。通过在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 (instead of res/anim/). Using the animator directory name is optional, but necessary if you want to use the layout editor tools in the Eclipse ADT plugin (ADT 11.0.0+), because ADT only searches the res/animator/ directory for property animation resources.

 

为了区分使用新属性动画API和使用遗留的视图动画框架的动画文件,从Android 3.1开始,你应该在res/animator目录中保存用于属性动画的XML文件(而非res/anim/)。使用animator目录名是可选的,但这是必要的如果你希望使用Eclipse ADT插件(ADT 11.0.0以上)中的布局编辑工具,因为ADT只搜索res/animator/中的属性动画资源。

 

The following property animation classes have XML declaration support with the following XML tags:

 

以下属性动画类拥有使用以下XML标签的XML声明支持。

 

* ValueAnimator - <animator>

* ObjectAnimator - <objectAnimator>

* AnimatorSet - <set>

 

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. Calling setTarget() sets a single target object for all children of the AnimatorSet as a convenience. The following code shows how to do this:

 

为了运行此动画,你必须在你的代码中解压XML资源到一个AnimatorSet对象,然后在开始动画集合之前为所有动画设置目标对象。作为一种便利,调用setTarget()为AnimatorSet的所有子对象设置一个单一目标对象。以下代码展示如何做这件事:

 

-------------------------------

 

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,

    R.anim.property_animator);

set.setTarget(myObject);

set.start();

 

-------------------------------

 

For information about the XML syntax for defining property animations, see Animation Resources.

 

想获得关于定义属性动画的XML语法的信息,请参见动画资源。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 08 Mar 2012 0:34

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)

 

(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:

* ソフトウェア技術ドキュメントを勝手に翻訳

http://www.techdoctranslator.com/android

* Ley's Blog

http://leybreeze.com/blog/

* 农民伯伯

http://www.cnblogs.com/over140/

* Android中文翻译组

http://androidbox.sinaapp.com/


分享到:
评论

相关推荐

    一款非常好的WPF编程宝典2010 源代码

    4.3.2 DockPanel面板 75 4.3.3 嵌套布局包容器 77 4.4 Grid面板 78 4.4.1 调整行和列 80 4.4.2 跨越行和列 82 4.4.3 分割窗口 83 4.4.4 共享尺寸组 86 4.4.5 UniformGrid面板 88 4.5 使用Canvas面板进行 ...

    Apress.Pro.WPF.in.C.Sharp.2008.2nd.Edition.Feb.2008

    4.3.2 DockPanel面板 75 4.3.3 嵌套布局包容器 77 4.4 Grid面板 78 4.4.1 调整行和列 80 4.4.2 跨越行和列 82 4.4.3 分割窗口 83 4.4.4 共享尺寸组 86 4.4.5 UniformGrid面板 88 4.5 使用Canvas面板进行 基于坐标的...

    计步器的实现

    * 动画时长 */ private int animationLength = 3000; /** * 当前运动类型 */ private String type = "Riding"; /** * 当前活跃等级 */ private String level = "等级:轻度活跃"; /** * 步数上方...

    PowerPoint.2007宝典 3/10

    4.2.3 将Word文档作为新演示文稿打开 75 4.2.4 从网页导入文本 76 4.3 管理幻灯片 76 4.3.1 选择幻灯片 77 4.3.2 删除幻灯片 77 4.3.3 撤销误操作 78 4.3.4 重排幻灯片 78 4.4 使用内容占位符 80 ...

    PowerPoint.2007宝典 10/10

    4.2.3 将Word文档作为新演示文稿打开 75 4.2.4 从网页导入文本 76 4.3 管理幻灯片 76 4.3.1 选择幻灯片 77 4.3.2 删除幻灯片 77 4.3.3 撤销误操作 78 4.3.4 重排幻灯片 78 4.4 使用内容占位符 80 ...

Global site tag (gtag.js) - Google Analytics