插補器
插補器是一個被定義在XML中的能夠影響動畫的變化頻率的編輯器。它會對既存動畫的加速、減速、重放、反彈等效果等形成影響。
使用android:interpolator屬性,把插補器應用到一個動畫元素,它的值是一個插補器資源的引用。
在Android中所有可用的插補器都是Interpolator類的子類。對於每個插補器類,Android都包含瞭一個能夠引用的公共資源,以便把這個插補器應用於動畫所使用的android:interpolator屬性。下表中指定瞭每個插補器可用的資源。
插補器類
資源ID
AccelerateDecelerateInterpolator
@android:anim/accelerate_decelerate_interpolator
AccelerateInterpolator
@android:anim/accelerate_interpolator
AnticipateInterpolator
@android:anim/anticipate_interpolator
AnticipateOvershootInterpolator
@android:anim/anticipate_overshoot_interpolator
BounceInterpolator
@android:anim/bounce_interpolator
CycleInterpolator
@android:anim/cycle_interpolator
DecelerateInterpolator
@android:anim/decelerate_interpolator
LinearInterpolator
@android:anim/linear_interpolator
OvershootInterpolator
@android:anim/overshoot_interpolator
可以像下面這樣來設定android:interpolator屬性:
<setandroid:interpolator="@android:anim/accelerate_interpolator">
…
</set>
定制插補器
如果對平臺提供的插補器不滿意(上表中列出的插補器),還可以編輯屬性的方法來創建定制的插補器資源。如,能夠調整AnticipateInterpolator插補器的加速頻率,也能夠調整CycleInterpolator插補器的周期數量。要達到這個目的,需要在一個XML文件中創建自己的插補器資源。
文件位置(FILE LOCATION):
res/anim/filename.xml,文件名會作為資源的ID。
被編譯的資源類型(COMPILED RESOURCE DATATYPE)
資源指向相應的插補器對象
資源引用(RESOURCE REFERENCE):
在XML中:@[package:]anim/filename
語法(SYNTAX):
<?xml version="1.0" encoding="utf-8"?>
<InterpolatorNamexmlns:android="http://schemas.android.com/apk/res/android"
android:attribute_name="value"
/>
如果沒有應用任何屬性,那麼這個定制的插補器會與平臺提供的插補器具有完全相同的功能。
元素(ELEMENTS):
註意,每個插補器的實現,當在XML中定義時,都是用小寫字母開頭的。
<accelerateDecelerateInterpolator>
降低動畫開始和結束時變化的頻率,但在動畫的中間部分加速。
沒有屬性
<accelerateInterpolator>
降低動畫啟動時變化的頻率,然後開始加速
屬性(ATTRIBUTES):
android:factor
浮點值,指定加速頻率(默認是1)
<anticipateInterpolator>
動畫啟動時先向後,然後再向前變化(橡皮筋效果)。
屬性(ATTRIBUTES):
android:tension
浮點值,指定拉力數(默認是2)
<anticipateOvershootInterpolator>
動畫啟動時先向後,然後再向前,並飛過目標值,然後再終點穩定。
屬性(ATTRIBUTES):
android:tension
浮點值,指定拉力數(默認是2)
android:extraTension
浮點值,指定拉力的倍數(默認是1.5)
<bounceInterpolator>
在動畫結尾的反彈效果。
沒有屬性
<cycleInterpolator>
用指定的循環數重復動畫,變化頻率是按照正弦模式進行。
屬性(ATTRIBUTES):
android:cycles
整數值,指定循環周期數(默認是1)。
<decelerateInterpolator>
動畫啟動時,快速跳出,然後減速。
屬性(ATTRIBUTES):
android:factor
浮點值,指定減速比率(默認是1)
<linearInterpolator>
動畫的變化頻率是固定的。
沒有屬性。
<overshootInterpolator>
向前拋出,並飛過終點,然後再返回來。
屬性(ATTRIBUTES):
android:tension
浮點值,設定拉力數(默認是2)
例子:
XML文件被保存在res/anim/my_overshoot_interpolator.xml:
<?xml version="1.0" encoding="utf-8"?>
<overshootInterpolatorxmlns:android="http://schemas.android.com/apk/res/android"
android:tension="7.0"
/>
以下是這個動畫XML使用的插補器:
<scalexmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/my_overshoot_interpolator"
android:fromXScale="1.0"
android:toXScale="3.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700"/>
幀動畫
定義在XML中動畫,它會電影那樣,按序播放圖片。
文件位置(FILE LOCATION):
res/drawable/filename.xml
文件名被用作資源的ID。
被編譯資源類型(COMPILED RESOURCE DATATYPE):
資源指向一個AnimationDrawable對象
資源引用(RESOURCE REFERENCE)
在Java代碼中:R.drawable.filename
在XML中:@[package:]drawable.filename
語法(SYNTAX):
<?xml version="1.0" encoding="utf-8"?>
<animation-listxmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource_name"
android:duration="integer"/>
</animation-list>
元素(ELEMENTS):
<animation-list>
必須的,它必須是根元素。它要包含一個或多個<item>元素。
屬性(ATTRIBUTES):
android:oneshot
佈爾值,如果隻想讓動畫執行一次,那麼就設定為“true”,否則,設定為“false”來循環播放動畫。
<item>
動畫的一幀,必須是<animation-list>元素的子元素。
屬性(ATTRIBUTES):
android:drawable
指定用於本幀動畫的可描畫資源。
android:duration
整數值,以毫秒為單位,顯示本幀動畫的持續時間。
例子:
XML文件被保存在res/anim/rocket.xml中:
<?xml version="1.0" encoding="utf-8"?>
<animation-listxmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<itemandroid:drawable="@drawable/rocket_thrust1"android:duration="200"/>
<itemandroid:drawable="@drawable/rocket_thrust2"android:duration="200"/>
<itemandroid:drawable="@drawable/rocket_thrust3"android:duration="200"/>
</animation-list>
應用程序代碼會把這個動畫設置為一個View對象的背景,然後播放動畫:
ImageView rocketImage =(ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation =(AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();
摘自 FireOfStar的專欄