[英]How to create an ARGB_8888 pixel value?
Say I want to create an array of pixel values to pass into the createBitmap
method described here . 假设我要创建一个像素值数组,以传递到此处描述的createBitmap
方法中。 I have three int values r, g, b
in the range 0 - 0xff. 我有三个int值r, g, b
,范围为0-0xff。 How do I transform those into an opaque pixel p
? 如何将它们转换为不透明像素p
?
Does the alpha channel go in the high byte or the low byte? alpha通道进入高字节还是低字节?
I googled up the documentation but it only states that: 我用谷歌搜索了文档,但是只声明了:
Each pixel is stored on 4 bytes. 每个像素存储在4个字节上。 Each channel (RGB and alpha for translucency) is stored with 8 bits of precision (256 possible values.) This configuration is very flexible and offers the best quality. 每个通道(RGB和alpha表示半透明)均以8位精度(256个可能的值)进行存储。此配置非常灵活,可提供最佳质量。 It should be used whenever possible. 应尽可能使用它。
So, how to write this method? 那么,该怎么写呢?
int createPixel(int r, int g, int b)
{
return ?
}
It looks like the RGBA pixel format is pretty well documented and I'm assuming that's what the Android docs mean, just using a different name to match the channel membership position in the bit field: 看来RGBA像素格式已被很好地记录下来,我假设这就是Android文档的含义,只是使用一个不同的名称来匹配bit字段中的通道成员资格位置:
Something like this should work: 这样的事情应该起作用:
int createPixel(int r, int g, int b) {
return createPixel(r, g, b, 0xff);
}
int createPixel(int r, int g, int b, int a) {
return (a<<24) | (r<<16) | (g<<8) | b;
}
Also, you might want to use a byte
instead of an int
to avoid overflow-type errors, or mask only the bits you want: 另外,您可能希望使用byte
而不是int
以避免溢出类型错误,或仅屏蔽所需的位:
int createPixel(int r, int g, int b, int a) {
return ((a & 0xff) << 24)
| ((r & 0xff) << 16)
| ((g & 0xff) << 8)
| ((b & 0xff));
}
Although clamping the values to [0,255]
might make more sense. 尽管将值限制为[0,255]
可能更有意义。
从文档 :
(alpha << 24) | (red << 16) | (green << 8) | blue
First of all, your arguments should be bytes, not ints, but I guess this could be an implementation issue. 首先,您的参数应该是字节,而不是整数,但是我想这可能是实现问题。
In general you would do something like 一般来说,您会做类似的事情
return b | r<<8 | g<<16 | 255<<24;
assuming your alpha is 255 (non-transparent). 假设您的Alpha为255(非透明)。
This should work: 这应该工作:
int createPixel(int r, int g, int b)
{
return 0xff000000 | (r << 16) | (g << 8) | b;
}
or, if you want to be baroque: 或者,如果您想成为巴洛克风格:
int createPixel(int r, int g, int b)
{
return (((((-1 << 8) | r) << 8) | g) << 8) | b;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.