android 关于Button在StateListDrawable 中使用AnimationDrawable的问题

最近在实现一个button的效果时,UI要求未点击时,button有相应的动画,按住时有相应的效果,于是自然而然的想到了使用StateListDrawable ,以及帧动画。

遇到了一个异常:android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable。

以下是我的相应代码:
xml中:

<Button android:id="@+id/setting_user_rank_id"
            android:layout_width="@dimen/person_setting_user_rank_btn_width"
            android:layout_height="@dimen/person_setting_user_rank_btn_height"
            android:layout_marginRight="@dimen/person_setting_user_rank_btn_margin_right"
            android:layout_marginTop="@dimen/person_setting_user_rank_btn_margin_top"
            android:background="@drawable/person_setting_user_rank_style"
            android:layout_alignParentRight="true"
            android:onClick="onClick"/>

person_setting_user_rank_style.xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:drawable="@drawable/person_setting_user_rank_pressed" android:state_pressed="true"/>
   <item android:drawable="@anim/user_rank_btn_anim" android:state_focused="true"/>
   <item android:drawable="@anim/user_rank_btn_anim" />

</selector>

res/anim/user_rank_btn_anim.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/user_rank_btn_bg_0" android:duration="125"></item>
    <item android:drawable="@drawable/user_rank_btn_bg_1" android:duration="125"></item>
    <item android:drawable="@drawable/user_rank_btn_bg_2" android:duration="125"></item>
    <item android:drawable="@drawable/user_rank_btn_bg_3" android:duration="125"></item>
    <item android:drawable="@drawable/user_rank_btn_bg_4" android:duration="125"></item>
</animation-list>

一开始在activity中是直接调用了:

mUserRankAnim= (AnimationDrawable) mUserRankBtn.getBackground(); 
mUserRankAnim.start();

结果出现了:
android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable异常。

由于button的android:background采用的是StateListDrawable ,那么mUserRankBtn.getBackground()得到的也就会是StateListDrawable ,而不是AnimationDrawable,所以会出现这个异常。

正确的写法是:

private void startUserRankAnim() {
        //让按钮获取到焦点(为了进入activity后自动开始动画,如果是点击button后才开始动画,那么可以不用获取焦点)
        mUserRankBtn.setFocusable(true);
        mUserRankBtn.setFocusableInTouchMode(true);
        mUserRankBtn.requestFocus();
        mUserRankBtn.requestFocusFromTouch();
        StateListDrawable background = (StateListDrawable) mUserRankBtn.getBackground();
        Drawable current = background.getCurrent();
        if (current instanceof AnimationDrawable) {
            mUserRankAnim = (AnimationDrawable) current;
            mUserRankAnim.start();
        }
    }

参考资料:

http://stackoverflow.com/questions/5299219/button-states-with-background-as-animationdrawable-in-android

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,967评论 25 709
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 12,866评论 0 6
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,651评论 2 45
  • 今天没做什么事,拿着手机左刷刷右刷刷,昨晚想着今天要做的事也没做,看来还是要写过时间计划表啊,把明天要做的事写下来...
    向往精灵阅读 819评论 0 0
  • 9yue7 委托 delegate 一、委托的概念 把方法作为一个参数来传递(方法是变化的) 二、声明委托 访问修...
    cGunsNRoses阅读 2,187评论 0 0