Android 動畫詳解之Frame動畫 (Drawable Animation),androiddrawable
Frame動畫就像是gif圖,通過一些靜態圖片來達到動畫的效果。
Android sdk中的AnimationDrawable就是專門針對Frame動畫,當然Frame動畫也可在java代碼或者xml中寫,但是提倡大家還是在xml中寫,先上個。
<span style="font-size:18px;"><?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/market_loading_01" android:duration="100"/> <item android:drawable="@drawable/market_loading_02" android:duration="100"/> <item android:drawable="@drawable/market_loading_03" android:duration="100"/> <item android:drawable="@drawable/market_loading_04" android:duration="100"/> <item android:drawable="@drawable/market_loading_05" android:duration="100"/> <item android:drawable="@drawable/market_loading_06" android:duration="100"/> <item android:drawable="@drawable/market_loading_07" android:duration="100"/> <item android:drawable="@drawable/market_loading_08" android:duration="100"/> <item android:drawable="@drawable/market_loading_09" android:duration="100"/> <item android:drawable="@drawable/market_loading_10" android:duration="100"/> <item android:drawable="@drawable/market_loading_11" android:duration="100"/> <item android:drawable="@drawable/market_loading_12" android:duration="100"/></animation-list></span>
Frame動畫在xml中的根節點是<animation-list>其中的oneshot=false是迴圈播放,為true的話則播放至最後一張圖片就會停止播放,在java中調用
ImageView imageView;AnimationDrawable animationDrawable;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView = (ImageView) findViewById(R.id.image);imageView.setBackgroundResource(R.drawable.drawable_progress);animationDrawable = (AnimationDrawable) imageView.getBackground();animationDrawable.start();
因為Frame 動畫是有一堆靜態圖構成的所以,可以當成background。
用java代碼寫的話
AnimationDrawable animationDrawable2 = new AnimationDrawable(); Drawable drawable = getResources().getDrawable(R.drawable.fengjing_1); Drawable drawable2 = getResources().getDrawable(R.drawable.fengjing_2); Drawable drawable3 = getResources().getDrawable(R.drawable.fengjing_3); animationDrawable2.addFrame(drawable, 1000); animationDrawable2.addFrame(drawable2, 1000); animationDrawable2.addFrame(drawable3, 1000); animationDrawable2.setOneShot(false); imageView.setBackgroundDrawable(animationDrawable2); animationDrawable2.start();
恩。。就是這樣了,Frame 動畫瞭解到這已經差不多了。
項目源碼