ObjectAnimation
屬性動(dòng)畫
屬性動(dòng)畫系統(tǒng)是一個(gè)強(qiáng)大的框架,它允許你動(dòng)畫幾乎所有的東西。無論是否它繪制到屏幕上或沒有,你可以定義一個(gè)動(dòng)畫改變?nèi)魏螌?duì)象的屬性伴隨時(shí)間的推移。屬性動(dòng)畫改變了屬性(對(duì)象的一個(gè)域)的值超過指定的時(shí)間長(zhǎng)度。對(duì)應(yīng)動(dòng)畫的東西,如指定你想要?jiǎng)赢嫷膶?duì)象屬性,例如一個(gè)對(duì)象在屏幕中的位置,要?jiǎng)赢嫸嗑?#xff0c;和動(dòng)畫之間的距值。
屬性動(dòng)畫系統(tǒng)可讓您定義動(dòng)畫以下特點(diǎn):
- Duration(時(shí)間):您可以指定動(dòng)畫的持續(xù)時(shí)間。默認(rèn)長(zhǎng)度是300毫秒。
- Time interpolation(時(shí)間插值):定義了動(dòng)畫變化的頻率。
- Repeat count and behavior(重復(fù)計(jì)數(shù)和行為):您可以指定是否有一個(gè)動(dòng)畫的重復(fù),當(dāng)它到達(dá)時(shí)間結(jié)束,如何多次重復(fù)的動(dòng)畫。您還可以指定是否要反向播放動(dòng)畫。把它設(shè)置為扭轉(zhuǎn)起著動(dòng)畫向前然后向后反復(fù),直到重復(fù)次數(shù)達(dá)到。
- Animator sets(動(dòng)畫設(shè)置):你能按照一定的邏輯設(shè)置來組織動(dòng)畫,一起播放或順序或指定延遲。
- Frame refresh delay(幀刷新延遲):您可以指定如何經(jīng)常刷新你的動(dòng)畫幀。默認(rèn)設(shè)置每10毫秒刷新,但在您的應(yīng)用程序可以指定刷新幀的速度,最終取決于系統(tǒng)整體的狀態(tài)和提供多快服務(wù)的速度依據(jù)底層的定時(shí)器。
屬性動(dòng)畫工作機(jī)制
首先,讓我們?nèi)ト绾蝿?dòng)畫一個(gè)簡(jiǎn)單的例子。圖1描繪了一個(gè)假想的動(dòng)畫對(duì)象的x屬性,代表其在屏幕上的水平位置。動(dòng)畫的持續(xù)時(shí)間設(shè)置為40毫秒和旅行的距離是40像素。每隔10毫秒,這是默認(rèn)的幀刷新速率,物體水平移動(dòng)10個(gè)像素。在40ms的結(jié)束,動(dòng)畫停止,對(duì)象在水平位置40結(jié)束。這是一個(gè)線性插值動(dòng)畫的例子,這意味著對(duì)象在一個(gè)恒定的速度移動(dòng)。
圖1線性動(dòng)畫的例子。您還可以指定動(dòng)畫有一個(gè)非直線插補(bǔ)。圖2說明了一個(gè)假想的對(duì)象,加速在開頭動(dòng)畫,在動(dòng)畫結(jié)束時(shí)減速。對(duì)象仍然在40毫秒移動(dòng)40個(gè)像素,但非線性。在開始的時(shí)候,這個(gè)動(dòng)畫加速的中間點(diǎn),然后從中間點(diǎn)減速,直到動(dòng)畫結(jié)束。如圖2所示,動(dòng)畫的開始和結(jié)束移動(dòng)距離小于中間。
圖2非線性動(dòng)畫的例子。讓我們看在屬性動(dòng)畫系統(tǒng)的重要組成部分,如何計(jì)算像上面顯示的動(dòng)畫的詳細(xì)介紹。圖3描述了主要類是怎么工作的。
圖3。動(dòng)畫是如何計(jì)算的ValueAnimator對(duì)象保持動(dòng)畫的實(shí)時(shí)跟蹤,如動(dòng)畫已經(jīng)運(yùn)行的時(shí)間,和當(dāng)前動(dòng)畫的屬性值。
在ValueAnimator封裝TimeInterpolator,它定義動(dòng)畫插值,和TypeEvaluator,它定義了如何計(jì)算的動(dòng)畫屬性的值。例如,如圖2,使用的TimeInterpolator將 ??是 AccelerateDecelerateInterpolator和TypeEvaluator的將會(huì)是IntEvaluator的。
啟動(dòng)動(dòng)畫,創(chuàng)建一個(gè)ValueAnimator,給你想要的動(dòng)畫開始和結(jié)束值,定義動(dòng)畫的持續(xù)時(shí)間。當(dāng)你調(diào)用的start()動(dòng)畫開始。在整個(gè)動(dòng)畫,ValueAnimator計(jì)算經(jīng)過部分的分?jǐn)?shù) 介于0和1,基于動(dòng)畫的持續(xù)時(shí)間和多少時(shí)間已過。經(jīng)過的部分代表,動(dòng)畫已完成的時(shí)間百分比,0表示0%和100%1的含義。例如,在圖1中,在t = 10毫秒時(shí)間的比例是0.25,因?yàn)榭偣て跒門 = 40毫秒。 計(jì)算經(jīng)過部分ValueAnimator時(shí),它調(diào)用TimeInterpolator當(dāng)前設(shè)置,計(jì)算插值分?jǐn)?shù)。一個(gè)插值分?jǐn)?shù)經(jīng)過部分映射到一個(gè)新的,考慮到設(shè)置的時(shí)間內(nèi)插的分?jǐn)?shù)。例如,在圖2中,因?yàn)閯?dòng)畫慢慢加速,插約0.15分?jǐn)?shù),是比過去0.25部分少,在t = 10毫秒內(nèi)。在圖1中,插值分?jǐn)?shù)始終是經(jīng)過分?jǐn)?shù)相同。
當(dāng)插值分?jǐn)?shù)計(jì)算,ValueAnimator的的調(diào)用適當(dāng)?shù)腡ypeEvaluator,計(jì)算你的動(dòng)畫屬性值,基于內(nèi)插的分?jǐn)?shù),起始值,結(jié)束值和動(dòng)畫。例如,在圖2中,插值部分是在t=0.15 在10毫秒內(nèi),所以當(dāng)時(shí)時(shí)間屬性值為0.15 x(40 - 0),或6。
關(guān)于如何使用屬性動(dòng)畫,系統(tǒng)的com.example.Android.apis.animation包中的API演示示例項(xiàng)目提供了很多例子。
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里面找到屬性動(dòng)畫系統(tǒng)大部分API。因?yàn)橐晥D的動(dòng)畫系統(tǒng)已經(jīng)定義了許多插值在android.view.animation,你可以使用屬性動(dòng)畫系統(tǒng)的插值。下表描述的屬性動(dòng)畫系統(tǒng)的主要組成部分。
Animator 類提供了用于創(chuàng)建動(dòng)畫的基本結(jié)構(gòu)。你通常不使用這個(gè)類,因?yàn)樗苯犹峁┳罨镜墓δ?#xff0c;必須擴(kuò)展到完全支持動(dòng)畫值。以下子類擴(kuò)展Animator:
??表1。?動(dòng)畫家(Animators)
| ValueAnimator | 屬性動(dòng)畫時(shí)序引擎也計(jì)算屬性動(dòng)畫的值。它擁有所有的核心功能,計(jì)算動(dòng)畫值,并包含每個(gè)動(dòng)畫,有關(guān)時(shí)序的詳細(xì)信息是否動(dòng)畫重復(fù),聽眾接收更新事件,并設(shè)置自定義類型的能力評(píng)估。有兩件,以生動(dòng)活潑的屬性:動(dòng)畫值計(jì)算和設(shè)置這些對(duì)象的屬性動(dòng)畫值。ValueAnimator不進(jìn)行第二件,所以你一定要更新計(jì)算值ValueAnimator和修改你想用自己的邏輯動(dòng)畫的對(duì)象。請(qǐng)參閱有關(guān)更多信息Animating with ValueAnimator部分 。 |
| ObjectAnimator | ValueAnimator的子類,允許你設(shè)置一個(gè)目標(biāo)對(duì)象和對(duì)象屬性的動(dòng)畫。當(dāng)計(jì)算出一個(gè)新的動(dòng)畫值,本類更新相應(yīng)的屬性。你大部分情況使用ObjectAnimator,因?yàn)樗沟脛?dòng)畫的目標(biāo)對(duì)象的值更簡(jiǎn)單。然而,有時(shí)你直接使用ValueAnimator,因?yàn)镺bjectAnimator有一些限制,如對(duì)目標(biāo)對(duì)象目前要求的具體acessor方法。 |
| AnimatorSet | 提供機(jī)制,以組合動(dòng)畫一起,讓他們關(guān)聯(lián)性運(yùn)行。你可以設(shè)置動(dòng)畫一起播放,順序,或在指定的延遲之后。請(qǐng)參閱有關(guān)部分Choreographing multiple animations with Animator Sets更多信息。 |
評(píng)估人員告訴屬性動(dòng)畫系統(tǒng)如何計(jì)算一個(gè)給定的屬性值。他們采取的時(shí)機(jī),是由一個(gè)數(shù)據(jù)Animator 類,動(dòng)畫的開始和結(jié)束值,并計(jì)算基于此數(shù)據(jù)屬性的動(dòng)畫值。屬性動(dòng)畫系統(tǒng)提供了以下評(píng)價(jià):
??表2。?評(píng)價(jià)者(Evaluators)
| IntEvaluator | 默認(rèn)的計(jì)算器來計(jì)算int屬性值 |
| FloatEvaluator | 默認(rèn)評(píng)估值來計(jì)算浮動(dòng)屬性。 |
| ArgbEvaluator | 默認(rèn)的計(jì)算器計(jì)算值表示為十六進(jìn)制值的色彩屬性。 |
| TypeEvaluator | 一個(gè)接口,允許你創(chuàng)建自己的評(píng)估。如果你是動(dòng)畫對(duì)象的屬性,不是一個(gè)整數(shù),浮點(diǎn)數(shù),或顏色,你必須實(shí)現(xiàn)的TypeEvaluator接口指定如何計(jì)算對(duì)象屬性的動(dòng)畫值。您也可以指定自定義TypeEvaluator整數(shù),浮點(diǎn)數(shù),顏色值以及,如果你想對(duì)比默認(rèn)行為來處理這些類型的不同。請(qǐng)參閱有關(guān)部分Using a TypeEvaluator更多關(guān)于如何編寫自定義評(píng)估信息。 |
一個(gè)時(shí)間插補(bǔ)定義如何在一個(gè)動(dòng)畫的特定值作為時(shí)間函數(shù)的計(jì)算。例如,你可以指定動(dòng)畫發(fā)生線性在整個(gè)動(dòng)畫,這意味著動(dòng)畫均勻地移動(dòng)整個(gè)時(shí)間,或者可以指定使用非線性時(shí)間的動(dòng)畫,例如,在開始加速,并在最后減速動(dòng)畫。表3說明中所含的插值android.view.animation。如果沒有提供插值適合您的需要,實(shí)施TimeInterpolator接口,并創(chuàng)建自己的。請(qǐng)參閱Using interpolators如何編寫一個(gè)定制的插補(bǔ)的更多信息。
??表3。?插值(Interpolators)
| AccelerateDecelerateInterpolator | 插補(bǔ),其變化率慢慢開始和結(jié)束,但通過中間加速。 |
| AccelerateInterpolator | 插補(bǔ),其變化率開始緩慢,然后加快。 |
| AnticipateInterpolator | 內(nèi)插的變化開始落后,然后向前甩。 |
| AnticipateOvershootInterpolator | 內(nèi)插的變化,開始落后,甩向前過沖目標(biāo)值,然后終于可以追溯到最終值。 |
| BounceInterpolator | 插補(bǔ),其變化在最后反彈。 |
| CycleInterpolator | 內(nèi)插動(dòng)畫重復(fù)指定的周期數(shù)。 |
| DecelerateInterpolator | 插補(bǔ),其變化的速度開始很快,然后減速。 |
| LinearInterpolator | 插補(bǔ),其變化率是恒定的 |
| OvershootInterpolator | 內(nèi)插的變化甩向前和過沖的最后一個(gè)值,然后回來。 |
| TimeInterpolator | 一個(gè)接口,使您實(shí)現(xiàn)自己的插補(bǔ)。 |
ValueAnimator動(dòng)畫
ValueAnimator類讓你動(dòng)畫動(dòng)畫的持續(xù)時(shí)間由某種類型的值指定了一套整,浮,或顏色值動(dòng)畫。您獲得通過ValueAnimator調(diào)用工廠方法之一:ofInt() , ofFloat() , or ofObject()。例如:
[java]?view plaincopy
在這段代碼中,ValueAnimator開始動(dòng)畫的計(jì)算值,1000毫秒,當(dāng)時(shí) ??間為0和1之間,運(yùn)行的start()方法。 你也可以指定一個(gè)自定義類型的動(dòng)畫通過執(zhí)行下列操作:
在這段代碼中,ValueAnimator開始計(jì)算之間的動(dòng)畫值,使用所提供的邏輯MyTypeEvaluator 的start()方法運(yùn)行時(shí)間為1000毫秒,當(dāng)startPropertyValue和endPropertyValue。
然而,前面的代碼片段,有沒有對(duì)象的實(shí)際效果,因?yàn)樵赩alueAnimator不直接操作對(duì)象或?qū)傩?。最有可能的事?#xff0c;你想要做的是修改這些計(jì)算值要進(jìn)行動(dòng)畫的對(duì)象。你定義在聽眾ValueAnimator妥善處理動(dòng)畫的壽命期間的重要事件,如幀更新。實(shí)施的聽眾時(shí),你可以通過調(diào)用特定的幀刷新計(jì)算值getAnimatedValue()。對(duì)聽眾的更多信息,請(qǐng)參閱有關(guān)部分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:
[java]?view plaincopy
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.
- 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:
- 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創(chuàng)編動(dòng)畫
在許多情況下,你要播放的動(dòng)畫,取決于另一個(gè)動(dòng)畫開始或者結(jié)束時(shí)。Android系統(tǒng),讓你捆綁動(dòng)畫到AnimatorSet一起,使您可以指定是否要同時(shí),按順序,或在指定的延遲后開始動(dòng)畫。你可以在對(duì)方還AnimatorSet對(duì)象。
從下面的示例代碼彈彈球樣品(簡(jiǎn)單修改)扮演下面的動(dòng)畫 對(duì)象以下列方式:
- Plays bounceAnim.
- Plays squashAnim1, squashAnim2, stretchAnim1, and stretchAnim2 at the same time.
- Plays bounceBackAnim.
- Plays fadeAnim.
對(duì)于如何使用動(dòng)畫集更完整的例子,彈彈球在APIDemos樣本。
動(dòng)畫聽眾
與下文所述的聽眾,你可以聽動(dòng)畫的持續(xù)時(shí)間期間的重要事件。
- Animator.AnimatorListener
- onAnimationStart()?-所謂的動(dòng)畫開始時(shí)。
- onAnimationEnd()?-在動(dòng)畫結(jié)束時(shí)調(diào)用。
- onAnimationRepeat()?-調(diào)用時(shí),動(dòng)畫的重演。
- onAnimationCancel()?-調(diào)用時(shí),動(dòng)畫將被取消。取消動(dòng)畫還可以調(diào)用onAnimationEnd(),不管他們是如何結(jié)束。
- ValueAnimator.AnimatorUpdateListener
- onAnimationUpdate()?-在動(dòng)畫的每一幀被調(diào)用。聽此事件使用所產(chǎn)生的計(jì)算值ValueAnimator在一個(gè)動(dòng)畫。使用的值,查詢ValueAnimator對(duì)象傳遞到事件與目前動(dòng)畫值getAnimatedValue()方法。如果使用ValueAnimator實(shí)現(xiàn)這個(gè)監(jiān)聽器是必需的。
這取決于你是動(dòng)畫什么屬性或?qū)ο?#xff0c;你可能需要瀏覽強(qiáng)制重繪新的動(dòng)畫值,到屏幕面積上,調(diào)用invalidate()。例如,動(dòng)畫的繪制對(duì)象的顏色屬性,僅造成更新到屏幕上時(shí),該對(duì)象重繪本身。在視圖的全部屬性set方法,例如[http://developer.android.com/reference/android/view/View.html#setAlpha(float)?setAlpha()]and?setTranslationX()初始化視圖屬性,所以你不用使用新值調(diào)用這些方法初始化視圖。
可以延長(zhǎng)AnimatorListenerAdapter類,而不是實(shí)施的Animator.AnimatorListener接口,如果你不想執(zhí)行的所有方法Animator.AnimatorListener接口。AnimatorListenerAdapter類提供的方法,你可以選擇覆蓋的空實(shí)現(xiàn)。
例如,Bouncing Balls(彈彈球)例子在API演示創(chuàng)建一個(gè)AnimatorListenerAdapter在onAnimationEnd()?回調(diào):
[java]?view plaincopy動(dòng)畫布局的變化,以ViewGroups
屬性動(dòng)畫系統(tǒng)提供的能力,以動(dòng)畫變化ViewGroup對(duì)象以及自己的動(dòng)畫視圖對(duì)象提供了一個(gè)簡(jiǎn)單的方法。
你可以在一個(gè)ViewGroup動(dòng)畫與布局的變化LayoutTransition類。一個(gè)ViewGroup內(nèi)部的視圖,可以通過動(dòng)畫出現(xiàn)和消失,當(dāng)您添加或刪除它們從一個(gè)ViewGroup或當(dāng)你調(diào)用一個(gè)視圖的setVisibility()的方法VISIBLE(可見),android.view.View#INVISIBLE(隱形),或去除。在ViewGroup中存在的視圖可以動(dòng)畫到你增加或者刪除視圖的新位置上。你可以通過調(diào)用定義在下面的動(dòng)畫LayoutTransition的對(duì)象android.animation.Animator) setAnimator()在通過動(dòng)畫對(duì)象有下列的LayoutTransition常量:
- 出現(xiàn)(APPEARING) -一個(gè)標(biāo)志,指示在容器中的項(xiàng)目上出現(xiàn)運(yùn)行的動(dòng)畫。
- CHANGE_APPEARING -一個(gè)標(biāo)志,指示一個(gè)新的在容器中項(xiàng)目上出現(xiàn)運(yùn)行的動(dòng)畫。
- 消失(DISAPPEARING) -一個(gè)標(biāo)志,指示動(dòng)畫的運(yùn)行項(xiàng)目,從容器中消失。
- CHANGE_DISAPPEARING -一個(gè)標(biāo)志,指示一個(gè)從容器中的項(xiàng)目上運(yùn)行的動(dòng)畫消失。
你可以定義自己的自定義動(dòng)畫這四種類型的事件定制的布局過渡,或只是告訴動(dòng)畫系統(tǒng)使用默認(rèn)的動(dòng)畫。
在LayoutAnimations的API演示示例顯示您如何定義布局過渡的動(dòng)畫,然后設(shè)置要?jiǎng)赢嬕晥D對(duì)象的動(dòng)畫。
在LayoutAnimationsByDefault其的相應(yīng)layout_animations_by_default.xml布局資源文件表明您如何啟用在XML ViewGroups的默認(rèn)布局轉(zhuǎn)換。你唯一需要做的是設(shè)置的 Android :animateLayoutchanges 屬性為 true 的ViewGroup的。例如:
[java]?view plaincopy這個(gè)屬性設(shè)置為true,自動(dòng)動(dòng)畫ViewGroup增加或移除存在于ViewGroup的視圖。
使用一個(gè)TypeEvaluator
如果你想動(dòng)畫的類型是Android系統(tǒng)沒有的,可以通過實(shí)施創(chuàng)建自己定義TypeEvaluator接口。被稱為Android系統(tǒng)的類型是整數(shù),浮點(diǎn)數(shù),或一種顏色,這是由支持的IntEvaluator,FloatEvaluator,和ArgbEvaluator的類型評(píng)估。
只有一個(gè)方法來實(shí)現(xiàn)在TypeEvaluator接口,T, T) evaluate()方法。這允許您使用您的動(dòng)畫屬性的一個(gè)適當(dāng)?shù)闹捣祷卦诋?dāng)前點(diǎn)的動(dòng)畫。FloatEvaluator類演示了如何做到這一點(diǎn):
[java]?view plaincopy
注:當(dāng)ValueAnimator(或ObjectAnimator)運(yùn)行時(shí),它計(jì)算消耗動(dòng)畫(0和1之間的值)的一小部分,然后 計(jì)算出一個(gè)插值取決于上什么插補(bǔ)您正在使用的版本。插值分?jǐn)?shù)您的TypeEvaluator,通過接收部分參數(shù),所以你沒有考慮到插補(bǔ)計(jì)算動(dòng)畫值時(shí)。
使用插值
內(nèi)插定義動(dòng)畫中的具體值作為時(shí)間函數(shù)的計(jì)算。例如,你可以指定動(dòng)畫發(fā)生線性在整個(gè)動(dòng)畫,這意味著動(dòng)畫均勻地移動(dòng)整個(gè)時(shí)間,或者可以指定使用非線性時(shí)間的動(dòng)畫,例如,使用加速或減速的開始或結(jié)束動(dòng)畫。
動(dòng)畫集代表經(jīng)過時(shí)間的動(dòng)畫內(nèi)插動(dòng)畫系統(tǒng)收到的一小部分。插值修改這個(gè)分?jǐn)?shù)與動(dòng)畫的類型相吻合,它旨在提供。Android系統(tǒng)提供了一個(gè)共同插值設(shè)置的android.view.animation包。如果沒有這些適合您的需求,可以實(shí)現(xiàn)TimeInterpolator接口,并創(chuàng)建自己的。
作為一個(gè)例子,如何默認(rèn)的插補(bǔ)AccelerateDecelerateInterpolator和LinearInterpolator計(jì)算的插值分?jǐn)?shù)比較如下。在LinearInterpolator沒有經(jīng)過部分的影響。AccelerateDecelerateInterpolator加速到動(dòng)畫和它的減速。下列方法確定這些插值的邏輯:
AccelerateDecelerateInterpolator
[java]?view plaincopy
LinearInterpolator
下表表示持續(xù)1000毫秒的動(dòng)畫,這些插值計(jì)算的近似值:
| 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毫秒之間慢。
指定關(guān)鍵幀
一個(gè)關(guān)鍵幀Keyframe的對(duì)象包括時(shí)間/值對(duì),可以讓你定義一個(gè)特定的狀態(tài),在特定時(shí)間的動(dòng)畫。每個(gè)關(guān)鍵幀也可以有自己的插補(bǔ)控制動(dòng)畫中的前一個(gè)關(guān)鍵幀之間的時(shí)間,這個(gè)關(guān)鍵幀的時(shí)間間隔的行為。
實(shí)例化一個(gè)關(guān)鍵幀Keyframe的對(duì)象,你必須使用工廠方法之一的,ofInt()?,ofFloat()?,或ofObject()獲得適當(dāng)類型的關(guān)鍵幀。然后,你調(diào)用android.animation.Keyframe...) ofKeyframe()工廠方法獲得PropertyValuesHolder對(duì)象。一旦你有了這個(gè)對(duì)象,你可以通過獲得一個(gè)動(dòng)畫PropertyValuesHolder對(duì)象和對(duì)象的動(dòng)畫。下面的代碼片段演示了如何做到這一點(diǎn):
對(duì)于如何使用關(guān)鍵幀的更完整的范例,看到MultiPropertyAnimation在APIDemos的例子。
動(dòng)畫視圖
屬性動(dòng)畫系統(tǒng)允許精簡(jiǎn)視圖對(duì)象的動(dòng)畫和offerse在視圖動(dòng)畫系統(tǒng)有幾個(gè)優(yōu)點(diǎn)。視圖動(dòng)畫系統(tǒng)改造,通過改變,他們繪制的方式查看對(duì)象。這是在每個(gè)視圖的容器處理,因?yàn)橐晥D本身沒有屬性來操作。這導(dǎo)致在動(dòng)畫的查看,但沒有造成人員在視圖對(duì)象本身的變化。這導(dǎo)致的行為,如在原來的位置上仍然存在的對(duì)象,即使是在屏幕上的不同位置上繪制。在Android 3.0中,增加了新的屬性和相應(yīng)的getter和setter方法??來消除這一缺點(diǎn)。
屬性動(dòng)畫系統(tǒng)可以改變視圖對(duì)象的實(shí)際屬性,動(dòng)畫在屏幕上的視圖。此外,視圖還自動(dòng)調(diào)用invalidate()方法來刷新屏幕時(shí)改變其屬性。在新的屬性視圖類促進(jìn)屬性動(dòng)畫的是:
- translationX和translationY:這些屬性控制的地方查看位于從它的左側(cè)和頂部設(shè)置其布局容器的坐標(biāo)的增量。
- rotation, rotationX, and rotationY: 這些屬性控制在2D旋轉(zhuǎn)(旋轉(zhuǎn)屬性)和3D的支點(diǎn)周圍。
- scaleX 和 scaleY :這些屬性控制周圍的支點(diǎn)視角的2D縮放。
- pivotX和pivotY:這些屬性控制支點(diǎn)的位置,圍繞著它旋轉(zhuǎn)和縮放變換發(fā)生。默認(rèn)情況下,支點(diǎn)位于中心的對(duì)象。
- x 和 y :這些都是簡(jiǎn)單實(shí)用的特性來形容查看其容器的最終位置,左側(cè)和頂部值和translationX和translationY的價(jià)值的總和。
- alpha :代表alpha透明度。此值默認(rèn)為1(不透明),0代表完全透明(不可見)。
動(dòng)畫視圖對(duì)象的屬性,如它的顏色或旋轉(zhuǎn)值,所有你需要做的是創(chuàng)建一個(gè)屬性的動(dòng)畫,并指定要?jiǎng)赢嬕晥D屬性。例如:
創(chuàng)建動(dòng)畫的詳細(xì)信息,請(qǐng)參閱與動(dòng)畫ValueAnimator和ObjectAnimator。
ViewPropertyAnimator的動(dòng)畫
在該ViewPropertyAnimator并行動(dòng)畫的幾個(gè)屬性查看,使用單一的基礎(chǔ)Animator對(duì)象提供了一個(gè)簡(jiǎn)單的方法。它的行為像一個(gè)ObjectAnimator,因?yàn)樗薷囊晥D屬性的實(shí)際值,但更有效的動(dòng)畫時(shí)許多屬性一次。此外,使用的的代碼ViewPropertyAnimator是更簡(jiǎn)潔,更易于閱讀。下面的代碼片段顯示在使用多個(gè)的差異ObjectAnimator對(duì)象,一個(gè)的單ObjectAnimator,以及時(shí)的ViewPropertyAnimator同時(shí)動(dòng)畫的 x 和 y 屬性視圖。
Multiple ObjectAnimator objects
One ObjectAnimator
[java]?view plaincopy
ViewPropertyAnimator
如需更詳細(xì)的信息ViewPropertyAnimator,看到相應(yīng)的Android開發(fā)者博客文章
在XML定義動(dòng)畫
屬性動(dòng)畫系統(tǒng),可以讓你聲明與XML屬性動(dòng)畫,而不是做編程。在XML中定義的動(dòng)畫,你可以很容易地在多個(gè)活動(dòng)中重用的動(dòng)畫更容易編輯動(dòng)畫序列。來區(qū)分使用那些使用傳統(tǒng)的新的屬性的動(dòng)畫API 動(dòng)畫框架與Android 3.1開始,動(dòng)畫文件,你應(yīng)該保存的XML文件中的屬性的動(dòng)畫res/animator/目錄代替res/anim/。使用動(dòng)畫的目錄名稱是可選的,但必要的,如果你想使用的布局編輯工具,在Eclipse的ADT插件(ADT 11.0.0 +),因?yàn)锳DT只搜索res/animator/目錄屬性的動(dòng)畫資源。
下列屬性動(dòng)畫類有下列XML標(biāo)記的XML聲明支持:
- ValueAnimator?- <animator>
- ObjectAnimator?- <objectAnimator>
- AnimatorSet?- <SET>
下面的示例播放兩套對(duì)象的動(dòng)畫順序,第一個(gè)是嵌套播放兩個(gè)對(duì)象的動(dòng)畫:
[java]?view plaincopy為了運(yùn)行這個(gè)動(dòng)畫,你必須在你的代碼XML資源到AnimatorSet對(duì)象,然后設(shè)置目標(biāo)對(duì)象為所有的動(dòng)畫開始前的動(dòng)畫設(shè)置。調(diào)用setTarget()設(shè)置一個(gè)所有兒童的單目標(biāo)對(duì)象AnimatorSet的作為一種方便。下面的代碼演示如何做到這一點(diǎn):
[java]?view plaincopy為定義屬性動(dòng)畫的XML語法的信息,請(qǐng)參閱動(dòng)畫資源Animation Resources。
總結(jié)
以上是生活随笔為你收集整理的ObjectAnimation的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无线警务移动办公系统解决方案
- 下一篇: imx6ull u-boot 下载/编译