1、布局文件中添加ImageView
<ImageView
android:id="@+id/iv_fan" android:layout_width="wrap_content" android:layout_height="wrap_content"android:background="@drawable/f1" />//注意:如果不是手写的代码,即拖动进布局的ImageView需要修改android:src 改成android:background,否则无法播放!
2、res文件夹下添加anim文件夹,用来存放<animation-list>
在anim文件夹下创建一个名称为fan_list的xml文件
右键anim文件夹->new->Android XML File进入选择界面
Resource Type: 选择Drawable
Project: 自己的工程目录,通常情况下程序自己已经匹配好了,不用配置
File: xml文件的名称
Root Element: 选择Animation-list
选择Finish 完成创建
3、创建完成文件会出现在Drawable文件夹下,没关系,复制粘贴到anim文件夹下即可
编辑xml文件,创建好的xml文件通常只带有一个头文件和一个标签
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >//在此处填写数据
</animation-list>
填写数据的时候,android不会给出提示,所以需要记住
fan_list文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" ><item
android:drawable="@drawable/f1" android:duration="100"/> <item android:drawable="@drawable/f2" android:duration="100"/> <item android:drawable="@drawable/f3" android:duration="100"/> <item android:drawable="@drawable/f4" android:duration="100"/> <item android:drawable="@drawable/f5" android:duration="100"/> <item android:drawable="@drawable/f6" android:duration="100"/> <item android:drawable="@drawable/f7" android:duration="100"/> <item android:drawable="@drawable/f8" android:duration="100"/></animation-list>
其中android:drawable是需要播放的图片数据,程序播放的时候会从上到下依次播放
android:duration 持续时间,每张图片显示的时间,单位是ms
这两个属性必须填写,不然无法播放
4、在程序中引用
public void InitView(){
iv_fan = (ImageView) findViewById(R.id.imageView1);//实例化控件
iv_fan.setBackgroundResource(R.anim.fan_list);//设置播放数据 final AnimationDrawable anim = (AnimationDrawable) iv_fan//使用AnimationDrawable控制数据的播放和暂停 .getBackground(); iv_fan.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {anim.setOneShot(true);//设置只播放一次,false无限循环播放
anim.start();//播放}
});}