开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜