0

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.

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I thought I was doing it wrong by sending the canvas my own bitmap. Just couldnt figure out a way to reference it afterwards.
@Finn I'm glad we resolved your issue; thank you for taking the time to accept my answer. :-) Android animation is not trivial...I have found it helpful to use sample code as a point of departure whenever possible. Off the top my head, the closest thing I can think of to what you have shown is the Lunar Lander example in the SDK. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.