Android:如何检查路径是否包含接触点? [英] Android: How to check if a path contains touched point?
问题描述
我将尝试开发可在其中绘制平面图的应用程序.因此,每个房间都有自己的 ID 或名称,如果我触摸一个房间,我想显示带有该ID或名称的Toast消息.问题是如何检查是否碰触了哪个路径!
i would try to develop an application in which i can draw a planimetry. So, each room has got its own ID or name and, if i touch a room, i want to show a Toast Message with that ID or name. The problem is how check if and which path is touched!!
我看到了很多讨论此问题的主题讨论.有人说要使用 getBounds 方法,然后包含用于检查触摸点是否在Rect中的方法.但是,我猜想getBounds方法返回包含路径的最小Rect,对吧?
I saw a lot of topic discussions that talked about this problem. Someone says to use getBounds method and, after, contains method for checking if touched point is in Rect. But, i guess getBounds method returns the smallest Rect that contains path, right?
因此,房间具有不同的自定义几何形状,因此,如果我受到2个封闭房间的限制,则方法可能会返回一组共享的点.坏的!每个房间只有他们的面积点.我该如何解决这个问题?
So, rooms have different custom geometric forms and, for this reason, if i get bounds about 2 close rooms, method could return a shared set of points. Bad! Each room has got only their area points. How can i solve this problem ?
在iOS中,我可以使用 PathContainsPoint 方法,但是不幸的是,Android Path没有类似的东西.
In iOS i could use PathContainsPoint method, but, unfortunally, Android Path doesn't have something similar.
我希望有人能帮助我 提前谢谢
I hope someone can help me Thx in advance
推荐答案
好,我解决了我的问题.我发布了示例代码:
Ok i solved my problem. I post the example code:
Path p;
Region r;
@Override
public void onDraw(Canvas canvas) {
p = new Path();
p.moveTo(50, 50);
p.lineTo(100, 50);
p.lineTo(100, 100);
p.lineTo(80, 100);
p.close();
canvas.drawPath(p, paint);
RectF rectF = new RectF();
p.computeBounds(rectF, true);
r = new Region();
r.setPath(p, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));
}
public boolean onTouch(View view, MotionEvent event) {
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
Log.d(TAG, "point: " + point);
if(r.contains((int)point.x,(int) point.y))
Log.d(TAG, "Touch IN");
else
Log.d(TAG, "Touch OUT");
return true;
}
这篇关于Android:如何检查路径是否包含接触点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!