我想将动画列表添加到选择器xml文件,然后将其添加到ImageButton中。在默认状态下,我想要播放动画并按下状态,我想要显示其他图像。我的代码正在为按钮状态更改工作,但动画在默认状态下不起作用。 其实我读的Android如何将帧动画添加到ImageButton,包括按钮状态更改处理?
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"/>
我曾尝试使用AnimationDrawable,就像你提到的。但它会导致应用程序崩溃,错误描述为'E/AndroidRuntime:FATAL EXCEPTION:main Process:java.lang.RuntimeException:无法启动活动ComponentInfo {AttackPlanetActivity}:java.lang.ClassCastException:android.graphics.drawable.StateListDrawable can not在android.app.ActivityThread.performLaunchActivity'上投射到android.graphics.drawable.AnimationDrawable。如果我将anim_virus设置为按钮并删除android:src参数,则可以使用AnimationDrawable start() –
检查新的编辑预览。 –