アニメーションリストをセレクタxmlファイルに追加し、これをImageButtonに追加します。デフォルト状態では、アニメーションを再生し、押した状態で、他の画像を表示したい。私のコードはボタンの状態の変更のために働いていますが、アニメーションはデフォルト状態では機能しませんでした。 実際に私は、AndroidButton State change handlingを含む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プロセス:java.lang.RuntimeException:アクティビティを開始できませんでしたアプリケーションのクラッシュにつながるComponentInfo {AttackPlanetActivity}:java.lang.ClassCastException:android.graphics.drawable.StateListDrawableできませんandroid.app.ActivityThread.performLaunchActivity 'にandroid.graphics.drawable.AnimationDrawableにキャストしてください。 anim_virusをボタンに設定してandroid:srcパラメータを削除した場合、AnimationDrawable start()を使用してアニメーションを見ることができます。 –
新しい編集芽を確認します。 –