java android中的路径交叉

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11184397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:06:46  来源:igfitidea点击:

Path intersection in android

javaandroidgraphicspathandroid-gesture

提问by V I J E S H

I have 2 path objects in my android code.I have tried all the way to check whether these paths are intersected or not, but not able to do it. How can I check whether the paths are intersected or not. Appreciate any good response, Thanks !

我的android代码中有2个路径对象。我一直尝试检查这些路径是否相交,但无法做到。如何检查路径是否相交。感谢任何好的回应,谢谢!

采纳答案by Dirk

have a look at Region.op

看看Region.op

I haven't tried it but I would suggest to use:

我还没有尝试过,但我建议使用:

Region.setPath(Path path, Region clip);

to get a region from both of your paths and afterwards you can use:

从你的两个路径中获取一个区域,然后你可以使用:

if (region1.op(region2,Region.Op.INTERSECT)) {
  // intersection
}

to check for intersection...

检查交叉点...

回答by Steven McConnon

The answer given by Dheeraj has the answer to your question:

Dheeraj 给出的答案已经回答了您的问题:

https://stackoverflow.com/a/9918830/1268168

https://stackoverflow.com/a/9918830/1268168

Here's a copy and paste of his answer:

这是他的答案的复制和粘贴:

Another method I can think of will work with simple objects that can be constructed using Paths.

我能想到的另一种方法适用于可以使用路径构造的简单对象。

Once you have two objects whose boundaries are represented by paths, you may try this:

一旦你有两个边界由路径表示的对象,你可以试试这个:

Path path1 = new Path();
path1.addCircle(10, 10, 4, Path.Direction.CW);
Path path2 = new Path();
path2.addCircle(15, 15, 8, Path.Direction.CW);

Region region1 = new Region();
region1.setPath(path1, clip);
Region region2 = new Region();
region2.setPath(path2, clip);

if (!region1.quickReject(region2) && region1.op(region2, Region.Op.INTERSECT)) {
    // Collision!
}

Once you have your objects as Paths, you can draw them directly using drawPath(). You can also perform movement by transform()ing the path.

一旦将对象作为路径,就可以直接使用 drawPath() 绘制它们。您还可以通过对路径进行转换()来执行移动。

From my understanding, the variable "clip" in the above code should be the bounding box of the path. For general purposes I use

根据我的理解,上面代码中的变量“clip”应该是路径的边界框。出于一般目的,我使用

Region clip = new Region(0, 0, layoutWidth, layoutHeight);

Where the layout width and height can be the size of your canvas or activity or whatever.

布局宽度和高度可以是画布或活动的大小或其他任何大小。

回答by S.D.

From API 19 onwards, Pathnow has an op()method.

从 API 19 开始,Path现在有一个op()方法。

boolean intersects = path.op(p1,p2,Path.Op.INTERSECT)