问题
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