逐帧动画就是将多张图片按顺序展示,从而产生一种动态的效果。
---------------
下面是我的代码演示
1.准备几张连续的图片,编写动画描述文件(在anim资源文件夹下新建一个XML)。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/c1" android:duration="200" />
<item android:drawable="@drawable/c2" android:duration="200" />
<item android:drawable="@drawable/c3" android:duration="200" />
<item android:drawable="@drawable/c4" android:duration="200" />
<item android:drawable="@drawable/c5" android:duration="200" />
<item android:drawable="@drawable/c6" android:duration="200" />
</animation-list>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/c1" android:duration="200" />
<item android:drawable="@drawable/c2" android:duration="200" />
<item android:drawable="@drawable/c3" android:duration="200" />
<item android:drawable="@drawable/c4" android:duration="200" />
<item android:drawable="@drawable/c5" android:duration="200" />
<item android:drawable="@drawable/c6" android:duration="200" />
</animation-list>
2.在窗体里放置一个ImageView 控件,并在代码里编写
_imageView1 = (ImageView)findViewById(R.id.imageView1);//放置的ImageView 控件
//设置动画背景
_imageView1.setBackgroundResource(R.anim.animation_list); //其中R.anim.animation_list就是上一步准备的动画描述文件的资源名
//设置动画背景
_imageView1.setBackgroundResource(R.anim.animation_list); //其中R.anim.animation_list就是上一步准备的动画描述文件的资源名
//获得动画对象
_animaition = (AnimationDrawable) _imageView1.getBackground();
_animaition = (AnimationDrawable) _imageView1.getBackground();
3.启动动画
//是否仅仅启动一次?
_animaition.setOneShot(false);
if(_animaition.isRunning())//是否正在运行?
{
_animaition.stop();//停止
}
_animaition.start();//启动
_animaition.setOneShot(false);
if(_animaition.isRunning())//是否正在运行?
{
_animaition.stop();//停止
}
_animaition.start();//启动