Android bitmap.setPixel (x, y, color) sets the value of the passed value

Create a bitmap using Bitmap.create (25, 25, Config.ARGB_8888)

Setting a pixel with an alpha value less than or equal to 0xA9 causes the pixel to not be set with the transmitted one. I read another question that says setHasAlpha (true), which I did in my test, but that still didn't solve the problem.

Here is my test case for Android that shows my problem:

    public void testSettingBitmaps() {
    Bitmap bitmap = Bitmap.createBitmap(25, 25, Config.ARGB_8888);
    bitmap.setHasAlpha(true);

    int color = 0x00fefefe;
    int x= 0;
    int y = 0;

    for(int alpha = 0xFF000000; alpha != 0x00000000; alpha = alpha - 0x01000000) {
        int colorPlusAlpha = color + alpha;
        bitmap.setPixel(x, y, colorPlusAlpha);  

        //
        // This test succeeds if the bitmap let us set the pixel.
        //
        assertEquals(String.format("Current alpha value: %x, Expected pixel value: %x, Actual pixel value: %x", alpha, colorPlusAlpha, bitmap.getPixel(x, y)), 
                colorPlusAlpha, bitmap.getPixel(x, y)); 

    }
}

This code does not work with the following output: junit.framework.AssertionFailedError: Current alpha value: a9000000, Expected pixel value: a9fefefe, Actual pixel value: a9fdfdfd expected: <-1442906370> but was: <-1442972163>

+1
1

.

, -. , 0xa9fefefe, 0xa9a8a8a8 (0xa9 * 0xfe/255 = 0xa8.) , getPixel(), "un-premultiplied". 0xa9fdfdfd.

, ARGB 0xa9fefefe. RGB - . :

R = 169 * 254/255 R = 168 ( 0xa8)

, getPixel(), RGB alpha:

R = 255 * 168/169 R = 253 ( 0xfd)

+5

Source: https://habr.com/ru/post/1530843/


All Articles