逐帧动画其实跟动画片的原理是一样的,一段时间内连续播放一组图片,使之看起来像在动一样
下面是一个例子,不用专门说明就可以理解了
定义文件的资源名为girl,放在res->anmi目录下,下面有一个重要的参数 "
android:oneshot="false"如果为true,动画将不会循环播放
<?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/one" android:duration = "60" /> <item android:drawable="@drawable/two" android:duration = "60" /> <item android:drawable="@drawable/three" android:duration = "60" /> <item android:drawable="@drawable/four" android:duration = "60" /> <item android:drawable="@drawable/five" android:duration = "60" /> <item android:drawable="@drawable/six" android:duration = "60" /> </animation-list>
<?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" android:background="#fff"> - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" /> </LinearLayout> <ImageView android:id="@+id/anim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/girl" android:scaleType="center" /> </LinearLayout>
package WangLi.Graphics.AnimationFrame; 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 AnimationFrame extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取两个按钮 Button play = (Button)findViewById(R.id.play); Button stop = (Button)findViewById(R.id.stop); ImageView imageView = (ImageView)findViewById(R.id.anim); //获取AnimationDrawable动画对象 final AnimationDrawable anim = (AnimationDrawable)imageView.getBackground(); play.setOnClickListener(new OnClickListener(){ public void onClick(View v) { anim.start(); } }); stop.setOnClickListener(new OnClickListener(){ public void onClick(View v) { anim.stop(); } }); } }