2

I am trying to make a very simple tile engine. However, using the Bitmap.getpixel(x,y) is not always matching the color correctly. It seems to be doing fine with 0xFFFFFFFF, 0xFF000000, 0xFF189600, and 0xFF18FF00, but has a problem with 0xFF186600. I tried changing it to multiple different similar colors, but it still doesn't seem to be reading it correctly. I am comparing with a simple switch statement. Here is the code for my method

public void LoadLevel(Canvas canvas, int levelName)
{

        Bitmap level = BitmapFactory.decodeResource(getResources(), levelName);
        Bitmap startTile = BitmapFactory.decodeResource(getResources(), R.drawable.starttile);

        canvas.drawColor(Color.WHITE);

        int drawX = 0;
        int drawY = 0;

        for(int y = 0; y < level.getHeight(); y++)
        {
            for(int x = 0; x < level.getWidth(); x++)
            {
                switch(level.getPixel(x, y))
                {
                    case 0xFF000000: break;

                    case 0xFFFFFFFF: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF189600: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF18FF00: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;

                    case 0xFF186600: 
                        canvas.drawBitmap(startTile, drawX, drawY, null); 
                        break;
                }

                Log.d("Color", Integer.toString(level.getPixel(x, y)));

                drawX += 128;
            }
            drawX = 0;
            drawY += 128;
        }
}

According to the log, the color is "-15177472". I am not sure what color that actually is though... So I am not sure if -15177472 == 0xFF186600

What am I doing incorrectly to not get the pixel? Is android changing the image? Are there safe colors I am suppose to use?

4

1 回答 1

0

-151774720xFF186900。请注意9数字而不是6您要查找的数字。

您从哪里获得这些预期值,以及如何加载位图?如果您在绘图程序中创建位图,然后将它们加载到您的 Android 应用程序中,您可能会发现它们被解压缩为 16bpp,在这种情况下,将像素值转换回 32bpp 的getPixel().

于 2011-11-28T11:08:15.927 回答