android

Android中如何使用AnimationDrawable

小樊
84
2024-08-15 09:43:39
栏目: 编程语言

要在Android中使用AnimationDrawable,首先需要在res/drawable文件夹中创建一个animation-list资源文件,用于定义动画帧。然后在activity中使用AnimationDrawable类来加载和控制动画。

以下是一个简单的示例代码:

  1. 在res/drawable文件夹中创建一个animation-list资源文件,比如名为animation.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>
  1. 在activity中加载并控制动画:
ImageView imageView = findViewById(R.id.imageView);
AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation);
imageView.setBackground(animationDrawable);

// 开始动画
animationDrawable.start();

// 停止动画
animationDrawable.stop();

// 判断动画是否在运行中
boolean isRunning = animationDrawable.isRunning();

这样就可以在Android应用中使用AnimationDrawable来实现帧动画效果。

0
看了该问题的人还看了