Android怎样播放多张图片形成一个动画


在Android里可以逐帧的播放图片,然后产生一种动态的效果,准备好几张连续的图片,然后在于源程序res文件夹下建立anim文件夹,然后新建一个XML

XML代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"   
  3. android:oneshot="true">   
  4. <item android:drawable="@drawable/c1" android:duration="200" />   
  5. <item android:drawable="@drawable/c2" android:duration="200" />   
  6. <item android:drawable="@drawable/c3" android:duration="200" />  
  7. <item android:drawable="@drawable/c4" android:duration="200" />  
  8. <item android:drawable="@drawable/c5" android:duration="200" />  
  9. <item android:drawable="@drawable/c6" android:duration="200" />  
  10. </animation-list>  

其中c1,c2,c3,c4,c5,c6是加入的图片的名称。

在窗体里面放置一个ImageView控件,并在代码中编写:

_imageView1 =(ImageView)findViewById(R.id.imageView1);//放置的ImageView控件

  1. //设置动画背景   
  2. _imageView1.setBackgroundResource(R.anim.animation_list);//其中R.anim.animation_list就是上一步准备的动画描述文件的资源名   
  3. //获得动画对象   
  4. _animaition = (AnimationDrawable)_imageView1.getBackground();  
  5. 最后,就可以启动动画了,代码如下:  
  6. //是否仅仅启动一次?   
  7. _animaition.setOneShot(false);  
  8. if(_animaition.isRunning())//是否正在运行?   
  9. {  
  10. _animaition.stop();//停止   
  11. }  
  12. _animaition.start();//启动  

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容