個人呢寫的比較的簡單,只是為了練習使用
代碼如下
java代碼
package mars.com;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;import android.widget.Toast;public class Demo_Activity extends Activity {private Button button1;private Button button2;private ImageView image;private AnimationDrawable ad;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);image = (ImageView) findViewById(R.id.image);ad = (AnimationDrawable) image.getBackground();// 聲明幀動畫對象button1.setOnClickListener(new OnClickListener() {public void onClick(View v) {ad.addFrame(getResources().getDrawable(R.drawable.p0), 100);// 添加幀ad.addFrame(getResources().getDrawable(R.drawable.p1), 100);// 添加幀ad.addFrame(getResources().getDrawable(R.drawable.p2), 100);// 添加幀ad.addFrame(getResources().getDrawable(R.drawable.p3), 100);// 添加幀ad.addFrame(getResources().getDrawable(R.drawable.p4), 100);// 添加幀ad.setAlpha(100);// 設定透明度ad.setOneShot(true);// 設定是否迴圈播放ad.start();// 開始Toast.makeText(Demo_Activity.this,"幀數:" + ad.getNumberOfFrames(), Toast.LENGTH_SHORT);// 擷取幀數}});button2.setOnClickListener(new OnClickListener() {public void onClick(View v) {ad.stop();// 結束}});}}
main.xml代碼如下
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="執行動畫" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止動畫" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/anim" /></LinearLayout>
設定檔anim.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/p0" android:duration="50"> </item> <item android:drawable="@drawable/p1" android:duration="50"> </item> <item android:drawable="@drawable/p2" android:duration="50"> </item> <item android:drawable="@drawable/p3" android:duration="50"> </item> <item android:drawable="@drawable/p4" android:duration="50"> </item></animation-list>
OK,然後把自己弄好的圖片放在自己的drawable檔案夾下就行了,這裡就不傳圖片了,怪麻煩的。