Android - colors.xml resource to int value
I have a bunch of colors defined in the colors.xml. I need to get their int representation not their resource id so that I can manipulate the alpha and the colors in my code. How can I do this?
mColor = R.color.blue; // gets resource id not the actual color as an int.
mColor &= ~0xFF000000;开发者_Go百科
The getColor() method of Resources returns the color in 0xAARRGGBB format;
int color = getResources().getColor(R.color.white); // color is now 0xFFFFFFFF
int alpha = Color.alpha(color);
int red = Color.red(color);
...
You can use:
ContextCompat.getColor(getContext(), R.color.some_color);
This returns the int representation of the color associated with a particular resource ID.
精彩评论