Android inverse clip

萝らか妹 提交于 2019-12-04 23:01:43

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!