一、逐帧动画介绍
视图动画由两部分组成,补间动画和逐帧动画,下面讲解逐帧动画。
Frame-by-frame Animation主要作用于view,可以利用xml或者代码生成动画,如果使用xml方式生成动画需要在res/drawable 目录下创建动画xml文件(animation-list)。
逐帧动画的原理是一张一张的播放图片资源(drawable资源),然后出现动画效果。
逐帧动画对应的类是AnimationDrawable,在android.graphics.drawable.Drawable包名下。
逐帧动画使用方式:把逐帧动画作为view的背景,然后获取动画,开启动画。
1.构造函数:
AnimationDrawable()
2.属性说明:
- oneshot:是否只播放一次,取值true,false,默认为false,用于animation-list
- duration:每个item(每一帧动画)播放时长
- drawable: 每一帧的drawable资源
- visible:drawable资源是否可见,默认不可见
3.AnimationDrawable的主要函数:
- addFrame(Drawable frame, int duration)添加drawable
- frame:每一帧的图片资源
- duration:每一帧的持续动画
- start():开始动画
- stop(): 结束动画
- isRunning():是否正在执行
- setOneShot(boolean oneShot):设置是否只播放一次
- getNumberOfFrames() :获取帧的动画数
二、使用方式
1.xml使用方式
首先定义animation-list 类型的drawable frameanimation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" >
// drawable= 图片资源;duration = 一帧持续时间(ms)
<item android:drawable="@drawable/d1" android:duration="1000"/>
<item android:drawable="@drawable/d2" android:duration="1000"/>
</animation-list>
设置动画资源的三种使用方式
第一种:
// 设置动画
imageView.setImageResource(R.drawable.frameanimation);
// 获取动画对象
animationDrawable = (AnimationDrawable)imageView.getDrawable();
animationDrawable.start();
第二种
设置背景:
imageView.setBackgroundResource(R.drawable.frameanimation);
animationDrawable = (AnimationDrawable) imageView.getBackground()
第三种
直接获取然后设置:
animationDrawable = (AnimationDrawable) getResources().getDrawable(
R.drawable.frameanimation);
imageView.setBackground(animationDrawable)
2代码使用方式
animationDrawable = new AnimationDrawable();
// 为AnimationDrawable添加动画帧
animationDrawable .addFrame(getResources().getDrawable(R.drawable.d1), 50);
imageView.setBackground(animationDrawable );
三、代码示例
1.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/compositedst1" android:duration="500"></item>
<item android:drawable="@drawable/compositedst2" android:duration="500"></item>
<item android:drawable="@drawable/compositedst3" android:duration="500"></item>
<item android:drawable="@drawable/compositedst4" android:duration="500"></item>
</animation-list>
imageView = findViewById(R.id.imageview);
imageView.setImageResource(R.drawable.animationlist);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();
2.代码实现方式
AnimationDrawable animationDrawable = new AnimationDrawable();
animationDrawable.setOneShot(false);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.compositedst1),1000);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.compositedst2),1000);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.compositedst3),1000);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.compositedst4),1000);
imageView.setImageDrawable(animationDrawable);
animationDrawable.start();