android 帧动画重叠,Android动画之帧动画详解

一.概念

帧动画也叫Drawable Animation,是最简单最直观的动画类型,他利用人眼的视觉暂留效应——也就是光对视网膜所产生的视觉在光停止作用后,仍会保留一段时间的现象。在Android中实现帧动画,就是由设计师给出一系列状态不断变化的图片,开发者可以指定动画中每一帧对应的图片和持续时间,然后就可以开始播放动画了。具体有两种方式可以实现帧动画,分别是采用XML资源文件和代码实现。

二.实现

◆XML资源文件方式

1.在res/drawable目录中放入需要的图片

d37d9ff9c502?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

2.在res/drawable目录中新建animlist.xml文件,其中oneshot表示是否循环播放false为循环播放,duration表示图片停留的时间。

android:duration="120"/>

android:duration="120"/>

android:duration="120"/>

android:duration="120"/>

android:duration="120"/>

android:duration="120"/>

android:duration="120"/>

3.在布局文件中进行设置

android:id="@+id/image"

android:background="@drawable/animlist"

android:layout_width="wrap_content"

android:layout_centerInParent="true"

android:layout_height="wrap_content" />

4.代码中播放动画

AnimationDrawable animationDrawable= (AnimationDrawable) imageView.getBackground();

//开始动画

animationDrawable.start();

//结束动画

animationDrawable.stop();

◆代码动态实现

AnimationDrawable animationDrawable=new AnimationDrawable();

for(int x=1;x<8;x++){

int id=getResources().getIdentifier("a"+x,"drawable",getPackageName());

Drawable drawable=getResources().getDrawable(id);

animationDrawable.addFrame(drawable,120);

}

//设置是否循环播放

animationDrawable.setOneShot(false);

imageView.setBackgroundDrawable(animationDrawable);

//开始动画

animationDrawable.start();

//结束动画

animationDrawable.stop();

效果展示

d37d9ff9c502?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值