2

我已经关闭了由许多 Bezier 段组成的 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 平板电脑模拟器中

4

1 回答 1

4

通常Java代码会导致异常而不是分段错误,这意味着Java虚拟机有问题,除非您的项目中有自己的JNI代码并且导致分段错误。

您可以使用足够大的剪辑矩形将所有可能的路径绑定为剪辑区域,而不是计算路径的边界,这对于您的复杂路径来说似乎过于昂贵的操作,这样您就可以避免调用繁重和不必要的 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");
}
于 2012-10-17T10:23:58.943 回答