Home » Android » Android – Canvas.clipRect() – Removing existing clip

Android – Canvas.clipRect() – Removing existing clip

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a Canvas object, at some point I set a clip so I could draw into that ‘window/section’ without worrying about overflow into other parts of the canvas.

After I’m done drawing into the ‘window’ I want to remove that clip area, which I cant seem to do.

There is no remove clip function, when I try to apply a new clip on the entire canvas, that clip is not being applied – Calling canvas.getClipBounds() right after applying the new clip returns a Rect with the previous clip.

How to&Answers:

you must use ‘clipRect (Rect rect, Region.Op op)’ with op Union so it´ll add. It´s croping more and more until you specify the add operation. It´s kind of a boolean intersect. set the union with all the surface. Also you can canvas.save() before croping, crop, draw, and restore to have full clip bounds again.

BTW, notice the javadoc for clipRect specifies the clip will apply to the current clip so it´s an AND operation, intersecting both paths

Answer:

Use canvas.save() before setting the clip rect then canvas.restore() to discard the clipping rect.