Android inverse clip

£可爱£侵袭症+ 提交于 2019-12-06 15:34:08

问题


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:


回答1:


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.




回答2:


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

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