Android中AnimationDrawable的使用

首先,可以在drawable文件夹下定义一个xml的文件。如下所示:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/compass_1" android:duration="70" />
    <item android:drawable="@drawable/compass_2" android:duration="70" />
    <item android:drawable="@drawable/compass_3" android:duration="70" />
    <item android:drawable="@drawable/compass_4" android:duration="70" />
    <item android:drawable="@drawable/compass_5" android:duration="70" />
</animation-list>

根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画。其中,每一个item是一帧,android:duration="400"表示每帧持续400ms,android:drawable是每帧要显示的图片。
接下来,代码中使用: 

AnimationDrawable ad = (AnimationDrawable) getResources().getDrawable(R.drawable.bootanimation);

得到AnimationDrawable对象。再将该对象设为当前view的backgroud。

mView.setBackgroundDrawable(ad);

接着,就可以调用 ad.start()方法和ad.stop()方法来启动和停止该动画。
注意:必须设为当前view的background,否则start和stop该动画是没有效果的。

转载于:https://www.cnblogs.com/lovejoy/p/3372279.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值