• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps onDraw, invalidate

shiva01

Lurker
I have a simple draw application on Canvas. User should be able to draw with a chosen color. On MotionEvent.ACTION_UP, invalidate() is called. Now it redraws previously drawn lines with the current color ( Redrawing the entire drawing with the current color). I do not want to change the color of previously drawn lines. I am not sure what is missing. Can I make it redraw only newly drawn lines?
 

Attachments

When I run your code it doesn't quite behave the way you describe. It draws a line when I drag my finger, but then everything gets cleared on a pointer up event.

I think you need to create a Bitmap object. When the user drags their finger over the view, draw to the bitmap. Then in the View.onDraw() function just draw the bitmap to the screen. If you application supports rotation you will need to do something clever with the bitmap.
 
Sorry George. here is the code. I guess I changed the code just before the upload.
Now it shows what I have described in my problem.

Thanks for your help.
 

Attachments

When you ask a View to draw itself, you have to draw the whole thing - it doesn't remember what you drew last time.

By creating a backing bitmap and drawing to that, you avoid the problem.

In the first code you uploaded, when you get pointer events you push points onto a path, but clear it on a pointer up (so when you draw after that point you get a blank canvas.)

In the second version of the code, you don't clear the contents of the path. So every point you have added remains in the path. Now when you change the colour the entire path gets drawn in the new colour.

The third version of the code is like the first version, except you draw to a bitmap. Now after a pen up the path is cleared, but the pixels you have drawn remain on the bitmap. When you change colour new paths are drawn in the new colour, but because you cleared the old points from the path you don't draw over the top of your old lines.
 
Back
Top Bottom