OverView
An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
定義多幀畫面連續(xù)播放構(gòu)成了幀動畫。幀動畫的應(yīng)用場景不多,主要用在過于復(fù)雜而無法用代碼實(shí)現(xiàn)的gif效果锄弱,如一些復(fù)雜度比較高的loading效果等考蕾。
使用
幀動畫的定義非常簡單
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/loading_icon_1"
android:duration="70"/>
<item android:drawable="@drawable/loading_icon_2"
android:duration="70"/>
<item android:drawable="@drawable/loading_icon_3"
android:duration="70"/>
<item android:drawable="@drawable/loading_icon_4"
android:duration="70"/>
</animation-list>
以animation-list作為根節(jié)點(diǎn),每一個item就是一幀動畫drawable對應(yīng)一張圖片資源会宪。android:oneshot="false"
指定只播放一次還是循環(huán)播放肖卧。
使用同樣很簡單幀動畫一般作為View的背景或者src來使用:
<ImageView
android:id="@+id/drawable_anim_iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="32dp"
android:src="@drawable/loading_animation_list"/>
需要注意的是即使設(shè)置為background了幀動畫也不會自動播放,需要我們start一下掸鹅。
AnimationDrawable animationDrawable = (AnimationDrawable)(drawableAnimIV.getDrawable());
if(animationDrawable.isRunning()){
animationDrawable.stop();
} else{
animationDrawable.start();
}
需要注意的是start()
方法不能用來Activity的onCreate中塞帐,因?yàn)榇藭r頁面還沒渲染完。通常放到onWindowFocusChanged()
方法中處理比較合適巍沙,當(dāng)然這也要看你的具體需求葵姥。