Android Animation學習(一) Property Animation介紹
Android Animation
Android framework提供了兩種動畫系統: property animation (introduced in Android 3.0)和view animation。
除了這兩種系統外,也可以利用Drawable animation,也就是播放序列幀圖像。
所以,Android中的Animation分三種:
下面主要說Property Animation。
Property Animation
Property Animation是Android 3.0引進的,也即API Level 11,這套系統讓你可以對任意對象(即便不在屏幕上的對象)的任何屬性進行動畫變換。
為了讓某件東西動起來,你指定對象要變換的屬性,動畫持續的時間,以及你在動畫之中想要達到的值即可。
通過property animation系統你可以定義一個動畫的下列特性:
Duration: 動畫持續的時間,默認值是300ms。
Time interpolation: 時間插值,你可以定義隨着時間的變換,屬性值是如何變換的。
Repeat count and behavior: 你可以指定一個動畫是否重復進行,以及重復幾次,也可以指定是否讓動畫倒着回放,這樣可以動畫可以來回進行,直到達到了所要求的重復次數。
Animator sets: 你可以把動畫行為組織成一個邏輯集合,它們一起播放或者順序播放,或者也可以在指定的延遲后播放。
Frame refresh delay: 你可以指定多久刷新一次你的動畫的幀。默認值被設置為每10ms,但是你的應用刷新幀的頻率是和系統當前的實際情況相關的。
原理介紹 How Property Animation Works
ValueAnimator
對象持有動畫時間,比如動畫已經進行了多長時間,和變換的屬性的當前值。
ValueAnimator中封裝了TimeInterpolator和TypeEvaluator。
TimeInterpolator定義了動畫的時間插值,比如可以線性或者加速減速;
TypeEvaluator定義了如何計算被動畫改變的屬性值。
為了開啟一個動畫,首先構造一個ValueAnimator對象,把你想要改變的屬性值的起始值、終止值以及動畫時間告訴它,當你調用 start()
方法時,動畫開始。
在整個動畫的過程中,所涉及的計算分為下面三步:
1.這個ValueAnimator對象會根據動畫的總時間和已經流逝的時間計算出一個0到1之間的elapsed fraction值。這個elapsed fraction值就代表了時間完成的程度。
2.計算出elapsed fraction之后,ValueAnimator對象會調用TimeInterpolator
來計算一個interpolated fraction,即,根據所設置的時間插值方法將elapsed fraction映射到interpolated fraction。
如果是線性插值的話elapsed fraction和interpolated fraction會是一直相等的,但是非線性變換就不是了。
3. interpolated fraction計算出來后,ValueAnimator
會調用TypeEvaluator
,來進行你要動畫的屬性值的計算。
這時候用的輸入參數就是interpolated fraction的值,以及屬性值的起始值和終止值。
整個過程如下圖所示:
Property Animation API
Property Animation系統的大多數API都在這個包中:android.animation
但是由於View Animation系統定義了一些插值器(interpolator),所以你可以直接使用這個包android.view.animation中的一些插值器。
鏈接:http://developer.android.com/guide/topics/graphics/prop-animation.html中的API Overview部分列表介紹了Property Animation的API,可前往查看。
API主要分為Table 1. Animators,Table 2. Evaluators,Table 3. Interpolators三大部分。
Animatiors中:
ValueAnimator只計算出屬性值,並不將屬性值設置在對象上,所以你必須監聽屬性值的更新,自己修改對象的屬性,實現動畫邏輯。
用法見:Animating with ValueAnimator
ObjectAnimator是ValueAnimator的子類,在構造函數中傳入了目標對象和對應屬性值的名字(要求對象類有相應的get/set方法),會自動進行屬性值得設置。
用法見:Animating with ObjectAnimator
AnimatorSet提供了Animation的組合,
用法見: Choreographing multiple animations with Animator Sets
另外,Animation Listeners也很重要:Animation Listeners
Property Animation和View Animation的關系
View Animation是比較舊的一套系統,僅僅適用於View對象。
並且View Animation系統限制了可以動畫的方面,比如縮放和旋轉是可以的,但是背景顏色的動畫是做不了的。
View Animation系統的另一個缺陷就是它僅僅改變了View繪制的位置,並沒有改變View本身實際的位置。
比如,如果你讓一個按鈕通過動畫移動到屏幕上的另一個位置,雖然它繪制在目標位置,但是你要點擊它還是要在原來的位置,所以你需要自己寫邏輯去處理這種問題。
Property animation系統就不存在上面的問題,它是確實地改變了View對象的真實屬性。
從Android 3.0起,View類加了很多屬性和方法用來進行Property animation。
這些屬性有:
translationX and translationY rotation, rotationX, and rotationY scaleX and scaleY pivotX and pivotY x and y alpha
當這些屬性值被改變的時候,View會自動調用 invalidate()
方法來進行刷新。
Declaring Animations in XML
Property animation系統允許你用xml來聲明動畫,這樣做一是可以達到動畫的復用,通用性更強,另一個好處是編輯多個動畫的序列更加容易,可讀性更好。
為了區分Property animation和View animation的資源文件,從Android 3.1開始,Property animation的xml文件存在res/animator/目錄下(View animation的存在res/anim/目錄下), animator這個名是可選的。但是如果你想要使用Eclipse ADT plugin (ADT 11.0.0+)的布局編輯器,你就必須使用res/animator/目錄,因為ADT只在該目錄下尋找property animation的資源文件。
對應的標簽如下:
ValueAnimator
-<animator>
ObjectAnimator
-<objectAnimator>
AnimatorSet
-<set>
定義Property Animation的語義,詳見: Animation Resources
參考資料
官方API Guides:
http://developer.android.com/guide/topics/graphics/overview.html
Property Animation:
http://developer.android.com/guide/topics/graphics/prop-animation.html
Property Animation package:
http://developer.android.com/reference/android/animation/package-summary.html