Property Animation

屬性動畫

屬性動畫系統是一個強大的框架,它允許你動畫幾乎所有的東西。無論是否它繪製到屏幕上或沒有,你可以定義一個動畫改變任何對象的屬性伴隨時間的推移。屬性動畫改變了屬性(對象的一個域)的值超過指定的時間長度。對應動畫的東西,如指定你想要動畫的對象屬性,例如一個對象在屏幕中的位置,要動畫多久,和動畫之間的距值。

屬性動畫系統可讓您定義動畫以下特點:

  • Duration(時間):您可以指定動畫的持續時間。默認長度是300毫秒。
  • Time interpolation(時間插值):定義了動畫變化的頻率。
  • Repeat count and behavior(重複計數和行爲):您可以指定是否有一個動畫的重複,當它到達時間結束,如何多次重複的動畫。您還可以指定是否要反向播放動畫。把它設置爲扭轉起着動畫向前然後向後反覆,直到重複次數達到。
  • Animator sets(動畫設置):你能按照一定的邏輯設置來組織動畫,一起播放或順序或指定延遲。
  • Frame refresh delay(幀刷新延遲):您可以指定如何經常刷新你的動畫幀。默認設置每10毫秒刷新,但在您的應用程序可以指定刷新幀的速度,最終取決於系統整體的狀態和提供多快服務的速度依據底層的定時器。


屬性動畫工作機制

首先,讓我們去如何動畫一個簡單的例子。圖1描繪了一個假想的動畫對象的x屬性,代表其在屏幕上的水平位置。動畫的持續時間設置爲40毫秒和旅行的距離是40像素。每隔10毫秒,這是默認的幀刷新速率,物體水平移動10個像素。在40ms的結束,動畫停止,對象在水平位置40結束。這是一個線性插值動畫的例子,這意味着對象在一個恆定的速度移動。

animation-linear.png
圖1線性動畫的例子。

您還可以指定動畫有一個非直線插補。圖2說明了一個假想的對象,加速在開頭動畫,在動畫結束時減速。對象仍然在40毫秒移動40個像素,但非線性。在開始的時候,這個動畫加速的中間點,然後從中間點減速,直到動畫結束。如圖2所示,動畫的開始和結束移動距離小於中間。

animation-nonlinear.png
圖2非線性動畫的例子。

讓我們看在屬性動畫系統的重要組成部分,如何計算像上面顯示的動畫的詳細介紹。圖3描述了主要類是怎麼工作的。

valueanimator.png
圖3。動畫是如何計算的

ValueAnimator對象保持動畫的實時跟蹤,如動畫已經運行的時間,和當前動畫的屬性值。

在ValueAnimator封裝TimeInterpolator,它定義動畫插值,和TypeEvaluator,它定義瞭如何計算的動畫屬性的值。例如,如圖2,使用的TimeInterpolator將 ​​是 AccelerateDecelerateInterpolator和TypeEvaluator的將會是IntEvaluator的。

啓動動畫,創建一個ValueAnimator,給你想要的動畫開始和結束值,定義動畫的持續時間。當你調用的start()動畫開始。在整個動畫,ValueAnimator計算經過部分的分數 介於0和1,基於動畫的持續時間和多少時間已過。經過的部分代表,動畫已完成的時間百分比,0表示0%和100%1的含義。例如,在圖1中,在t = 10毫秒時間的比例是0.25,因爲總工期爲T = 40毫秒。 計算經過部分ValueAnimator時,它調用TimeInterpolator當前設置,計算插值分數。一個插值分數經過部分映射到一個新的,考慮到設置的時間內插的分數。例如,在圖2中,因爲動畫慢慢加速,插約0.15分數,是比過去0.25部分少,在t = 10毫秒內。在圖1中,插值分數始終是經過分數相同。

當插值分數計算,ValueAnimator的的調用適當的TypeEvaluator,計算你的動畫屬性值,基於內插的分數,起始值,結束值和動畫。例如,在圖2中,插值部分是在t=0.15 在10毫秒內,所以當時時間屬性值爲0.15 x(40 - 0),或6。

關於如何使用屬性動畫,系統的com.example.android.apis.animation包中的API演示示例項目提供了很多例子。

How Property Animation Differs from View Animation

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

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

您可以在android.animation裏面找到屬性動畫系統大部分API。因爲視圖的動畫系統已經定義了許多插值在android.view.animation,你可以使用屬性動畫系統的插值。下表描述的屬性動畫系統的主要組成部分。

Animator 類提供了用於創建動畫的基本結構。你通常不使用這個類,因爲它直接提供最基本的功能,必須擴展到完全支持動畫值。以下子類擴展Animator:

  表1。 動畫家(Animators)

描述

ValueAnimator

屬性動畫時序引擎也計算屬性動畫的值。它擁有所有的核心功能,計算動畫值,幷包含每個動畫,有關時序的詳細信息是否動畫重複,聽衆接收更新事件,並設置自定義類型的能力評估。有兩件,以生動活潑的屬性:動畫值計算和設置這些對象的屬性動畫值。ValueAnimator不進行第二件,所以你一定要更新計算值ValueAnimator和修改你想用自己的邏輯動畫的對象。請參閱有關更多信息Animating with ValueAnimator部分 。

ObjectAnimator

ValueAnimator的子類,允許你設置一個目標對象和對象屬性的動畫。當計算出一個新的動畫值,本類更新相應的屬性。你大部分情況使用ObjectAnimator,因爲它使得動畫的目標對象的值更簡單。然而,有時你直接使用ValueAnimator,因爲ObjectAnimator有一些限制,如對目標對象目前要求的具體acessor方法。

AnimatorSet

提供機制,以組合動畫一起,讓他們關聯性運行。你可以設置動畫一起播放,順序,或在指定的延遲之後。請參閱有關部分Choreographing multiple animations with Animator Sets更多信息。


評估人員告訴屬性動畫系統如何計算一個給定的屬性值。他們採取的時機,是由一個數據Animator 類,動畫的開始和結束值,並計算基於此數據屬性的動畫值。屬性動畫系統提供了以下評價:

  表2。 評價者(Evaluators)

類/接口 描述

IntEvaluator

默認的計算器來計算int屬性值

FloatEvaluator

默認評估值來計算浮動屬性。

ArgbEvaluator

默認的計算器計算值表示爲十六進制值的色彩屬性。

TypeEvaluator

一個接口,允許你創建自己的評估。如果你是動畫對象的屬性,不是一個整數,浮點數,或顏色,你必須實現的TypeEvaluator接口指定如何計算對象屬性的動畫值。您也可以指定自定義TypeEvaluator整數,浮點數,顏色值以及,如果你想對比默認行爲來處理這些類型的不同。請參閱有關部分Using a TypeEvaluator更多關於如何編寫自定義評估信息。

一個時間插補定義如何在一個動畫的特定值作爲時間函數的計算。例如,你可以指定動畫發生線性在整個動畫,這意味着動畫均勻地移動整個時間,或者可以指定使用非線性時間的動畫,例如,在開始加速,並在最後減速動畫。表3說明中所含的插值android.view.animation。如果沒有提供插值適合您的需要,實施TimeInterpolator接口,並創建自己的。請參閱Using interpolators如何編寫一個定製的插補的更多信息。


  表3。 插值(Interpolators)

類/接口 描述

AccelerateDecelerateInterpolator

插補,其變化率慢慢開始和結束,但通過中間加速。

AccelerateInterpolator

插補,其變化率開始緩慢,然後加快。

AnticipateInterpolator

內插的變化開始落後,然後向前甩。

AnticipateOvershootInterpolator

內插的變化,開始落後,甩向前過沖目標值,然後終於可以追溯到最終值。

BounceInterpolator

插補,其變化在最後反彈。

CycleInterpolator

內插動畫重複指定的週期數。

DecelerateInterpolator

插補,其變化的速度開始很快,然後減速。

LinearInterpolator

插補,其變化率是恆定的

OvershootInterpolator

內插的變化甩向前和過沖的最後一個值,然後回來。

TimeInterpolator

一個接口,使您實現自己的插補。

ValueAnimator動畫

ValueAnimator類讓你動畫動畫的持續時間由某種類型的值指定了一套整,浮,或顏色值動畫。您獲得通過ValueAnimator調用工廠方法之一:ofInt() , ofFloat() , or ofObject()。例如:

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


在這段代碼中,ValueAnimator開始動畫的計算值,1000毫秒,當時 ​​間爲0和1之間,運行的start()方法。 你也可以指定一個自定義類型的動畫通過執行下列操作:

ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);
  animation.setDuration(1000);
  animation.start();


在這段代碼中,ValueAnimator開始計算之間的動畫值,使用所提供的邏輯MyTypeEvaluator 的start()方法運行時間爲1000毫秒,當startPropertyValue和endPropertyValue。


然而,前面的代碼片段,有沒有對象的實際效果,因爲在ValueAnimator不直接操作對象或屬性。最有可能的事情,你想要做的是修改這些計算值要進行動畫的對象。你定義在聽衆ValueAnimator妥善處理動畫的壽命期間的重要事件,如幀更新。實施的聽衆時,你可以通過調用特定的幀刷新計算值getAnimatedValue()。對聽衆的更多信息,請參閱有關部分Animation Listeners

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

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 anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);
anim.setDuration(1000);
anim.start();


To have the ObjectAnimator update properties correctly, you must do the following:

  • 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: ◦Add the setter method to the class if you have the rights to do so.
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.
Use ValueAnimator instead.
  • 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.
  • 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:
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.


AnimatorSet創編動畫

在許多情況下,你要播放的動畫,取決於另一個動畫開始或者結束時。Android系統,讓你捆綁動畫到AnimatorSet一起,使您可以指定是否要同時,按順序,或在指定的延遲後開始動畫。你可以在對方還AnimatorSet對象。


從下面的示例代碼彈彈球樣品(簡單修改)扮演下面的動畫 對象以下列方式:

  • Plays bounceAnim.
  • Plays squashAnim1, squashAnim2, stretchAnim1, and stretchAnim2 at the same time.
  • Plays bounceBackAnim.
  • 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();

對於如何使用動畫集更完整的例子,彈彈球在APIDemos樣本。

動畫聽衆

與下文所述的聽衆,你可以聽動畫的持續時間期間的重要事件。

這取決於你是動畫什麼屬性或對象,你可能需要瀏覽強制重繪新的動畫值,到屏幕面積上,調用invalidate()。例如,動畫的繪製對象的顏色屬性,僅造成更新到屏幕上時,該對象重繪本身。在視圖的全部屬性set方法,例如[http://developer.android.com/reference/android/view/View.html#setAlpha(float) setAlpha()]and setTranslationX()初始化視圖屬性,所以你不用使用新值調用這些方法初始化視圖。


可以延長AnimatorListenerAdapter類,而不是實施的Animator.AnimatorListener接口,如果你不想執行的所有方法Animator.AnimatorListener接口。AnimatorListenerAdapter類提供的方法,你可以選擇覆蓋的空實現。

例如,Bouncing Balls(彈彈球)例子在API演示創建一個AnimatorListenerAdapteronAnimationEnd() 回調:

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

動畫布局的變化,以ViewGroups

屬性動畫系統提供的能力,以動畫變化ViewGroup對象以及自己的動畫視圖對象提供了一個簡單的方法。

你可以在一個ViewGroup動畫與佈局的變化LayoutTransition類。一個ViewGroup內部的視圖,可以通過動畫出現和消失,當您添加或刪除它們從一個ViewGroup或當你調用一個視圖的setVisibility()的方法VISIBLE(可見),android.view.View#INVISIBLE(隱形),或去除。在ViewGroup中存在的視圖可以動畫到你增加或者刪除視圖的新位置上。你可以通過調用定義在下面的動畫LayoutTransition的對象android.animation.Animator) setAnimator()在通過動畫對象有下列的LayoutTransition常量:

  • 出現(APPEARING) -一個標誌,指示在容器中的項目上出現運行的動畫。
  • CHANGE_APPEARING -一個標誌,指示一個新的在容器中項目上出現運行的動畫。
  • 消失(DISAPPEARING) -一個標誌,指示動畫的運行項目,從容器中消失。
  • CHANGE_DISAPPEARING -一個標誌,指示一個從容器中的項目上運行的動畫消失。

你可以定義自己的自定義動畫這四種類型的事件定製的佈局過渡,或只是告訴動畫系統使用默認的動畫。

LayoutAnimations的API演示示例顯示您如何定義佈局過渡的動畫,然後設置要動畫視圖對象的動畫。

LayoutAnimationsByDefault其的相應layout_animations_by_default.xml佈局資源文件表明您如何啓用在XML ViewGroups的默認佈局轉換。你唯一需要做的是設置的 Android :animateLayoutchanges 屬性爲 true 的ViewGroup的。例如:

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

這個屬性設置爲true,自動動畫ViewGroup增加或移除存在於ViewGroup的視圖。

使用一個TypeEvaluator

如果你想動畫的類型是Android系統沒有的,可以通過實施創建自己定義TypeEvaluator接口。被稱爲Android系統的類型是整數,浮點數,或一種顏色,這是由支持的IntEvaluatorFloatEvaluator,和ArgbEvaluator的類型評估。

只有一個方法來實現在TypeEvaluator接口,T, T) 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);
     }
   }


注:當ValueAnimator(或ObjectAnimator)運行時,它計算消耗動畫(0和1之間的值)的一小部分,然後 計算出一個插值取決於上什麼插補您正在使用的版本。插值分數您的TypeEvaluator,通過接收部分參數,所以你沒有考慮到插補計算動畫值時。

使用插值

內插定義動畫中的具體值作爲時間函數的計算。例如,你可以指定動畫發生線性在整個動畫,這意味着動畫均勻地移動整個時間,或者可以指定使用非線性時間的動畫,例如,使用加速或減速的開始或結束動畫。


動畫集代表經過時間的動畫內插動畫系統收到的一小部分。插值修改這個分數與動畫的類型相吻合,它旨在提供。Android系統提供了一個共同插值設置的android.view.animation包。如果沒有這些適合您的需求,可以實現TimeInterpolator接口,並創建自己的。


作爲一個例子,如何默認的插補AccelerateDecelerateInterpolatorLinearInterpolator計算的插值分數比較如下。在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;
  }


下表表示持續1000毫秒的動畫,這些插值計算的近似值:

已過毫秒 經過分數/插值的一小部分(線性) 插值分數(加速/減速)

0

0

0

200

.2

.1

400

.4

.345

600

.6

.8

800

.8

.9

1000

1

1


作爲該表顯示,LinearInterpolator改變以同樣的速度值,爲每200ms傳遞.2。該AccelerateDecelerateInterpolator更改值比LinearInterpolator在200毫秒和600毫秒快和在600毫秒和1000毫秒之間慢。

指定關鍵幀

一個關鍵幀Keyframe的對象包括時間/值對,可以讓你定義一個特定的狀態,在特定時間的動畫。每個關鍵幀也可以有自己的插補控制動畫中的前一個關鍵幀之間的時間,這個關鍵幀的時間間隔的行爲。


實例化一個關鍵幀Keyframe的對象,你必須使用工廠方法之一的,ofInt() ofFloat() ,或ofObject()獲得適當類型的關鍵幀。然後,你調用android.animation.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);


對於如何使用關鍵幀的更完整的範例,看到MultiPropertyAnimation在APIDemos的例子。

動畫視圖

屬性動畫系統允許精簡視圖對象的動畫和offerse在視圖動畫系統有幾個優點。視圖動畫系統改造,通過改變,他們繪製的方式查看對象。這是在每個視圖的容器處理,因爲視圖本身沒有屬性來操作。這導致在動畫的查看,但沒有造成人員在視圖對象本身的變化。這導致的行爲,如在原來的位置上仍然存在的對象,即使是在屏幕上的不同位置上繪製。在Android 3.0中,增加了新的屬性和相應的getter和setter方法​​來消除這一缺點。


屬性動畫系統可以改變視圖對象的實際屬性,動畫在屏幕上的視圖。此外,視圖還自動調用invalidate()方法來刷新屏幕時改變其屬性。在新的屬性視圖類促進屬性動畫的是:


  • translationX和translationY:這些屬性控制的地方查看位於從它的左側和頂部設置其佈局容器的座標的增量。
  • rotation, rotationX, and rotationY: 這些屬性控制在2D旋轉(旋轉屬性)和3D的支點周圍。
  • scaleX 和 scaleY :這些屬性控制周圍的支點視角的2D縮放。
  • pivotX和pivotY:這些屬性控制支點的位置,圍繞着它旋轉和縮放變換髮生。默認情況下,支點位於中心的對象。
  • x 和 y :這些都是簡單實用的特性來形容查看其容器的最終位置,左側和頂部值和translationX和translationY的價值的總和。
  • alpha :代表alpha透明度。此值默認爲1(不透明),0代表完全透明(不可見)。


動畫視圖對象的屬性,如它的顏色或旋轉值,所有你需要做的是創建一個屬性的動畫,並指定要動畫視圖屬性。例如:

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

創建動畫的詳細信息,請參閱與動畫ValueAnimatorObjectAnimator


ViewPropertyAnimator的動畫

在該ViewPropertyAnimator並行動畫的幾個屬性查看,使用單一的基礎Animator對象提供了一個簡單的方法。它的行爲像一個ObjectAnimator,因爲它修改視圖屬性的實際值,但更有效的動畫時許多屬性一次。此外,使用的的代碼ViewPropertyAnimator是更簡潔,更易於閱讀。下面的代碼片段顯示在使用多個的差異ObjectAnimator對象,一個的單ObjectAnimator,以及時的ViewPropertyAnimator同時動畫的 x 和 y 屬性視圖。


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

如需更詳細的信息ViewPropertyAnimator,看到相應的Android開發者博客文章

在XML定義動畫

屬性動畫系統,可以讓你聲明與XML屬性動畫,而不是做編程。在XML中定義的動畫,你可以很容易地在多個活動中重用的動畫更容易編輯動畫序列。來區分使用那些使用傳統的新的屬性的動畫API 動畫框架與Android 3.1開始,動畫文件,你應該保存的XML文件中的屬性的動畫res/animator/目錄代替res/anim/。使用動畫的目錄名稱是可選的,但必要的,如果你想使用的佈局編輯工具,在Eclipse的ADT插件(ADT 11.0.0 +),因爲ADT只搜索res/animator/目錄屬性的動畫資源。


下列屬性動畫類有下列XML標記的XML聲明支持:

下面的示例播放兩套對象的動畫順序,第一個是嵌套播放兩個對象的動畫:

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

爲了運行這個動畫,你必須在你的代碼XML資源到AnimatorSet對象,然後設置目標對象爲所有的動畫開始前的動畫設置。調用setTarget()設置一個所有兒童的單目標對象AnimatorSet的作爲一種方便。下面的代碼演示如何做到這一點:

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

爲定義屬性動畫的XML語法的信息,請參閱動畫資源Animation Resources

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章