我已经关闭了 Path-s,它由许多贝塞尔线段组成。这些贝塞尔曲线段的整数坐标最大为 5000,5000。我需要计算一个点是否位于这些闭合路径之一内。我使用这段代码:
// p is a Path, bounds is a RectF
p.computeBounds(bounds, true);
Region region = new android.graphics.Region();
region.setPath(path, new android.graphics.Region((int)bounds.left, (int)bounds.top, (int)bounds.right, (int)bounds.bottom));
我在每个路径上执行一次,然后执行
region.contains(x, y);
问题是,computeBounds 使我的大路径的应用程序崩溃。没有强制关闭,它只是收到 SIGSEGV 并返回主屏幕,没有任何消息。我试图将坐标缩小到较小的数字(除以1000),但它没有帮助,程序仍然崩溃。
是否有其他方法可以计算一个点是否位于复杂路径内,并且不会崩溃?
编辑 有没有办法用 RenderScript 来计算这个?我找不到任何带有路径/贝塞尔曲线的 RenderScript 示例...
编辑2 这种情况发生在带有 4.1.1 和 4.1.2 的 Nexus 7 以及 ICS x86 平板电脑模拟器中
最佳答案
通常 Java 代码会导致异常而不是段错误,这意味着 Java 虚拟机出现问题,除非您的项目中有自己的 JNI 代码,并且这会导致段错误。
您可以使用足够大的剪辑矩形将所有可能的路径绑定(bind)为剪辑区域,而不是计算路径的边界,这对于复杂路径来说似乎是太昂贵的操作,这样您就可以避免调用繁重且不必要的 Path.computeBounds。
import android.graphics.Region;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Rect;
private static final String id = "Graphics";
...
Path path = new Path();
/* Initialize path here... */
/* Huge rectangle to bound all possible paths */
Region clip = new Region(0, 0, 10000, 10000);
/* Define the region */
Region region = new Region();
if (region.setPath(path, clip)) {
Log.d(id, "This region is fine");
} else {
Log.e(id, "This region is empty");
}
关于android - 计算路径崩溃的界限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892711/