Android帧动画就是把一组连续的图片按照一定的顺序给它播放出来动画效果:
一、首先先给出一组连续的图片,这里就不给了,大家自己找,然后把图片放在项目对应的drawable文件下或者mipmap文件夹下看你自己。
二、然后在drawable文件下新建一个xml文件,这里命名为granule_list_anim.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="40" android:drawable="@mipmap/rectangle_025"/>
<item android:duration="40" android:drawable="@mipmap/rectangle_026"/>
<item android:duration="40" android:drawable="@mipmap/rectangle_027"/>
<item android:duration="40" android:drawable="@mipmap/rectangle_028"/>
<item android:duration="40" android:drawable="@mipmap/rectangle_029"/>
<item android:duration="40" android:drawable="@mipmap/rectangle_030"/>
<item android:duration="40" android:drawable="@mipmap/rectangle_031"/>
<item android:duration="40" android:drawable="@mipmap/rectangle_032"/>
<item android:duration="40" android:drawable="@mipmap/rectangle_033"/>
<item android:duration="40" android:drawable="@mipmap/rectangle_034"/>
</animation-list>
这里简单说一下,帧动画是一组图片,所以外层使用 animation-list 标签,
每个item 中的 duration 为每一帧图片的持续时间,这里给40 单位为毫秒,具体时间根据你的需求以及效果定,后面的drawable 里面就是你的图片了,记得按顺序写,
还有一个重要的属性; android:oneshot="false" 这个是在 animation-list 标签里面的,注意了,他有两个值,一个true 还有一个false ,true 代表不循环播放,false代表帧动画结束之后,继续重头开始循环播放,我开始看别的写的这么循环播放的时候,很多人介绍都不一样,有的说true是循环,有的说是false是循环把我搞糊涂了,这里亲自实践,false是循环,,是否循环仅限本篇帧动画写法。一会还有另一种实现方法,比这种要节省内存。但是我没搞出来怎么循环,这里暂且先不说。
三、开始写代码:
定义一个 AnimationDrawable 对象,然后使用ImageView获取他的Drawable
AnimationDrawable animationDrawable;
ImageView boxImg;//记得先findViewById,或者BindView都行,
// granule_list_anim 就是上面定义的drawable xml文件
boxImg.setImageResource(R.drawable.granule_list_anim);
animationDrawable = (AnimationDrawable) boxImg.getDrawable();
//接着调用start即可实现帧动画的循环播放
animationDrawable.start();
//animationDrawable 还有其他的方法,根据需要自己添加
好了,这个循环播放的或者不循环播放就实现了。下一篇给个比这个更省内存的帧动画。