android - 如何判断闭合路径是否包含给定点?

标签 android graphics

在 Android 中,我有一个 Path 对象,我碰巧知道它定义了一个闭合路径,我需要弄清楚给定点是否包含在路径中。我所希望的是

path.contains(int x, int y)

但这似乎不存在。

我正在寻找这个的具体原因是因为我在屏幕上有一组形状定义为路径,我想弄清楚用户点击了哪个。如果有更好的方法来解决这个问题,例如使用不同的 UI 元素而不是自己“艰难地”做,我愿意接受建议。

如果必须的话,我愿意自己编写算法,但我想这意味着不同的研究。

最佳答案

这是我所做的并且似乎有效:

RectF rectF = new RectF();
path.computeBounds(rectF, true);
region = new Region();
region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));

现在您可以使用 region.contains(x,y) 方法。

Point point = new Point();
mapView.getProjection().toPixels(geoPoint, point);

if (region.contains(point.x, point.y)) {
  // Within the path.
}

** 2010 年 6 月 7 日更新 ** 如果 rectF 太大,region.setPath 方法将导致我的应用程序崩溃(没有警告消息)。这是我的解决方案:

// Get the screen rect.  If this intersects with the path's rect
// then lets display this zone.  The rectF will become the 
// intersection of the two rects.  This will decrease the size therefor no more crashes.
Rect drawableRect = new Rect();
mapView.getDrawingRect(drawableRect);

if (rectF.intersects(drawableRect.left, drawableRect.top, drawableRect.right, drawableRect.bottom)) {
   // ... Display Zone.
}

关于android - 如何判断闭合路径是否包含给定点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2597590/

相关文章:

java - 用drawstring()写入一个变量

java - android - 带有随机图片的动画

android - Button button = findViewById(R.id.button) 在 Android 中总是解析为 null

android - 在 Android 位图上绘制透明文本

java - android中表单程序生成的布局到XML布局

java - android studio(java) 使用字符串查找 View id

python - 如何简化这个很长的 if 语句?

c# - C# 本身是否使用 GPU 进行图形处理?

android - 如何实现 Android 日历应用程序的 Day-View?

android - 用于显示应用程序加载进度的 UIImageView