如何将帧动画添加到包含按钮状态更改处理的 ImageButton?

How to add Frame Animation to an ImageButton including Button State change handling?

我想将动画列表添加到选择器 xml 文件,该文件又被添加到 ImageButton。在默认状态下,我希望播放动画,在按下状态下,我希望显示其他图像。我的代码适用于按钮状态更改,但动画在默认状态下不起作用。 其实我在看Android

anim_virus.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/attackvirus0" android:duration="50" />
    <item android:drawable="@drawable/attackvirus1" android:duration="50" />
    <item android:drawable="@drawable/attackvirus2" android:duration="50" />
    <item android:drawable="@drawable/attackvirus3" android:duration="50" />
    <item android:drawable="@drawable/attackvirus4" android:duration="50" />
    <item android:drawable="@drawable/attackvirus5" android:duration="50" />
    <item android:drawable="@drawable/attackvirus4" android:duration="50" />
    <item android:drawable="@drawable/attackvirus3" android:duration="50" />
    <item android:drawable="@drawable/attackvirus2" android:duration="50" />
    <item android:drawable="@drawable/attackvirus1" android:duration="50" />
</animation-list>

button_state_virus.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/attackviruspress"/>
    <item android:state_focused="true" android:drawable="@drawable/attackvirusfocus"/>
    <item android:drawable="@drawable/anim_virus" android:state_enabled="true"/>
</selector>

而我的图片按钮标签及其参数是:

<ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="#00000000"
      android:layout_margin="10dp"
      android:soundEffectsEnabled="true"
      android:src="@drawable/button_state_virus"
      android:id="@+id/button_infect"
      android:contentDescription="@string/content_desc_virus"/>

对于 drawable 背景动画,您可以执行以下操作:

ImageButton imageButton = (ImageButton) findViewById(R.id.button_infect);
imageButton.setBackgroundResource(R.drawable.anim_virus);

如果你想.start()动画,这样做:

((AnimationDrawable) imageButton.getBackground()).start();

如果你想.stop()它,这样做:

((AnimationDrawable) imageButton.getBackground()).stop();

但是,要从您的 ImageButton 获得 StateListDrawable 动画,您已将 StateListDrawable 设置为 XML 中的 android:background 属性。

StateListDrawable background = (StateListDrawable) imageButton.getBackground();
Drawable current = background.getCurrent();

if (current instanceof AnimationDrawable) {
  imageButton = (AnimationDrawable) current;
  btnAnimation.start();
}

因此此代码从 ImageButton 获取背景,然后根据您所在的状态将背景定义为 drawable。如果当前状态是 instanceof AnimationDrawable 即你的 animation-list 然后它会播放动画。

编辑:修改了评论中的错误

E/AndroidRuntime: FATAL EXCEPTION: main Process: java.lang.RuntimeException: Unable to start activity ComponentInfo{AttackPlanetActivity}: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable at android.app.ActivityThread.performLaunchActivity

编辑致谢:Button states with Background as AnimationDrawable in Android