7

How does one go about doing this? Could somebody give me an outline?

From what I've found online, it seems like in my run() function:

  1. create a bitmap
  2. create a canvas and attach it to the bitmap
  3. lockCanvas()
  4. call draw(canvas) and draw bitmap into back buffer (how??)
  5. unlockCanvasAndPost()

Is this correct? If so, could I get a bit of an explanation; what do these steps mean and how do I implement them? I've never programmed for Android before so I'm a real noob. And if it isn't correct, how DO I do this?

Kalina
  • 5,504
  • 16
  • 64
  • 101

2 Answers2

17

It's already double buffered, that's what the unlockCanvasAndPost() call does. There is no need to create a bitmap.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • I don't quite get it. So, I just draw everything I need between lock and unlock and... double buffering happens on it's own? Don't I need to also draw BEFORE I lock? Sorry, I need this explained in the most basic way possible... – Kalina Jun 30 '11 at 17:32
  • 7
    @TheBeatlemaniac: When you're drawing on a `Canvas` after `lockCanvas()` has been called, you're actually drawing stuff on the *next* frame, while the *current* frame is being displayed. An `unlockCanvasAndPost()` call will switch the next frame buffer with the current frame buffer in order to display an updated `Canvas`. – Wroclai Jun 30 '11 at 17:36
  • The bitmap portion comes in handy if you want to be able to zoom in/out and move your "worskpace" around. – TheRealChx101 Jan 08 '16 at 13:55
2

The steps from Android Developers Group say that you need a buffer-canvas, to which all the renders are drawn onto.

Bitmap buffCanvasBitmap;
Canvas buffCanvas;

// Creating bitmap with attaching it to the buffer-canvas, it means that all the changes // done with the canvas are captured into the attached bitmap
tempCanvasBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
tempCanvas = new Canvas();
tempCanvas.setBitmap(tempCanvasBitmap);

// and then you lock main canvas
canvas = getHolder().lockCanvas();              
// draw everything you need into the buffer
tempCanvas.drawRect.... // and etc
// then you draw the attached bitmap into the main canvas
canvas.drawBitmap(tempCanvasBitmap, 0, 0, drawView.getPaint());
// then unlocking canvas to let it be drawn with main mechanisms
getHolder().unlockCanvasAndPost(canvas);

You are getting the main buffer, which you are drawing into without getting different double-buffer canvas' on each holder's lock.

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85