逐帧动画 AnimationDrawable

逐帧动画是通过android中的android.graphics.drawable.AnimationDrawable类来实现的

在该类中保存了帧序列以及显示的时间,为了简化动画的创建OPhone提供了一种通过XML来创建逐帧动画的方式,这样把动画的创建和代码分来以后如果需要修改动画内容,只需要修改资源文件就可以了不用修改代码,简化开发维护工作.

 

实现步骤:

1:首先在res/drawable/下新建一个picture_animation.xml文件,文件内容为:


    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:oneshot="false">  

   
     
     
     
     
     
     

 

2:在res/loyout/下新建一个animation_drawable.xml文件,文件内容为:


  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">
 


    android:id="@+id/imageview1"
  android:layout_width="100px"
  android:layout_height="100px"
  android:src="@drawable/picture_animation"
  >
 
 
    android:id="@+id/start"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Start"
  >
 
 
    android:id="@+id/start_once"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Start_once"
  >
 

 

3:新建一个Activity-->Animation1

  内容为:

public class Animation1 extends Activity {

 private AnimationDrawable draw = null;
 private Button start = null;
 private Button startOnce = null;
 private boolean isoneshot = true;
 
 public void onCreate(Bundle bundle)
 {
  super.onCreate(bundle);
  this.setContentView(R.layout.animation_drawable);
  ImageView view = (ImageView)findViewById(R.id.imageview1);
  draw = (AnimationDrawable)view.getDrawable();
  
  start = (Button)findViewById(R.id.start);
  start.setOnClickListener(new OnClickListener()
  {

   public void onClick(View v) {
    // TODO Auto-generated method stub
    startAnimation();
   }
   
  });
  
  startOnce = (Button)findViewById(R.id.start_once);
  startOnce.setOnClickListener(new OnClickListener()
  {

   public void onClick(View v) {
    // TODO Auto-generated method stub
    if(isoneshot)
    {
     startOnce.setText("startOnce");
    }
    else
    {
     startOnce.setText("Play Repace");
    }
    draw.setOneShot(isoneshot);
    isoneshot = !isoneshot;
   }
   
  });
 }
 
 private void startAnimation()
 {
  if(draw.isRunning())
  {
   draw.stop();
  }
  else
  {
   draw.stop();
   draw.start();
  }
 }
}

 

你可能感兴趣的:(逐帧动画 AnimationDrawable)