开发者

bitmap not displaying in android

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 开发者_StackOverflow社区- 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.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜