【问题标题】:Getting x and y co-ordinates of graph while touch in android在android中触摸时获取图形的x和y坐标
【发布时间】:2020-02-04 13:52:04
【问题描述】:

我想在 Android 中的图表上画一条线。单击图形的特定部分时,我无法获得图形的特定 x 和 y 坐标。我怎样才能找回它们?

【问题讨论】:

  • 您好,请提供更多信息,例如您的图形是任何自定义库还是只是您使用画布创建的,如果您使用任何侦听器或不使用一些代码 sn-p,那么我们可以提供帮助。

标签: android canvas graph drawing coordinates


【解决方案1】:

您可以使用 MotionEvent 获取 x 和 y 坐标

@Override
    public boolean onTouchEvent(MotionEvent event)
    {
          int x = (int)event.getX();
          int y = (int)event.getY();

          return false;
    }

【讨论】:

    【解决方案2】:

    使用 View.getLocationOnScreen(int[]) 获取图表相对于其父/屏幕的 X 和 Y。

      `int[] posXY = new int[2];
        myView.getLocationOnScreen(posXY);
        int x = posXY[0];
        int y = posXY[1];`
    

    【讨论】:

    • @Gowtham 您能否发布一些代码以便更好地理解该问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多