10275

Android FingerPaint Example using Canvas, what is the offscreen Canvas?

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?

Answer1:

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 <strong>off-screen</strong> 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.

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

Recommend

  • How can I get the size of the Text Widget in flutter
  • How to draw a curved line between 2 points on canvas?
  • How to save draw paint with custom view android?
  • GIF image works on emulator but not in real device?
  • How to make a custom Edittext,so that it will look like as 45 degree rotated in android
  • Diamond Shaped Button with transparent borders
  • Grid-lines on a GridView
  • Passing an array from class A to class B
  • Draw a bitmap from a control taller than the screen
  • Drawing a polygon over the entire map
  • Workflow - Database Design
  • How to resend request with android volley when not authorized
  • Removing event listeners on automatically created multiple elements
  • iOS Localization Doesn't Work with More Than 63 Files
  • Java making confirming exit
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • How to draw a line dynamically in android [duplicate]
  • Blackberry 6: how to detect a long click on track pad?
  • Application level floating views with navigation in Android
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • AndEngine Applying Transparancy to AndEngine View
  • Using a canvas object in a thread to do simple animations - Java
  • Chart.js Multiple dataset
  • Avoid links criss cross / overlap in d3.js using force layout
  • QLineEdit password safety
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Trying to switch camera back to front but getting exception
  • Javascript + PHP Encryption with pidCrypt
  • Why winpcap requires both .lib and .dll to run?
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Getting Messege Twice Using IMvxMessenger
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • Android Heatmap on canvas or ImageView
  • How can I use threading to 'tick' a timer to be accessed by other threads?