6
我在colors.xml中定义了一堆颜色。我需要得到他们的int表示不是他们的资源ID,以便我可以操纵我的代码中的alpha和颜色。我怎样才能做到这一点?Android - colors.xml资源为int值
mColor = R.color.blue; // gets resource id not the actual color as an int.
mColor &= ~0xFF000000;
我在colors.xml中定义了一堆颜色。我需要得到他们的int表示不是他们的资源ID,以便我可以操纵我的代码中的alpha和颜色。我怎样才能做到这一点?Android - colors.xml资源为int值
mColor = R.color.blue; // gets resource id not the actual color as an int.
mColor &= ~0xFF000000;
Resources的getColor()方法返回0xAARRGGBB格式的颜色;
int color = getResources().getColor(R.color.white); // color is now 0xFFFFFFFF
int alpha = Color.alpha(color);
int red = Color.red(color);
...
您可以使用:
ContextCompat.getColor(getContext(), R.color.some_color);
此方法返回一个特定的资源ID相关联的颜色的整型表示。
完美和颜色。功能将派上用场。 – Mark 2011-03-10 03:55:14
android.content.res.Resources没有静态方法'getColor'。你的意思是'getResources()。getColor()'? – azdev 2011-09-15 04:09:04
getColor()已被弃用 – fnc12 2015-12-17 11:14:26