我该如何正确翻译像素坐标机器人帆布坐标? [英] How do I correctly translate pixel coordinates to canvas coordinates in Android?

查看:253
本文介绍了我该如何正确翻译像素坐标机器人帆布坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我捕捉 MotionEvent 的长按在Android SurfaceView 使用 GestureListener 。然后,我需要将 MotionEvent 的坐标转换为画布上的坐标,从中我可以生成自定义地图坐标(不是谷歌地图)。

I am capturing a MotionEvent for a long click in an Android SurfaceView using a GestureListener. I then need to translate the coordinates of the MotionEvent to canvas coordinates, from which I can generate custom map coordinates (not Google Maps).

从我已阅读,我把给定 MotionEventè e.getX() e.getY()得到像素坐标。我怎样才能将这些坐标到 SurfaceView 的画布坐标?

From what I have read, I take that given MotionEvent e, e.getX() and e.getY() get pixel coordinates. How can I convert these coordinates to the SurfaceView's canvas coordinates?

下面是我的 GestureListener 听长点击:

/**
* Overrides touch gestures not handled by the default touch listener
*/
private class GestureListener extends GestureDetector.SimpleOnGestureListener {

  @Override
  public void onLongPress(MotionEvent e) {
     Point p = new Point();
     p.x =  (int) e.getX();
     p.y = (int) e.getY();
     //TODO translate p to canvas coordinates
  }
}

在此先感谢!

Thanks in advance!

编辑: 这是否都与屏幕尺寸/分辨率/深度和画布矩形对象?

Does this have to do with screen size/resolution/depth and the canvas' Rect object?

推荐答案

如果我理解正确的话里面有一个surfaceview画布视图。如果是这样 尝试 VIEW.getLeft()|共达()返回左|相对于它的视图的顶部位置的父。

If i understand correctly you have a canvas View inside surfaceview. If so try VIEW.getLeft() | getTop() that returns the left | top position of the view relative to it's parent.

float x= e.getX() - canvasView.getLeft();
float y= e.getY() - canvasView.getTop();

这篇关于我该如何正确翻译像素坐标机器人帆布坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆