How to implement a real eraser in my drawing app? - android

I am creating a simple drawing app using kotlin. I want to implement an eraser and was not able to find any proper solution for this. Every solution either uses white color as a substitute for eraser or completely resets the canvas after clicking on eraser button. As I have also implemented the feature of importing background images from the gallery, I can't use white color as eraser.

There is no magic eraser in canvas, because erasing is nothing but drawing something onto the canvas. You can't set the paint color to Transparent and draw on canvas to erase what is already drawn. This simply draws something that is transparent, which is basically nothing.
So you will need to implement an eraser by using a Color. Normally this is white, because the background is usually set to white. So this gives the appearance of erasing the content.
In your case, you have a background image. To implement an eraser for this, you will need 2 canvas. The background image will be drawn in the 1st canvas. All the other content (drawCircle, drawRect, drawPath, etc...) are drawn onto the 2nd canvas. Now, you could take the bitmap out of the 2nd canvas and draw that in the 1st canvas (drawBitmap). This gives the final appearance.
Now, in this setup, you could use "White" as a color to erase from the 2nd canvas. While drawing 2nd canvas content onto the 1st, use one of the PorterDuff mode, so that the "White" goes away.

Related

canvas drawing issue where paint needs to be restricted by black sketch

I need to create the painting application for the kids where kid can colour inside the black bordered sketch of any image
But, I am struck with the problem,that colouring can come outside the black bordering of the image...which i don't want to.Please guys help me to find out how to restrict the colouring by user within the black border of sketch
Also, I want that no colour should cover the black border.it should be inside the border.
I can post my code if required.
Look at Canvas clipPath and use the clipping built in to android to limit the 'coloring' of the kid/user to the regions you specify. You can then draw your objects as layers, kid/user colored layer on bottom, your sketch on top.
Finally..i have implemented it using mask bitmap and right usage of Portet.Duff mode

Selectively masking in android canvas

I have a canvas on which the background image if first drawn.
Then another image is drawn on top of the background.
I have a Gradient object that moves across the screen. The Paint used in the Gradient has its Xfermode set as,
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
The effect works but it is also applied on the background image. How can i avoid the background image from affected by the mask on top?
The paint will be applied on the whole of your Canvas. As your background is drawn using the same Canvas than the Gradient object, of course using a custom Xfermode will affect said background!
One possible solution would be to separate your background and your foreground in 2 different Canvas objects backed up by separate Bitmap objects, then merge the layers together as one would do in Photoshop. I posted a sample code that does just this on StackOverflow a while ago, here is the link to it:
https://stackoverflow.com/a/10370828/1350375

android: How to implement hardware accelerated image eraser?

I have a bitmap whiteboard app drawing above an imageview background. When I draw a CLEAR paint on top
mo_ink.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
or any PorterDuff method, the rendering has a horrible black border while drawing. Disabling hardware acceleration makes the app unusable.
How can I implement a background eraser without the hardware rendering errors?
Edit: I have that slightly wrong. PorterduffMOde CLEAR draws a black path that renders clear on completion. Any other Porterduff method I've tried renders a black bounding box around the path. Ideally I'd like the drawn line to be immediately transparent, drawing the background bitmap masked to the path, but applying a suitable shader would result in the black border. Any solutions? I can't work out what Rasterizer does or if it'll help!

On an android Canvas, how do I draw overlapping shapes with non-interacting alphas?

On an android Canvas, if I draw a circle with alpha 0xCC and color Color.RED and then draw another circle which partially overlaps the first circle with the same parameters, I'll end up with a venn diagram.
Here is a random example I found (just ignore the [Text] in there). I want to draw overlapping circles like in this diagram, but I don't want the center to be darker, but I do want the whole thing to have alpha so that the map underneath is visible.
Is there a way to do this directly or do I need to draw to a bitmap without alpha and then set the alpha for the whole bitmap and draw it to a canvas? (I haven't used bitmaps yet, so I am not sure how they are used yet.)
The easy way would be your suggested solution, ie. drawing all circles with no alpha to a bitmap, then draw that bitmap to another one using the desired alpha.
The hard way would be using blend modes, specifically PorterDuff.Mode in Android. An example can be found here.

Android draw with blur

I need do draw on Android's Canvas using Blur effect,
it is a very simple feature,
I need to draw a circular area, which is blurred (the foreground) and the background transparent, I can do everything with manipulating the colour alpha to do it with custom transparency but I need it to be blurred instead of transparent..
any ideas?
(For those who are coming back, though this is an old question)
On the Paint object which you are using to draw the Color, set
paint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL))
Then, in Android Manifest.xml, set
android:hardwareAccelerated="false"
for your activity
(Blur doesn't work when hardware acceleration is true)
For more explanation refer
Android BlurMaskFilter has no effect in canvas.drawOval while text is blurred
If you're not gonna need to manipulate it afterwards you could just draw to a buffer and blur it yourself pixel by pixel, then blit that buffer to screen when you need to paint it.
Or this: http://developer.android.com/reference/android/graphics/BlurMaskFilter.Blur.html
Take a look at this: Android, Blur Bitmap instantly?
Again I would recommend you first to draw into a bitmap. Scale it down (try different sizes), and the scale it back up to the original size (either as a new bitmap or if you use hw acceleration do it while you draw).

Categories

Resources