试图了解Bitmap.Config.ARBG_8888

时间:2019-07-01 08:45:54

标签: android colors bit-manipulation pixel

我试图了解ARGB_8888格式的彩色字节的打包。

documentation指出应使用以下公式进行打包:

int color = (A & 0xff) << 24 | (B & 0xff) << 16 | (G & 0xff) << 8 | (R & 0xff);

但不应该这样:

int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);

当我从具有所有红色像素的ARGB_8888颜色编码位图中解压缩样本像素时,我正在使用:

    final int r = (p >> 16) & 0xff;
    final int g = (p >> 8) & 0xff;
    final int b = p & 0xff;

这确实为我返回了每种颜色的正确值。

我的意思是,说明文件是否错误或我缺少什么?

1 个答案:

答案 0 :(得分:1)

是的,您是正确的,文档是错误的。如果您查看Color.java的Android source code,它是第二种方法:

* <h4>Encoding</h4>
* <p>The four components of a color int are encoded in the following way:</p>
* <pre class="prettyprint">
* int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
* </pre>

再往下走...

@ColorInt
public static int argb(
        @IntRange(from = 0, to = 255) int alpha,
        @IntRange(from = 0, to = 255) int red,
        @IntRange(from = 0, to = 255) int green,
        @IntRange(from = 0, to = 255) int blue) {
    return (alpha << 24) | (red << 16) | (green << 8) | blue;
}