Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android inverse clip

I need to specify a rectangle in which the Android canvas CANNOT draw inside of. I know that clipRect will specify and area in which to draw in, but I was if I could reverse this effect. In other words how do I draw an object making it draw to the outside of a rectangle. Image for clarification:

Clipping example

like image 795
TameHog Avatar asked Oct 15 '25 10:10

TameHog


2 Answers

With Android O, Canvas exposes the API clipOutPath(Path path); for targeting earlier versions, you can use clipPath(Path path, Region.Op op) as alluded to by @Gabe Sechan.

The implementation would look something like:

@Override
protected void dispatchDraw(Canvas canvas) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        canvas.clipOutPath(path);
    } else {
        canvas.clipPath(path, Region.Op.DIFFERENCE);
    }
    super.dispatchDraw(canvas);
}
like image 195
Tom Howard Avatar answered Oct 17 '25 00:10

Tom Howard


I'm not sure if this is actually going to be more performant than doing an overdraw. But you could set a clipping path to the full view, then set a second one to the exclusion zone with Region.Op DIFFERENCE set. That would set the clipping rect to the difference between the two.

like image 41
Gabe Sechan Avatar answered Oct 16 '25 23:10

Gabe Sechan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!