1、res下创建anim文件夹,在下面创建animation-list的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/firefox_animation_0"
android:duration="500" />
<item android:drawable="@drawable/firefox_animation_1"
android:duration="500" />
</animation-list>
xml文件名可以自用定义;根元素是animation-list,要有多个item元素。
2、在main.xml中创建ImageView组件,并且设置Background
<ImageView android:layout_width="wrap_content"
android:background="@anim/firefox_animation"
android:layout_height="wrap_content" android:id="@+id/ImageView01" />
3、在Java代码中获取ImageView对象,然后ImageView对象获取AnimationDrawable
AnimationDrawable ad = (AnimationDrawable) imgView.getBackground();
4、AnimationDrawable对象启动或停止。
ad.start();
ad.stop();
Android 逐帧动画的小例子
最新推荐文章于 2022-07-02 11:20:32 发布
本文介绍如何在Android应用中创建和使用AnimationDrawable实现简单的动画效果。通过在res/anim目录下定义XML动画列表文件,并在布局文件中为ImageView设置背景,最后在Java代码中控制动画播放与停止。
843

被折叠的 条评论
为什么被折叠?



