Frame 動畫,即順序播放事先做好的映像,跟電影類似。
接下來的案例是點擊按鈕實現播放動畫,點擊停止實現停止動畫播放!
1、:
2、main.xml檔案很簡單:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="開始"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/stop" android:text="停止"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:id="@+id/image"/>
3、然後在drawable檔案夾下定義一個frame.xml,為的是把所有的圖片都定義在該xml檔案中,它是一個animation列表,存放所有使用到的圖片!!
<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" ><item android:drawable="@drawable/girl_1" android:duration="100" /><item android:drawable="@drawable/girl_2" android:duration="100" /><item android:drawable="@drawable/girl_3" android:duration="100" /><item android:drawable="@drawable/girl_4" android:duration="100" /><item android:drawable="@drawable/girl_5" android:duration="200" /><item android:drawable="@drawable/girl_6" android:duration="500" /><item android:drawable="@drawable/girl_7" android:duration="500" /><item android:drawable="@drawable/girl_8" android:duration="200" /><item android:drawable="@drawable/girl_9" android:duration="100" /><item android:drawable="@drawable/girl_10" android:duration="100" /><item android:drawable="@drawable/girl_11" android:duration="100" /></animation-list>
每個item裡面有個duration屬性,該屬性為了使該圖片持續多長時間!!
4、最後是java代碼:
package cn.csdn.anim;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 FrameActivity extends Activity implements OnClickListener {private Button button, stop;private ImageView image;private AnimationDrawable attackAnimation;//定義動畫的對象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init() {image = (ImageView) findViewById(R.id.image);//顯示動畫的imageviewbutton = (Button) findViewById(R.id.button);//開始動畫stop = (Button) findViewById(R.id.stop);//停止動畫button.setOnClickListener(this);stop.setOnClickListener(this);image.setBackgroundResource(R.drawable.frame);//設定顯示動畫的image的背景資源參數是int,就是你自己寫的frame.xml,裡面是所有相關的圖片attackAnimation = (AnimationDrawable) image.getBackground();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button:attackAnimation.start();//開始動畫break;case R.id.stop:attackAnimation.stop();//停止動畫break;}}}
AnimationDrawable類是:
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.
The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call start()
to
run the animation.
An AnimationDrawable defined in XML consists of a single <animation-list>
element, and a series of nested <item>
tags. Each item defines a frame
of the animation.
一個對象用於建立frame-by-frame動畫,其定義是由一系列的可畫的對象,可以使用作為一個視圖對象的背景情況。
最簡單的方法來建立一個frame-by-frame動畫定義動畫在XML檔案中,放置在雜誌/可畫/檔案夾,並將它設定為背景,一個視圖對象。然後,叫開始()啟動並執行動畫。
定義一個AnimationDrawable在XML組成一個單一的< animation-list >元素,一系列的嵌套的<項目>標籤。 每一項定義了一個幀動畫