Android Animation

Android中經常使用兩種動畫模式,tween animation和frame animation,即補間動畫和幀動畫,但在android3.0中又引入了一個新的動畫系統:property animation,即屬性動畫,這三種動畫模式在SDK中被稱爲property animation,view animation,drawable animation。若是想在3.0以前的版本支持property animation的話,那麼可以使用NineOldAndroids,大神JakeWharton的又一開源項目。android

View Animation(Tween Animation)

View animation只能應用於View對象,並且只支持一部分屬性,如支持縮放旋轉而不支持背景顏色的改變。git

並且對於View animation,它只是改變了View對象繪製的位置,而沒有改變View對象自己,好比,你有一個Button,座標(100,100),Width:200,Height:50,而你有一個動畫使其變爲Width:100,Height:100,你會發現動畫過程當中觸發按鈕點擊的區域還是(100,100)-(300,150)。github

  View Animation就是一系列View形狀的變換,如大小的縮放,透明度的改變,位置的改變,動畫的定義既能夠用代碼定義也能夠用XML定義,固然,建議用XML定義。ruby

  能夠給一個View同時設置多個動畫,好比從透明至不透明的淡入效果,與從小到大的放大效果,這些動畫能夠同時進行,也能夠在一個完成以後開始另外一個。函數

用XML定義的動畫放在/res/anim/文件夾內,XML文件的根元素能夠爲alpha, scale, translate, rotate, interpolator元素或set(表示以上幾個動畫的集合,set能夠嵌套)。默認狀況下,全部動畫是同時進行的,能夠經過startOffset屬性設置各個動畫的開始偏移(開始時間)來達到動畫順序播放的效果。動畫

  能夠經過設置interpolator屬性改變更畫漸變的方式,如AccelerateInterpolator,開始時慢,而後逐漸加快。默認爲AccelerateDecelerateInterpolator。this

  定義好動畫的XML文件後,能夠經過相似下面的代碼對指定View應用動畫。lua

ImageView image = (ImageView)findViewById(R.id.image); Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation); spaceshipImage.startAnimation(animation);

TimeInterplator

Time interplator定義了屬性值變化的方式,如線性均勻改變,開始慢而後逐漸快等。在Property Animation中是TimeInterplator,在View Animation中是Interplator,這兩個是同樣的,在3.0以前只有Interplator,3.0以後實現代碼轉移至了TimeInterplator。Interplator繼承自TimeInterplator,內部沒有任何其餘代碼。spa

  • AccelerateInterpolator      加速,開始時慢中間加速code

  • DecelerateInterpolator       減速,開始時快而後減速

  • AccelerateDecelerateInterolator  先加速後減速,開始結束時慢,中間加速

  • AnticipateInterpolator       反向 ,先向相反方向改變一段再加速播放

  • AnticipateOvershootInterpolator  反向加回彈,先向相反方向改變,再加速播放,會超出目的值而後緩慢移動至目的值

  • BounceInterpolator        跳躍,快到目的值時值會跳躍,如目的值100,後面的值可能依次爲85,77,70,80,90,100

  • CycleIinterpolator         循環,動畫循環必定次數,值的改變爲一正弦函數:Math.sin(2 * mCycles * Math.PI * input)

  • LinearInterpolator         線性,線性均勻改變

  • OvershottInterpolator       回彈,最後超出目的值而後緩慢改變到目的值

  • TimeInterpolator         一個接口,容許你自定義interpolator,以上幾個都是實現了這個接口

Drawable Animation(Frame Animation)

Drawable Animation(Frame Animation):幀動畫,就像GIF圖片,經過一系列Drawable依次顯示來模擬動畫的效果。在XML中的定義方式以下:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/loading01" android:duration="200" />  <item android:drawable="@drawable/loading02" android:duration="200" />  <item android:drawable="@drawable/loading03" android:duration="200" /> </animation-list>

必須以animation-list爲根元素,以item表示要輪換顯示的圖片,duration屬性表示各項顯示的時間。XML文件要放在/res/drawable/目錄下。示例:

ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setBackgroundResource(R.drawable.drawable_anim); anim = (AnimationDrawable) imageView.getBackground(); anim.start();

Property Animation

屬性動畫,這個是在Android 3.0中才引進的,之前學WPF時裏面的動畫機制好像就是這個,它更改的是對象的實際屬性,在View Animation(Tween Animation)中,其改變的是View的繪製效果,真正的View的屬性保持不變,好比不管你在對話中如何縮放Button的大小,Button的有效點擊區域仍是沒有應用動畫時的區域,其位置與大小都不變。而在Property Animation中,改變的是對象的實際屬性,如Button的縮放,Button的位置與大小屬性值都改變了。並且Property Animation不止能夠應用於View,還能夠應用於任何對象。Property Animation只是表示一個值在一段時間內的改變,當值改變時要作什麼事情徹底是你本身決定的。

在Property Animation中,能夠對動畫應用如下屬性:

  • Duration:動畫的持續時間

  • TimeInterpolation:屬性值的計算方式,如先快後慢

  • TypeEvaluator:根據屬性的開始、結束值與TimeInterpolation計算出的因子計算出當前時間的屬性值

  • Repeat Count and behavoir:重複次數與方式,如播放3次、5次、無限循環,能夠此動畫一直重複,或播放完時再反向播放

  • Animation sets:動畫集合,便可以同時對一個對象應用幾個動畫,這些動畫能夠同時播放也能夠對不一樣動畫設置不一樣開始偏移

  • Frame refreash delay:多少時間刷新一次,即每隔多少時間計算一次屬性值,默認爲10ms,最終刷新時間還受系統進程調度與硬件的影響

具體使用方式,見NineOldAndroids官網

相關文章
相關標籤/搜索