Android framework提供了兩種動畫系統: property animation (introduced in Android 3.0)和view animation。html
除了這兩種系統外,也能夠利用Drawable animation,也就是播放序列幀圖像。android
因此,Android中的Animation分三種:編輯器
1. Property Animationide
2. View Animation函數
3. Drawable Animation佈局
下面主要說Property Animation。動畫
Property Animation是Android 3.0引進的,也即API Level 11,這套系統讓你能夠對任意對象(即使不在屏幕上的對象)的任何屬性進行動畫變換。ui
爲了讓某件東西動起來,你指定對象要變換的屬性,動畫持續的時間,以及你在動畫之中想要達到的值便可。lua
經過property animation系統你能夠定義一個動畫的下列特性:spa
Duration: 動畫持續的時間,默認值是300ms。
Time interpolation: 時間插值,你能夠定義隨着時間的變換,屬性值是如何變換的。
Repeat count and behavior: 你能夠指定一個動畫是否重複進行,以及重複幾回,也能夠指定是否讓動畫倒着回放,這樣能夠動畫能夠來回進行,直到達到了所要求的重複次數。
Animator sets: 你能夠把動畫行爲組織成一個邏輯集合,它們一塊兒播放或者順序播放,或者也能夠在指定的延遲後播放。
Frame refresh delay: 你能夠指定多久刷新一次你的動畫的幀。默認值被設置爲每10ms,可是你的應用刷新幀的頻率是和系統當前的實際狀況相關的。
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都在這個包中: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
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()
方法來進行刷新。
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