Android逐帧动画FrameAnimation

逐帧动画,就是使用多个静态图显示一个动态效果。

使用如下:

1.drawable下新建xxx.xml文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/idp" android:duration="300"/>
    <item android:drawable="@drawable/idq" android:duration="300"/>
    <item android:drawable="@drawable/idr" android:duration="300"/>
    <item android:drawable="@drawable/ids" android:duration="300"/>
    <item android:drawable="@drawable/idt" android:duration="300"/>
    <item android:drawable="@android:color/transparent" android:duration="300"/>
</animation-list>

oneshot: 帧动画是否只播放一次,默认为false。xxx.xml文件不设置该属性,可在代码中设置:anim.setOneShot(true);
duration: 每帧动画显示时长

注意:逐帧动画中的静态图片过多时,会报java.lang.OutOfMemoryError异常

2. 创建对象,开始动画

2.1

iv.setBackgroundResource(R.drawable.tip_anim);
AnimationDrawable anim = (AnimationDrawable)iv.getBackground();
anim.stop();//在动画start()之前要先stop(),不然在第一次动画之后会停在最后一帧,这样动画就只会触发一次
anim.start();

使用imageView的setBackgroundResource方法设置的资源背景,相当于布局文件中的android:background属性,
这个属性是view类的属性,必须通过getBackground()方法来获取。或者

2.2

iv.setImageResource(R.drawable.tip_anim);
AnimationDrawable anim = (AnimationDrawable) iv.getDrawable();
anim.start();

getDrawable()是ImageView类的方法, 必须通过在代码中setImageResource方法(对应布局文件中的android:src属性)或setImageDrawable(Drawable drawable)方法设置才可以使用getDrawable方法。

参考:

Android animation 动画背景图自动播放的实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值