当前位置: 首页 > 新闻动态 > 软件编程

Android中AnimationDrawable使用的简单实例

作者:用户投稿 浏览: 发布日期:2026-01-16
[导读]:这篇文章介绍了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该动画是没有效果的。

免责声明:转载请注明出处:http://m.jing-feng.com.cn/news/216177.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!