帧动画就是图片的循环播放
实现帧动画有两种方式:
1.通过代码实现:
//帧动画对象
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.one), 100);//加载个帧 参数1为drawable,参数2为持续时间animation.addFrame(getResources().getDrawable(R.drawable.two), 100);animation.addFrame(getResources().getDrawable(R.drawable.three), 100);animation.addFrame(getResources().getDrawable(R.drawable.four), 100);animation.addFrame(getResources().getDrawable(R.drawable.five), 100);animation.addFrame(getResources().getDrawable(R.drawable.six), 100);animation.addFrame(getResources().getDrawable(R.drawable.seven), 100);animation.addFrame(getResources().getDrawable(R.drawable.eight), 100);animation.addFrame(getResources().getDrawable(R.drawable.nine), 100);animation.addFrame(getResources().getDrawable(R.drawable.ten), 100);animation.addFrame(getResources().getDrawable(R.drawable.eleven), 100);animation.addFrame(getResources().getDrawable(R.drawable.twelve), 100);animation.addFrame(getResources().getDrawable(R.drawable.thirteen), 100);animation.addFrame(getResources().getDrawable(R.drawable.fourteen), 100);animation.addFrame(getResources().getDrawable(R.drawable.fifteen), 100);
//将帧动画加载到imageview上
mImg.setBackground(animation);
//帧动画是否循环播放 为false时循环播放,为true时不循环播放
animation.setOneShot(false);
//启动帧动画animation.start();
2.通过xml形式:
首先在drawable文件夹下新建一个文件名字为frame.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/one" android:duration="100" />
<item android:drawable="@drawable/two" android:duration="100" />
<item android:drawable="@drawable/three" android:duration="100" />
<item android:drawable="@drawable/four" android:duration="100" />
<item android:drawable="@drawable/five" android:duration="100" />
<item android:drawable="@drawable/six" android:duration="100" />
<item android:drawable="@drawable/seven" android:duration="100" />
<item android:drawable="@drawable/eight" android:duration="100" />
<item android:drawable="@drawable/nine" android:duration="100" />
<item android:drawable="@drawable/ten" android:duration="100" />
<item android:drawable="@drawable/eleven" android:duration="100" />
<item android:drawable="@drawable/twelve" android:duration="100" />
<item android:drawable="@drawable/thirteen" android:duration="100" />
<item android:drawable="@drawable/fourteen" android:duration="100" />
<item android:drawable="@drawable/fifteen" android:duration="100" />
</animation-list>
然后通过代码引用:
AnimationDrawable animatin = (AnimationDrawable) getResources().getDrawable(R.drawable.frame);
mImg.setBackground(animatin);
animatin.start()
最终实现效果
//两种方式都是一样的效果