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:
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.
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);
}
来源:https://stackoverflow.com/questions/29638983/android-inverse-clip