阅读量:102
要在Android项目中创建AnimationDrawable,可以按照以下步骤进行:
-
在res/drawable文件夹中创建一个xml文件,用于定义动画帧序列。例如,创建一个名为animation.xml的文件。
-
在该xml文件中,定义一个
标签,并在其中添加 - 标签来定义每一帧的图片资源和持续时间。示例代码如下:
- 标签来定义每一帧的图片资源和持续时间。示例代码如下:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/frame1"
android:duration="100"/>
<item
android:drawable="@drawable/frame2"
android:duration="100"/>
<item
android:drawable="@drawable/frame3"
android:duration="100"/>
</animation-list>
- 在activity中使用AnimationDrawable类来加载并播放动画。示例代码如下:
ImageView imageView = findViewById(R.id.imageView);
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation);
imageView.setBackground(animationDrawable);
animationDrawable.start();
- 在需要的时候可以停止动画,示例代码如下:
animationDrawable.stop();
通过以上步骤,就可以在Android项目中创建并播放AnimationDrawable动画了。