如何检查触摸事件是在 android 中的区域之外还是区域之内

How to check if a touch event is outside or inside of a region in android

我有一张篮球场或任何其他球场的图像,我想检查用户检查的是 3 分区域的外部还是内部,我该如何实现?我应该使用 x,y 的预定义点吗?

您可以使用 x 和 y 执行此操作,但请相信我,它很快就会变得非常混乱。我的建议是对不同的区域使用不同的图像视图,并将 onClickListeners 添加到每个区域。为此,您必须为法庭中的每个元素请求不同的资产。

好吧,如果那个区域是一个完美的半圆(我认为是),那么公式就很简单了。无论您喜欢哪种方式,您都可以获得触摸事件的 X 和 Y 坐标(这不是这里的问题,例如使用 GestureDetector)。我将仅举一个 isIn3PointArea() 方法的示例:

private boolean isIn3PointArea(float touchX, float touchY, float centerX, float centerY, float r){
    float x = touchX - centerX;
    float y = touchY - centerY;
    return (touchX > centerX && Math.sqrt(x*x+y*y) < r);
}

解释:

This is the method for the left 3 point area, for the right one you'd just need to swap out > operator with < in the touchX > centerX part. It's quite logical, your point needs to be less than r (which is the radius of your circle) from the center point (which has x coordinate of 0) and y, well whatever you give it, I'm not quite sure what you use to draw the court. Also it needs the touchX to be right of ( greater than > ) the centerX because if it's not, the touch is out of the field. The reverse logic applies to the right 3 point area.

您唯一需要推断的是为该方法提供哪些参数(您没有共享任何代码,所以我无法知道您的半径是多少或球场的坐标是多少)。