1

我想将动画列表添加到选择器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"/> 

回答

0

只是一drawable背景动画,你可以这样做:

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

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

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

,这样做:

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

但是,要从ImageButton获得StateListDrawable动画,您需要设置StateListDrawable作为您在0123中的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改变了java.lang.RuntimeException:无法启动活动ComponentInfo {AttackPlanetActivity}:java.lang.ClassCastException: android.graphics.drawable.StateListDrawable不能转换为android.graphics.drawable.AnimationDrawable在android.app.ActivityThread.performLaunchActivity

信用为编辑:Button states with Background as AnimationDrawable in Android

+0

我曾尝试使用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() –

+0

检查新的编辑预览。 –