Button states with Background as AnimationDrawable in Android

前端 未结 2 1276
梦谈多话
梦谈多话 2020-12-03 03:47

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 nic

2条回答
  •  一向
    一向 (楼主)
    2020-12-03 04:37

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

提交回复
热议问题