2.帧动画:顺序播放一组预先设定好的图片,类似与电影的播放。系统提供AnimationDrawable类来使用帧动画
先创建帧动画的xml布局,路径为res/drawable/anim_frame_test
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android1="http://schemas.android.com/apk/res/android"
android1:oneshot="false"
>
<item
android1:drawable="@drawable/emoji1"
android1:duration="200"
/>
<item
android1:drawable="@drawable/emoji2"
android1:duration="200"
/>
<item
android1:drawable="@drawable/emoji3"
android1:duration="200"
/>
<item
android1:drawable="@drawable/emoji4"
android1:duration="200"
/>
<item
android1:drawable="@drawable/emoji5"
android1:duration="200"
/>
<item
android1:drawable="@drawable/emoji6"
android1:duration="200"
/>
</animation-list>
设置动画
........
//帧动画
mAnimationDrawable = new AnimationDrawable();
mAnimationDrawable.setOneShot(false);
for(int i = 1; i<7; i++){
int id = getResources().getIdentifier("emoji" + i, "drawable", getPackageName());
Drawable drawable = ContextCompat.getDrawable(this, id);
mAnimationDrawable.addFrame(drawable, 200);
}
testImage.setImageDrawable(mAnimationDrawable);
startAnim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAnimationDrawable.start();
}
});
stopAnim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAnimationDrawable.stop();
}
});
动画效果:
至此就实现了帧动画的完整过程