2

Hi I was reading the fingerpaint example, because I'm building a signature activity, that allows the user to draw a signature on the cellphone and then save it to SD.

So far I've seen that the mPath variables holds the path that the user is currently drawing, and this path is drawn onto the screen on the onDraw(..) method by calling

canvas.drawPath(mPath, mPaint);

However on the example there is another canvas "mCanvas" that draws the path on the touch listener:

private void touch_up() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    mPath.reset();
}

And this is what I don't get. what is exactly this mCanvas object, and why are they using it in the example, it seems that only the regular canvas from the onDraw method and the mPath variable would have been enough for doing this?

Julian Suarez
  • 4,499
  • 4
  • 24
  • 40

1 Answers1

3

The onDraw method is executed on the UI thread. While we don't have access to the UI thread (you don't want to be using the UI thread that often) we keep an off-screen Bitmap with a Canvas that we use to draw on it.

Why do this? This is because it allows us to focus on the drawing/processing without having to worry about blocking the UI thread.

Note: Calling the method invalidate (or postInvalidate) does not instantaneously block and call onDraw - it just queues up a draw call with the OS.

Che Jami
  • 5,151
  • 2
  • 21
  • 18
  • So if I understand correctly: 1. mCanvas is constructed and we pass it mBitmap to draw on top of it. 2. inside onTouchEvent we draw mPath using our mCanvas which in turn draws on mBitmap. 3. when onDraw(Canvas canvas) is called we draw mBitmap using the canvas "canvas" and not mCanvas. – Julian Suarez Aug 25 '11 at 15:10
  • and what about mBitmapPaint, and mPaint? – Julian Suarez Aug 25 '11 at 15:11
  • Yes this is correct. We draw onto our off-screen `Bitmap` first and when we get a draw call, we draw that `Bitmap` onto the *on-screen* `Canvas` provided with the call. – Che Jami Aug 25 '11 at 15:23