How to get color of a button with ripple drawable

做~自己de王妃 提交于 2020-08-06 04:32:05

问题


I have variety of buttons and I would like to get their background color, getting background color in a color drawable is easy but it is not easy in ripple drawable, how can I manage to get background color from ripple drawable.


回答1:


Try this:

 RippleDrawable rippleDrawable = (RippleDrawable) button.getBackground();
 Drawable.ConstantState state = rippleDrawable.getConstantState();
 try {
     Field colorField = state.getClass().getDeclaredField("mColor");
     colorField.setAccessible(true);
     ColorStateList colorStateList = (ColorStateList) colorField.get(state);
     int rippleColor = colorStateList.getDefaultColor();
  } catch (NoSuchFieldException e) {
      e.printStackTrace();
  } catch (IllegalAccessException e) {
      e.printStackTrace();
  }



回答2:


please check following example for ripple effect in android..

http://www.viralandroid.com/2015/09/how-to-add-ripple-effect-to-android-button.html



来源:https://stackoverflow.com/questions/36352945/how-to-get-color-of-a-button-with-ripple-drawable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!