如何获得带有波纹可绘制按钮的颜色 [英] How to get color of a button with ripple drawable
本文介绍了如何获得带有波纹可绘制按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有各种各样的按钮,我想获得它们的背景颜色,在彩色可绘制对象中获取背景颜色很容易,但在波纹可绘制对象中并不容易,我如何设法从波纹可绘制对象中获取背景颜色.
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.
推荐答案
尝试一下:
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();
}
这篇关于如何获得带有波纹可绘制按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文