Android:如何确定触摸事件是否在一个圆圈内?

Android:如何确定触摸事件是否在一个圆圈内?,android,touch,geometry,Android,Touch,Geometry,我想在触摸圆形区域时播放媒体,但如何确定我的触摸位置在圆形区域中 到目前为止,我扩展了一个视图并实现了onTouchEvent,我需要一个算法来确定位置是在圆内还是圆外。您应该使用并获得左上角的x和y,同时假设您知道半径(或能够获取视图的宽度/高度以确定半径)。然后,使用和获取xTouch和yTouch,并检查是否: double centerX = x + radius; double centerY = y + radius; double distanceX = xTouch - cent

我想在触摸圆形区域时播放媒体,但如何确定我的触摸位置在圆形区域中


到目前为止,我扩展了一个
视图
并实现了
onTouchEvent
,我需要一个算法来确定位置是在圆内还是圆外。

您应该使用并获得左上角的
x
y
,同时假设您知道半径(或能够获取视图的宽度/高度以确定半径)。然后,使用和获取
xTouch
yTouch
,并检查是否:

double centerX = x + radius;
double centerY = y + radius;
double distanceX = xTouch - centerX;
double distanceY = yTouch - centerY;

boolean isInside() {
    return (distanceX * distanceX) + (distanceY * distanceY) <= radius * radius;
}
双中心x=x+半径;
双中心y=y+半径;
双距离X=X触摸-中心X;
双距离Y=Y接触-中心;
布尔isInside(){

return(distanceX*distanceX)+(distanceY*distanceY)另一种方法是使用两点之间的距离公式,并将该距离与半径进行比较。如果计算的距离小于半径,则触摸位于圆内

这里是代码

// Distance between two points formula
float touchRadius = (float) Math.sqrt(Math.pow(touchX - mViewCenterPoint.x, 2) + Math.pow(touchY - mViewCenterPoint.y, 2));

if (touchRadius < mCircleRadius)
{
    // TOUCH INSIDE THE CIRCLE!
}
//两点间距离公式
float-touchRadius=(float)Math.sqrt(Math.pow(touchX-mViewCenterPoint.x,2)+Math.pow(touchY-mViewCenterPoint.y,2));
if(接触半径
非常感谢,请帮我一个忙。有人能解释这个逻辑吗?它工作得很好,只是如果能解释一下就更好了logic@JoeMaher添加了有关几何体解释的更多详细信息