Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中背景为AnimationDrawable的按钮状态_Android_Animation_Button - Fatal编程技术网

Android中背景为AnimationDrawable的按钮状态

Android中背景为AnimationDrawable的按钮状态,android,animation,button,Android,Animation,Button,我在安卓系统中定制按钮已有一段时间了。事情很简单,只是为按钮状态创建了图像资源,并为其创建了一个选择器。一切都很顺利。现在我遇到了一个新情况。 我制作了一个可绘制的动画,并将其设置为按钮的背景 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/frame1"

我在安卓系统中定制按钮已有一段时间了。事情很简单,只是为按钮状态创建了图像资源,并为其创建了一个选择器。一切都很顺利。现在我遇到了一个新情况。 我制作了一个可绘制的动画,并将其设置为按钮的背景

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
  <item android:drawable="@drawable/frame1" android:duration="600" /> 
  <item android:drawable="@drawable/frame2" android:duration="300" /> 
  <item android:drawable="@drawable/frame3" android:duration="500" /> 
</animation-list> 
这似乎是问题所在,因为我的动画无法从选择器中正确获取,并且出现以下错误:

03-14 15:21:16.146: ERROR/AndroidRuntime(440): FATAL EXCEPTION: main
03-14 15:21:16.146: ERROR/AndroidRuntime(440): java.lang.ClassCastException: android.graphics.drawable.StateListDrawable
03-14 15:21:16.146: ERROR/AndroidRuntime(440):     at com.bebenjoy.MainActivity.onWindowFocusChanged(MainActivity.java:53)
03-14 15:21:16.146: ERROR/AndroidRuntime(440):     at ...

你知道怎么解决这个问题吗?谢谢。

您的演员阵容不正确--您的背景可绘制为
StateListDrawable
,而不是
AnimationDrawable
。我宁愿做这样的事情:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  btn = (Button)findViewById(R.id.btnAnim);
  StateListDrawable background = (StateListDrawable) btn.getBackground();
  Drawable current = background.getCurrent();
  if (current instanceof AnimationDrawable) {
      btnAnimation = (AnimationDrawable) current;
      btnAnimation.start();
  }
}

我知道我的回答有点晚了,但我面临着同样的问题。我检查了很多解决方案,但只找到一个。 我尝试在onWindowFocusChanged()中启动动画,在单独的线程中启动动画,但没有帮助

我使用
setVisible(布尔可见,布尔重启)解决了这个问题。

所以你可以试试这个:

    private Button ImgBtn;
    private AnimationDrawable btnAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.button1);
        StateListDrawable background = (StateListDrawable) btn.getBackground();
        btnAnimation = (AnimationDrawable) background.getCurrent();
        btnAnimation.setVisible(true, true); // it works even in onCreate()        
    }

希望这能对某人有所帮助:)

你能为我今天的主要活动提供完整的代码吗。非常感谢你的帮助。唯一对我有效的答案也是。谢谢。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  btn = (Button)findViewById(R.id.btnAnim);
  StateListDrawable background = (StateListDrawable) btn.getBackground();
  Drawable current = background.getCurrent();
  if (current instanceof AnimationDrawable) {
      btnAnimation = (AnimationDrawable) current;
      btnAnimation.start();
  }
}
    private Button ImgBtn;
    private AnimationDrawable btnAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.button1);
        StateListDrawable background = (StateListDrawable) btn.getBackground();
        btnAnimation = (AnimationDrawable) background.getCurrent();
        btnAnimation.setVisible(true, true); // it works even in onCreate()        
    }