程式功能,點擊按鈕時,圖片一張一張迴圈播放:
直接來代碼:
AppMain.java
[java] view plaincopy
- package lxy.litsoft;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class AppMain extends Activity {
- //聲明對象
- ImageView disPic;
- Button btDis;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //執行個體化對象
- disPic = (ImageView)findViewById(R.id.imageView01);
- btDis = (Button)findViewById(R.id.button01);
- //綁定監聽器
- btDis.setOnClickListener(new ButtonListener());
- }
- //按鈕的監聽器實現
- class ButtonListener implements OnClickListener{
- public void onClick(View v) {
- //動畫播放
- disPic.setBackgroundResource(R.drawable.anim);
- AnimationDrawable animationDrawable = (AnimationDrawable)disPic.getBackground();
- animationDrawable.start();
- }
- }
- }
main.xml
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"/>
- <ImageView
- android:id="@+id/imageView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></ImageView>
- <Button
- android:id="@+id/button01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Anim"></Button>
- </LinearLayout>
res/drawable/anim.xml
[html] view plaincopy
- <animation-list
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- <item android:drawable="@drawable/p30"
- android:duration="300"/>
- <item android:drawable="@drawable/p31"
- android:duration="300"/>
- <item android:drawable="@drawable/p32"
- android:duration="300"/>
- <item android:drawable="@drawable/p33"
- android:duration="300"/>
- <item android:drawable="@drawable/p34"
- android:duration="300"/>
- <item android:drawable="@drawable/p35"
- android:duration="300"/>
- <item android:drawable="@drawable/p36"
- android:duration="300"/>
- <item android:drawable="@drawable/p37"
- android:duration="300"/>
- </animation-list>
其中每一個item就是一幀的資源,android:drawable是圖片資源,android:duration是每一幀的顯示時間。