Trying to understand Bitmap.Config.ARBG_8888

和自甴很熟 提交于 2019-12-12 19:18:51

问题


I'm trying to understand packing of color bytes for ARGB_8888 format.

The documentation states that packing should be done using this formula:

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

But shouldn't it be instead:

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

When I unpack a sample pixel from a ARGB_8888 color encoded bitmap that has all red pixels, I'm using:

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

Which indeed returns me correct values for every color.

My point is, is it the documentation wrong or I'm missing something?


回答1:


Yes, you are correct and the documentation is wrong. If you look at the Android source code for Color.java it's done the second way:

* <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>

and further down...

@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;
}


来源:https://stackoverflow.com/questions/56832572/trying-to-understand-bitmap-config-arbg-8888

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!