Android动画之逐帧动画FrameAnimation

版权声明:本文为博主原创文章,转载希望能注明出处,感谢。 https://blog.csdn.net/u010126792/article/details/85318835

1 逐帧动画介绍

视图动画由两部分组成,补间动画和逐帧动画,前面文章已经讲解了补间动画,下面讲解逐帧动画。
Frame-by-frame Animation主要作用于view,可以利用xml或者代码生成动画,如果使用xml方式生成动画需要在res/drawable 目录下创建动画xml文件(animation-list)。
逐帧动画的原理是一张一张的播放图片资源(drawable资源),然后出现动画效果。

逐帧动画对应的类是AnimationDrawable,在android.graphics.drawable.Drawable包名下。

把逐帧动画作为view的背景,然后获取动画,开启动画。

构造函数
AnimationDrawable()

属性说明:
在这里插入图片描述

  • oneshot:是否只播放一次,取值true,false,默认为false,用于animation-list
  • duration:每个item(每一帧动画)播放时长
  • drawable: 每一帧的drawable资源
  • visible:drawable资源是否可见,默认不可见

AnimationDrawable的主要函数:

  • addFrame(Drawable frame, int duration)添加drawable
    frame:每一帧的图片资源
    duration:每一帧的持续动画
  • start():开始动画
  • stop(): 结束动画
  • isRunning():是否正在执行
  • setOneShot(boolean oneShot):设置是否只播放一次
  • getNumberOfFrames() :获取帧的动画数

2 使用方式

2.1xml使用方式
首先定义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.2代码使用方式
animationDrawable = new AnimationDrawable();
// 为AnimationDrawable添加动画帧
animationDrawable .addFrame(getResources().getDrawable(R.drawable.d1), 50);
imageView.setBackground(animationDrawable );

3 代码示例

3.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>

3.2 代码获取设置

imageView = findViewById(R.id.imageview);
imageView.setImageResource(R.drawable.animationlist);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();

在这里插入图片描述

代码实现方式
和上面xml实现动画相同,就不在贴gif动画

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();

注意:
内部使用图片作为资源,所以如果图片资源过大可能造成OOM,虽然简单,但是慎用。

猜你喜欢

转载自blog.csdn.net/u010126792/article/details/85318835