【问题标题】:How to determine whether or not user touch the oval in canvas?如何确定用户是否触摸画布中的椭圆?
【发布时间】:2013-04-02 04:00:59
【问题描述】:

我通过以下代码在画布上画了一个椭圆,效果很好:

Paint paint = new Paint();
canvas.drawOval(new RectF(10, 10, 300, 100), paint);

当用户点击屏幕时,如何判断用户是否在椭圆内点击?

注意事项: 当我绘制一个 Rect 时,我可以使用 Rect.contains(int x, int y) 来确定用户是否在其中点击。但现在我正在画一个椭圆。

提前致谢!

【问题讨论】:

    标签: android canvas


    【解决方案1】:

    当您触摸屏幕时,您将获得xy 坐标。您也可以知道中心或椭圆形。

    x, y 是触摸时的坐标,center_xcenter_y 是椭圆中心的坐标。 R是半径。

    float dx = Math.abs(x-center_x);
    float dy = Math.abs(y-center_y);
    float R = radius ;//radius of circle.
    
    boolean checkDistance(float dx,float dy,float R)
    {
    if(dx>R)
    {
    return false;//outside
    }
    else if(dy>R)
    {
    return false;//
    }
    else
    {
    return true;
    }
    }
    

    实际上,此解决方案适用于一个圆圈,但您可以使用它获得一个很好的近似值,或者可能会敞开心扉为您的目的更改此代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      相关资源
      最近更新 更多