Live2D

java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed 解决

/*最新版本*/

1.网上大部分都是底下的解决方法(不过有狠人可能会去修改版本号,哈哈!都很无奈啊!)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    canvas.clipPath(path);(第二个参数默认为Region.Op.INTERSECT)
} else {
    canvas.clipPath(path, Region.Op.XOR);// REPLACE、UNION 等
}
 

此方法根本就是舍弃了XOR啊,我的预期效果就没实现出来。

2.path替换

参考(1)Android10适配_星月黎明的博客-CSDN博客

        (2)android - What is the best alternative to `canvas.clipRect` with `Region.Op.REPLACE`? - Stack Overflow

 如果需要一些高级逻辑运算效果怎么办?

如小说的仿真翻页阅读效果,解决方案如下,用Path.op代替,先运算Path,再给canvas.clipPath:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
    Path mPathXOR = new Path();
    mPathXOR.moveTo(0,0);
    mPathXOR.lineTo(getWidth(),0);
    mPathXOR.lineTo(getWidth(),getHeight());
    mPathXOR.lineTo(0,getHeight());
    mPathXOR.close();
    //以上根据实际的Canvas或View的大小,画出相同大小的Path即可
    mPathXOR.op(mPath0, Path.Op.XOR);
    canvas.clipPath(mPathXOR);
}else {
    canvas.clipPath(mPath0, Region.Op.XOR);
}
 

 此代码在有path传过来的方或许能用吧,可我有的地方没有path啊

//待解决

/*旧版本*/

1.参考java.lang.IllegalArgumentException: Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed · Issue #541 · alamkanak/Android-Week-View (github.com)

After reading some StackOverflow posts I found the solution to this problem without changing target SDK.

The problematic line is canvas.clipRect() when used with Region.Op.Replaced

To fix the error you have to:

  • remove Region.Op.Replaced from every canvas.clipRect()
  • wrap canvas.clipRect() inside canvas.save() and canvas.restore()
    e.g ` canvas.save();

TL;DR
Source code

2.总之需要你把

(1)
例如canvas.clipPath(mNextPagePath, Region.Op.INTERSECT) 
替换为;
//canvas.clipPath(mNextPagePath, Region.Op.INTERSECT);canvas.clipPath(mNextPagePath);

//注意是所有的都得替换

(2)把canvas.clipRect(int,int,int,int)放入canvas.save() 与 canvas.restore()之间

我的为

canvas.clipRect(mViewWidth,0,getWidth(),(mViewHeight+36)+mHeaderRowPadding);(你的应该不一样)

4个int 都是自定义的(全局变量)

Source code(这个解答题作者的源程序,代码老多了,我没看进去,就是找答案然后炒下来了。你最好看看)

 

posted @ 2020-12-18 15:41  幽香飞狐  阅读(963)  评论(0)    收藏  举报
Live2D