绘图动画(Drawable Animation)

来源:互联网 发布:linux svn checkout 编辑:程序博客网 时间:2024/05/22 12:32
一、什么叫绘图动画?
加载一系列Drawable资源来创建动画,这种传统动画某种程度上就是创建不同图片序列,顺序播放,就像电影胶片。在代码中定义动画帧,使用AnimationDrawable类;XML文件能更简单的组成动画帧,在res/drawable文件夹,使用<animation-list>采用<item>来定义不同的帧。感觉只能设置的属性是动画间隔时间。

二、如何在 XML 文件中定义动画 
ad.xml
<?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/a" android:duration="100" />   
  <item android:drawable="@drawable/b" android:duration="100" />   
  <item android:drawable="@drawable/c" android:duration="100" />   
  <item android:drawable="@drawable/d" android:duration="100" />   
  <item android:drawable="@drawable/e" android:duration="100" />   
  <item android:drawable="@drawable/f" android:duration="100" />   
  <item android:drawable="@drawable/g" android:duration="100" />   
  <item android:drawable="@drawable/h" android:duration="100" />   
  <item android:drawable="@drawable/i" android:duration="100" />   
  <item android:drawable="@drawable/j" android:duration="100" />   

</animation-list>
说明:
android:oneshot="false"
             默认为false 表示 动画循环播放, 如果这里写true 则表示动画只播发一次。
 <item> 
            标签中记录着每一帧的信息android:drawable="@drawable/a"表示这一帧用的图片为"a" android:duration="100"    
             表示这一帧持续100毫秒,可以根据这个值来调节动画播放的速度。

Layout文件中
<ImageView  
   android:id="@+id/imageView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
 />  
代码中:
//获取AnimationDrawable 
 AnimationDrawable ad = (AnimationDrawable) getResources().getDrawable(R.drawable.ad);
//添加背景动画
  imageView.setBackgroundDrawable(ad);
//启动绘图动画
  ad.start();

AnimationDrawable 提供了很多方法来控制这个帧动画。

animationDrawable.start();
开始这个动画 
animationDrawable.stop(); 
结束这个动画 
animationDrawable.setAlpha(100);
设置动画的透明度, 取值范围(0 - 255) 
animationDrawable.setOneShot(true); 
设置单次播放 
animationDrawable.setOneShot(false);
设置循环播放 
animationDrawable.isRunning(); 
 判断动画是否正在播放 
animationDrawable.getNumberOfFrames();
得到动画的帧数。


二、如何在 JAVA代码中定义动画 

代码如下
 imageView = (ImageView) findViewById(R.id.iv);

   //实例化AnimationDrawable对象
  AnimationDrawable ad = new AnimationDrawable();
 
  Drawable a = getResources().getDrawable(R.drawable.a);
  Drawable b = getResources().getDrawable(R.drawable.b);
  Drawable c = getResources().getDrawable(R.drawable.c);
  Drawable d = getResources().getDrawable(R.drawable.d);
  Drawable e = getResources().getDrawable(R.drawable.e);
  Drawable f = getResources().getDrawable(R.drawable.f);
  Drawable g = getResources().getDrawable(R.drawable.g);
  Drawable h = getResources().getDrawable(R.drawable.h);
 
  //为动画添加一帧  
  //参数mBitAnimation是该帧的图片  
    //参数500是该帧显示的时间,按毫秒计算   
  ad.addFrame(a, 200);
  ad.addFrame(b, 200);
  ad.addFrame(c, 200);
  ad.addFrame(d, 200);
  ad.addFrame(e, 200);
  ad.addFrame(f, 200);
  ad.addFrame(g, 200);
  ad.addFrame(h, 200);
  //设置播放模式是否循环播放,false表示循环,true表示不循环
  ad.setOneShot(false);
  //设置本类将要显示的这个动画  
  imageView.setBackground(ad);
  ad.start();



0 0