天天看點

animation的xml定義中的android:interpolator屬性

 1.  <?xml  version="1.0"  encoding="utf-8"?>     

2.  <set   

3.  xmlns:android="http://schemas.android.com/apk/res/android"   

4.  android:interpolator="@android:anim/decelerate_interpolator"> 

<scale  android:fromxscale="2.0"  android:toxscale="1.0"     

5.  android:fromyscale="2.0"  android:toyscale="1.0"     

6.  android:pivotx="50%p"  android:pivoty="50%p"     

7.  android:duration="@android:integer/config_mediumanimtime"  /> 

</set>     

可能有很多人不了解其中的android:interpolator="@android:anim/decelerate_interpolator"是什麼含義,文檔裡說的也不太清楚,其實很簡單,看下面:

      interpolator定義一個動畫的變化率(the rate of change)。這使得基本的動畫效果(alpha, scale, translate, rotate)得以加速,減速,重複等。

用通俗的一點的話了解就是:動畫的進度使用 interpolator 控制。interpolator 定義了動畫的變化速度,可以實作勻速、正加速、負加速、無規則變加速等。interpolator 是基類,封裝了所有 interpolator 的共同方法,它隻有一個方法,即 getinterpolation (float input),該方法 maps a point on the timeline to a multiplier to be applied to the transformations of an

animation。android 提供了幾個 interpolator 子類,實作了不同的速度曲線,如下:

acceleratedecelerateinterpolator        在動畫開始與介紹的地方速率改變比較慢,在中間的時侯加速

accelerateinterpolator        在動畫開始的地方速率改變比較慢,然後開始加速

cycleinterpolator        動畫循環播放特定的次數,速率改變沿着正弦曲線

decelerateinterpolator        在動畫開始的地方速率改變比較慢,然後開始減速

linearinterpolator        在動畫的以均勻的速率改變

對于 linearinterpolator ,變化率是個常數,即 f (x) = x.

public float getinterpolation(float input) {

return input;

}

interpolator其他的幾個子類,也都是按照特定的算法,實作了對變化率。還可以定義自己的 interpolator 子類,實作抛物線、自由落體等實體效果。

繼續閱讀