操纵阿尔法字节的Java / Android的颜色INT [英] Manipulate alpha bytes of Java/Android color int
问题描述
如果我有Java的一个int,我使用的是Android颜色(在画布绘画),我该如何处理这个int只是alpha分量?例如,我该如何使用的操作做到这一点:
If I have an int in Java that I'm using as an Android color (for drawing on a Canvas), how do I manipulate just the alpha component of that int? For example, how can I use an operation to do this:
int myOpaqueColor = 0xFFFFFF;
float factor = 0;
int myTransparentColor = operationThatChangesAlphaBytes(myOpaqueColor, factor);
//myTransparentColor should now = 0x00FFFFFF;
在理想情况下,这将是很好用任何系数
比刚才设置的字节静态值乘的第一个字节,而
Ideally, it would be nice to multiply those first bytes by whatever factor
is, rather than just set the bytes to a static value.
推荐答案
查看颜色类。
您code看起来有点这样的事情。
Your code would look a bit something like this.
int color = 0xFFFFFFFF;
int transparent = Color.argb(0, Color.red(color), Color.green(color), Color.blue(color));
所以,包装在一个方法可能如下:
So wrapping it in a method might look like:
public int adjustAlpha(int color, float factor) {
int alpha = Math.round(Color.alpha(color) * factor);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
然后把它设置透明度,让我们说,50%:
And then call it to set the transparency to, let's say, 50%:
int halfTransparentColor = adjustAlpha(0xFFFFFFFF, 0.5f);
我想使用所提供的Color类是多一点点的自我记录,只是自己做位操作,再加上它已经为你做了。
I think using the provided Color class is a little bit more self-documenting that just doing the bit manipulation yourself, plus it's already done for you.
这篇关于操纵阿尔法字节的Java / Android的颜色INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!