按钮状态与背景AnimationDrawable Android中按钮、状态、背景、Android

2023-09-12 09:19:39 作者:菇凉我天生傲骨怎能服输i

我在Android的一段时间取得了自定义按钮。事情是为按钮状态简单,只是做图像资源,并提出了选择它。一切都进行得很顺利,真好。现在,我遇到了一个新的局面。 我已经做了动画绘制,并将其设置为背景为我的按钮。

I have made custom buttons in Android for a while. Things were simple, just made image resources for button states and made a selector for it. Everything went smooth and nice. Now I encountered a new situation. I have made a animation drawable and set it as a background for my button.

<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> 

如果我设置按钮的背景,它工作正常动画。如果我试图做一个简单的选择

If I set the animation as button's Background it works fine. If I try to make a simple selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
        android:state_pressed="false"
        android:drawable="@drawable/animation" />

    <item
        android:state_pressed="true"
        android:drawable="@drawable/pressed" />
  </selector>      

在这里的按钮正常状态下将有动画背景和pressed状态的静态图片,事情没有工作的权利。

where normal state of the button would have animation as background and the pressed state a static image, things don't work right.

在我的主要活动,在onWindowFocus我得到的按钮,背景和启动动画

On my main activity, on onWindowFocus I get the button background and start the animation

 @Override
  public void onWindowFocusChanged(boolean hasFocus) {
      super.onWindowFocusChanged(hasFocus);
          btn = (Button)findViewById(R.id.btnAnim);
          btnAnimation = (AnimationDrawable) btnAnim.getBackground();
          btnAnimation.start();
 }

下面似乎是问题,因为我的动画将不会正确的选择采取了,我得到了以下错误:

Here appears to be the problem, because my animation won't be taken correctly from the selector and I get the following error:

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 ...

在如何解决这一问题的任何想法?谢谢你。

Any idea on how to fix this ? Thanks.

推荐答案

您在做不正确铸 - 你的背景绘制的 StateListDrawable ,不是 AnimationDrawable 。我宁愿做这样的事情:

You're doing incorrect cast -- your background drawable is StateListDrawable, not AnimationDrawable. I'd rather do something like:

@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();
  }
}