假设我想创建一个像素值数组以传递到描述的 createBitmap
方法 here .我有 0 - 0xff 范围内的三个 int 值 r, g, b
。如何将它们转换为不透明像素 p
?
alpha channel 是高位还是低位?
我用谷歌搜索了 documentation但它只说明:
Each pixel is stored on 4 bytes. 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. It should be used whenever possible.
那么,这个方法怎么写呢?
int createPixel(int r, int g, int b)
{
return ?
}
最佳答案
它看起来像 RGBA pixel format有很好的记录,我假设这就是 Android 文档的意思,只是使用不同的名称来匹配位字段中的 channel 成员位置:
像这样的东西应该可以工作:
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;
}
此外,您可能希望使用 byte
而不是 int
来避免溢出类型的错误,或者只屏蔽您想要的位:
int createPixel(int r, int g, int b, int a) {
return ((a & 0xff) << 24)
| ((r & 0xff) << 16)
| ((g & 0xff) << 8)
| ((b & 0xff));
}
虽然将值限制在 [0,255]
可能更有意义。
关于java - 如何创建 ARGB_8888 像素值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246566/