Android怎樣播放多張圖片形成一個動畫,
在Android裡可以逐幀的播放圖片,然後產生一種動態效果,準備好幾張連續的圖片,然後在於來源程式res檔案夾下建立anim檔案夾,然後建立一個XML
XML代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true">
- <item android:drawable="@drawable/c1" android:duration="200" />
- <item android:drawable="@drawable/c2" android:duration="200" />
- <item android:drawable="@drawable/c3" android:duration="200" />
- <item android:drawable="@drawable/c4" android:duration="200" />
- <item android:drawable="@drawable/c5" android:duration="200" />
- <item android:drawable="@drawable/c6" android:duration="200" />
- </animation-list>
其中c1,c2,c3,c4,c5,c6是加入的圖片的名稱。
在表單裡面放置一個ImageView控制項,並在代碼中編寫:
_imageView1 =(ImageView)findViewById(R.id.imageView1);//放置的ImageView控制項
- //設定動畫背景
- _imageView1.setBackgroundResource(R.anim.animation_list);//其中R.anim.animation_list就是上一步準備的動畫描述檔案的資源名
- //獲得動畫對象
- _animaition = (AnimationDrawable)_imageView1.getBackground();
- 最後,就可以啟動動畫了,代碼如下:
- //是否僅僅啟動一次?
- _animaition.setOneShot(false);
- if(_animaition.isRunning())//是否正在運行?
- {
- _animaition.stop();//停止
- }
- _animaition.start();//啟動