1

アニメーションリストをセレクタ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"/> 

答えて

0

だけ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(); 

しかし、からStateListDrawable動画を取得するには、XMLのandroid:background属性としてを入力します。

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

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

ので、このコードはImageButtonから背景を取得し、それはあなたがしている状態に応じてdrawableような背景を定義します。つまり、現在の状態があなたのanimation-listすなわちinstanceof AnimationDrawableであれば、それが再生されますアニメーション。

編集:致命的な例外:メインプロセス:コメント

E/AndroidRuntimeにエラーから変更java.lang.RuntimeException:java.lang.ClassCastExceptionが:活性ComponentInfo {AttackPlanetActivity}を開始できません。 Button states with Background as AnimationDrawable in Android

:android.graphics.drawable.StateListDrawableは、編集のためにクレジット

android.app.ActivityThread.performLaunchActivity

でandroid.graphics.drawable.AnimationDrawableにキャストすることはできません
+0

あなたが言及したように私は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()を使用してアニメーションを見ることができます。 –

+0

新しい編集芽を確認します。 –

関連する問題