Android 中背景为 AnimationDrawable 的按钮状态 [英] Button states with Background as AnimationDrawable in Android

查看:25
本文介绍了Android 中背景为 AnimationDrawable 的按钮状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在按钮的正常状态将动画作为背景而按下状态为静态图像的情况下,事情无法正常工作.

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

这篇关于Android 中背景为 AnimationDrawable 的按钮状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆