Android帧动画FrameAnimation

一、逐帧动画介绍

视图动画由两部分组成,补间动画和逐帧动画,下面讲解逐帧动画。
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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网小熊猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值