Bitmap.getpixel 读取的颜色不正确?
我正在尝试制作一个非常简单的平铺引擎。但是,使用 Bitmap.getpixel(x,y) 并不总是能正确匹配颜色。它似乎对 0xFFFFFFFF
、0xFF000000
、0xFF189600
和 0xFF18FF00
表现良好,但对 有问题代码>0xFF186600。我尝试将其更改为多种不同的相似颜色,但它似乎仍然无法正确读取。我正在与一个简单的 switch 语句进行比较。这是我的方法的代码
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;
}
}
根据日志,颜色是“-15177472”。我不确定实际上是什么颜色...所以我不确定 -15177472 == 0xFF186600
我做错了什么而没有获得像素?安卓会改变图像吗?我应该使用安全颜色吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-15177472
是0xFF186900
。请注意您要查找的9
数字而不是6
。您从哪里获得这些预期值,以及如何加载位图?如果您在绘图程序中创建位图,然后将它们加载到 Android 应用程序中,您可能会发现它们被解压缩为 16bpp,在这种情况下,当将像素值转换回 32bpp 时,精度会有所损失 <代码>getPixel()。
-15177472
is0xFF186900
. Note the9
digit instead of the6
you were looking for.Where are you getting these expected values from, and how are you loading the bitmaps? If you are creating the bitmaps in a drawing program, and then loading them in your Android app, you may find that they are being decompressed as 16bpp, in which case there will be some loss of accuracy when converting pixel values back to 32bpp for
getPixel()
.