How to implement an eraser for a drawing app in Android - android

I have been searching all evening for some way to implement an eraser function for my drawing app. The most common answer is to simply paint the background color or image in, but this solution will not work for my application because I am implementing multiple layers (Gimp/Photoshop style).
The user should be able to draw a line with the brush tools provided in as many layers as they like (the onDraw method of my drawingview draws layer0...layerX on top of each other). Then if they choose the eraser tool it should cause any area of the current layer that they trace over to become transparent.
I cannot seem to find an appropriate class/function built in and am unsure how I could write it myself. I tried to do something like
Paint paint = new Paint();
paint.setAlpha(0);
and then use that Paint object to draw with, but that only draws an 'invisible' line.
I also attempted to use
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
but that just seemed to draw as normal and without effect. I'm probably using it incorrectly, but the Android documentation does not contain a clear description of what it does. I just happened to see it in some examples about modifying bitmaps.
I can supply code as needed, I am just not sure what would be helpful to solve my problem. Being pointed in the right direction would be the biggest help as I have not been successful with Google.

You can find the implementation of eraser function, create new canvas, brush function and save function, on this link :
create android drawing interface
it's have very good tutorial for making drawing app on android using motion event.

Related

paint in android studio

I have developed a drawing program in Android Studio with kotlin and I have placed a seekbar to change the width of the lines, but when I change the seekbar, the lines I have already drawn also change, which is not desirable.
I will upload any part of the code you need.
Just a wild guess here, I'll edit my answer if we get more info.
I'm assuming that you are using the onDraw(Canvas) function or something similar and in this function you are using a Paint to draw the lines each frame.
The problem is probably there, the Paint you are using isn't instanced. It seems that you are using the same object for all the lines, what you have to do is to have a different Paint object for each line or for each state of the seekbar.

Is there a way to draw a filled area with Canvas without using Path?

I am wondering if there is a way to draw filled areas (like a filled polygon) with the Android Canvas without using the Path class and call canvas.drawPath(...).
The reason I want to do this without Path is because I have to draw very large datasets and canvas.drawPath(...) is not hardware accelerated and therefore slow.
The performance when using canvas.drawLines(...) is much better because of hardware acceleration, however I have not found a way to draw the polygon filled using this approach (even when the lines are all connected).
Even calling paint.setStyle(Style.FILL) did not fill the polygon when using drawLines(...).
Is there a way to draw a filled-polygon without using the Path approach?
Or is there any other way to improve performance using the Canvas?
You might want to look at opengl view and use it for all the drawings you need. Definitely will be damn fast. Still, all your drawing code needs to be re-written.
You probably need to do something like :
Paint red = new Paint();
red.setColor(android.graphics.Color.RED);
red.setStyle(Paint.Style.FILL);
And use this color for your path, instead of your ARGB. Make sure the last point of your path ends on the first one, it makes sense also.

How to enable android antialiasing in java canvas?

we had problems to draw nice & smooth images in java, because they were jagged.
This code solved our problem:
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
but now we are making the same app for android, but we have no clue how to apply similar interpolation or antialiasing to an android app.
(we use eclipse emulator)
Use a Paint object.
create a Paint object p
set p.setFilterBitmap(true)
pass the paint p in your Canvas's draw methods
This will activate (bilinear) filtering that will smoothen the "content" of the bitmap. You can also try to activate anti-aliasing with Paint.setAntiAlias(true), but that will affect only the outer edges of the bitmap (it is pretty handy for text though).

Android Paint. 2 canvas?

I let my user draw geometries on it. So I store all the data(ie. coordinates) required for each geometries(ie. line, triangle, quads, etc). Now, I'm implementing a paint/brush on it which I don't care about all the points that was inputted. My problem is I need to call canvas.drawColor(Color.WHITE); every time the user modify the geometry resulting to clearing also the paint which is gone now and I don't have access to.
Is there any way to actually separate the two in two canvas and combining it later? I mean, the paint is directly draw on one canvas and the geometry on other canvas and combine it.
Yout can set the backgroundcolor of your View to White.
mView.setBackgroundColor(Color.WHITE);
This way you dont need to use
canvas.drawColor(Color.WHITE);
Check out TouchPaint.java in the API demos included in the Android SDK, for an example of how you can do what you want to do.

android Paint.setShadowLayer() vs Canvas.drawbitmap() problems

I use the following code to draw a bitmap during onDraw of a custom View
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
p.setShadowLayer(3,3,3,0xff000000);
Bitmap bmp = BitmapFactory.decodeResource(some drawable jpg);
canvas.drawBitmap(bmp,null,new Rect(blah blah),p);
and the shadow looks funny. What do I mean funny? I mean that I get a different shadow then if I was calling canvas.drawRect(). It looks sorta like the android shadow code is treating the bitmap as an alpha channel or something.
have tried adding p.setColor(0xff000000); and p.setStyle(Paint.Style.FILL); but not difference.
I guess I could drawRect with shadow on, then drawBitmap with shadow off, but that seems silly as it would be rendering pixels twice.
Basically the shadow layer doesn't work for anything except text. It's real dumb. Check the Android hardware acceleration supported operations chart.
This question was asked back in 2010 when hardware accelerated view trees didn't exist.. based on things I've read, even then the shadow layer only worked for simple shapes (if at all), and others got weird results using it on anything but text. You might be out of luck.
Finally, there are many ways you can fake a shadow layer. You can wrap a view in another view and draw underneath it. You can write a view that draws the shadow yourself as a radial gradient, etc. But you probably just want to make a 9patch that looks like a shadow and use that.
2018 Update
It looks like most of the operations are supported as of api 28! I haven't had a chance to play around with shadow layers recently, but things are looking much more optimistic now.
Maybe http://developer.android.com/reference/android/graphics/Paint.html#isAntiAlias()
can help you.
paint.setAntiAlias(true);
Maybe you should have different Paint objects for different purposes like text, bitmap, etc. So one's setting will not affect others.

Categories

Resources