Android 中的屏幕和视图坐标

发布于 2024-10-11 12:38:37 字数 423 浏览 3 评论 0原文

如果用户触摸屏幕,我可以使用 MotionEvent getX() 和 getY() 方法获取此触摸的坐标。

但如果我理解正确的话,将使用其他坐标来绘制画布。

我同事的意思是,这些是视图和屏幕坐标。但他不知道,是否有一些转化的方法。

也许你可以帮助我。

预先感谢您,

Mur

UPD

我有点困惑。我正在使用 osmdroid,他们正在将叠加层绘制为画布。

因此,如果我将它们用于绘图的画布坐标与 MotionEvent 坐标进行比较,我什么都不懂。

这些是我打印出来的矩形坐标(左、右、下、上)16899.0 / 16948.0 -86483.0 / -86515.0。

MotionEvent 的坐标为 252.0 / 223.0

If user touchs the screen I can get coordinates of this touch by using methods of MotionEvent getX() and getY().

But if I understood it right for drawing of canvas other coordinates will be used.

My colleague meant, these are view and screen coordinates. But he didn't know, if there are some transformations methods.

Maybe you can help me.

Thank you in advance,

Mur

UPD

I've got a bit confusion. I'm using osmdroid, they are drawing overlays as canvas.

So if I'm comparing canvas coordinates they are using for drawing with MotionEvent coordinates I don't understand anything.

These are coordinates of rectangle (left, right, bottom, top) 16899.0 / 16948.0 -86483.0 / -86515.0 I've printed out.

Coordinates of MotionEvent are 252.0 / 223.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北笙凉宸 2024-10-18 12:38:37

他错了。 Canvas 没有使用任何不同的东西。您只需考虑画布在屏幕上相对于用户触摸位置的位置。

He is wrong. Canvas doesn't use anything different. You just have to consider where the canvas is located on the screen in relation to where the user touched.

郁金香雨 2024-10-18 12:38:37

如果您在画布上的位置 0,0 处绘制,您将在左上角绘制。所以你不需要做任何特别的事情。

也许您的朋友对屏幕/窗口位置有点困惑:http://developer.android.com/reference/android/view/View.html#getLocationInWindow(int[])

If you draw at location 0,0 on your canvas you will draw in the top left corner. So you don't have to do anything special.

Perhaps your friend has got a bit confused with screen/window locations: http://developer.android.com/reference/android/view/View.html#getLocationInWindow(int[])

北斗星光 2024-10-18 12:38:37

这些是我打印出来的矩形坐标(左、右、下、上)16899.0 / 16948.0 -86483.0 / -86515.0。

MotionEvent 的坐标为 0/0 到 252 / 223

看起来 osmdroid 正在转换坐标。这对于地图来说是有意义的,所以如果你在 0,0 处绘制一些东西,你就会在你的位置绘制一些东西。

我认为返回的坐标是地图坐标,从位置 (0,0) 开始,因为图像 16899.0+86483.0=103382.0 像素高和 16948.0+86515.0=103463.0 像素宽是 10200 gigapixel 的图像,消耗近 30 Tb 的内存,并且我我怀疑智能手机是否可以有如此多的可用内存......

因此,对于您的应用程序,我建议采用如下方法:

touch = touch coordinates
scale = map scale
top = top left of the map

point_on_map = top + (touch*scale)

These are coordinates of rectangle (left, right, bottom, top) 16899.0 / 16948.0 -86483.0 / -86515.0 I've printed out.

Coordinates of MotionEvent are 0/0 to 252 / 223

Looks like osmdroid is trasforming coordinates. This makes sense for a map, so if you draw something at 0,0 you will draw something at your location.

I suppose returning coordinates are map coordinates, starting from location (0,0) because an image 16899.0+86483.0=103382.0 pixel tall and 16948.0+86515.0=103463.0 pixel wide is an image of 10200 gigapixel, consuming almost 30 Tb of memory, and I am in doubt that a smarphone can have such a lot of memory free...

So for your app I suggest an approach like this:

touch = touch coordinates
scale = map scale
top = top left of the map

point_on_map = top + (touch*scale)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文