位图在 Android 中不显示
我有以下代码;
@Override
public void run()
{
tickCount = 0L;
Log.d(TAG, "Starting game loop");
while (running)
{
try
{
gameCanvas = surfaceHolder.lockCanvas();
synchronized (surfaceHolder)
{
// Log.d(TAG, tickCount + " ticks so far");
tickCount++;
updateGameState();
render(gameCanvas);
}
} finally
{
if (gameCanvas != null)
{
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
surfaceHolder.unlockCanvasAndPost(gameCanvas);
}
}
}
}
我遇到的问题是当我测试应用程序时位图不显示。我尝试使用 setBitmap 复制可绘制对象。
例如。
mutableBackground = backgroundImage.copy(Bitmap.Config.ARGB_8888, true);
其中backgroundImage是解码后的可绘制资源。
和..
mutableBackground = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
gameCanvas = new Canvas();
gameCanvas.setBitmap(mutableBackground);
gameCanvas.drawColor(Color.GREEN);
和..
gameCanvas = new Canvas(mutableBackground);
如果我在渲染方法中画一条线,它确实会画.. 但在黑色背景上。奇怪的是,如果我这样做
int myColor = mutableBackground.getPixel(100, 100);
int greeness = Color.green(myColor);;
Log.d(TAG, "Greeness - " + greeness);
日志显示.. Greenness - 255
因此位图被着色为绿色,但显示为黑色。 这真的让我很困惑,我必须尽快解决这个问题。
感谢任何可以提供帮助的人。
I have the following code;
@Override
public void run()
{
tickCount = 0L;
Log.d(TAG, "Starting game loop");
while (running)
{
try
{
gameCanvas = surfaceHolder.lockCanvas();
synchronized (surfaceHolder)
{
// Log.d(TAG, tickCount + " ticks so far");
tickCount++;
updateGameState();
render(gameCanvas);
}
} finally
{
if (gameCanvas != null)
{
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
surfaceHolder.unlockCanvasAndPost(gameCanvas);
}
}
}
}
The problem I am having is when I test the application the bitmap doesnt display. I have tried copying a drawable using and using setBitmap.
eg.
mutableBackground = backgroundImage.copy(Bitmap.Config.ARGB_8888, true);
where backgroundImage is a decoded drawable resource.
and..
mutableBackground = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
gameCanvas = new Canvas();
gameCanvas.setBitmap(mutableBackground);
gameCanvas.drawColor(Color.GREEN);
and..
gameCanvas = new Canvas(mutableBackground);
If I draw a line in the render method, it does indeed draw.. but on a black background. Strangely enough if I do
int myColor = mutableBackground.getPixel(100, 100);
int greeness = Color.green(myColor);;
Log.d(TAG, "Greeness - " + greeness);
The log shows.. Greeness - 255
So the bitmap is being colored green but it displays as black.
This is really confusing me and I have to sort this out real soon.
Thanks anybody that can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点超出我的正常视野,但由于没有其他人回答,我会尝试一下。 :-)
当您以这种方式绘制时(即使用surfaceHolder.lockCanvas),您需要使用您获得的画布...您不能只创建一个新画布并将其发送到unlockCanvasAndPost。在正常情况下,您也不应该对画布的底层位图进行复杂的操作。在渲染函数中,为什么不直接绘制到画布中(首先是背景颜色,甚至是整个位图,然后是您需要的其他内容)。例如,类似:
This is a little bit outside my normal ken, but since no one else has answered, I will take a stab. :-)
When you draw this way (i.e. with surfaceHolder.lockCanvas) you need to work with the canvas you get...you cannot just create a new canvas and send that to unlockCanvasAndPost. Nor should you, under normal circumstances, futz with the canvas's underlying bitmap. In your render function, why don't you just draw into the canvas (first the color for the background, or even a whole bitmap, and then whatever else you need). For example, something like: