要设置AnimationDrawable的动画,首先需要在res/drawable文件夹下创建一个XML文件来定义动画帧序列。例如,创建一个名为animation.xml的文件,定义如下:
<animation-list android:id="@+id/selected" android:oneshot="false"> <item android:drawable="@drawable/frame1" android:duration="50" /> <item android:drawable="@drawable/frame2" android:duration="50" /> <item android:drawable="@drawable/frame3" android:duration="50" /> <!-- 添加更多的动画帧 --></animation-list>然后在代码中加载AnimationDrawable并设置到ImageView中:
// 加载AnimationDrawableImageView imageView = findViewById(R.id.imageView);AnimationDrawable animation = (AnimationDrawable) getResources().getDrawable(R.drawable.animation);// 将AnimationDrawable设置到ImageView中imageView.setBackground(animation);// 开始动画animation.start();这样就可以在ImageView中显示AnimationDrawable的动画了。


