要在Android中使用AnimationDrawable,首先需要在res/drawable文件夹中创建一个animation-list资源文件,用于定义动画帧。然后在activity中使用AnimationDrawable类来加载和控制动画。
以下是一个简单的示例代码:
在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>在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来实现帧动画效果。


