逐帧动画 AnimationDrawable

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

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

 

实现步骤:

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

<?xml version="1.0" encoding="utf-8"?>

<!-- 动画帧集合对象-->
<animation-list      
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:oneshot="false">  

<!--动画帧对象 android:duration表示每帧动画显示的时间,放在drawable下的动画图片不能太大,否则会内存爆掉-->   
<item android:drawable="@drawable/photo1" android:duration="80"/>     
<item android:drawable="@drawable/photo2" android:duration="80"/>     
<item android:drawable="@drawable/photo3" android:duration="80"/>     
<item android:drawable="@drawable/photo4" android:duration="80"/>     
<item android:drawable="@drawable/photo5" android:duration="80"/>     
<item android:drawable="@drawable/photo6" android:duration="80"/>     
</animation-list>

 

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">
 

<!--src是指引用动画帧集合来控制动画-->
  <ImageView
  android:id="@+id/imageview1"
  android:layout_width="100px"
  android:layout_height="100px"
  android:src="@drawable/picture_animation"
  >
  </ImageView>
 
  <Button 
  android:id="@+id/start"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Start"
  >
  </Button>
 
  <Button
  android:id="@+id/start_once"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Start_once"
  >
  </Button>
</LinearLayout>

 

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();
  }
 }
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值