位图在 Android 中不显示

发布于 2024-11-07 14:07:57 字数 1651 浏览 0 评论 0原文

我有以下代码;

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凯凯我们等你回来 2024-11-14 14:07:57

这有点超出我的正常视野,但由于没有其他人回答,我会尝试一下。 :-)

当您以这种方式绘制时(即使用surfaceHolder.lockCanvas),您需要使用您获得的画布...您不能只创建一个新画布并将其发送到unlockCanvasAndPost。在正常情况下,您也不应该对画布的底层位图进行复杂的操作。在渲染函数中,为什么不直接绘制到画布中(首先是背景颜色,甚至是整个位图,然后是您需要的其他内容)。例如,类似:

render(Canvas c) {
    c.drawColor(Color.GREEN);
    c.drawBitmap(sprite, x, y, null);
}

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:

render(Canvas c) {
    c.drawColor(Color.GREEN);
    c.drawBitmap(sprite, x, y, null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文