简体   繁体   English

Android:MotionEvent的x和y像素不会转换为图像坐标

[英]Android: MotionEvent's x and y pixel do not translate to image coordinates

I am using the getX() and getY() functions in MotionEvent to get the x and y positions of where the user touched the screen. 我在MotionEvent中使用getX()和getY()函数来获取用户触摸屏幕的x和y位置。 I am also capturing images using the android camera. 我也在使用Android相机捕获图像。 These images are 480x640. 这些图像是480x640。 My intention is to use have the user draw a box and the get the start and end x,y positions and use them to create a box and crop the image. 我的意图是使用户绘制一个框并获取起始和终止x,y位置,并使用它们创建一个框并裁剪图像。 Unfortunately, even if the user boxes the correct area using the android screen (which shows video from the camera), the coordinates from MotionEvent do not relate at all to the correct location on the image. 不幸的是,即使用户使用android屏幕(显示来自摄像机的视频)对正确的区域进行了框选,MotionEvent的坐标也完全不与图像上的正确位置相关。 Sometimes the x,y coordinates I get from the MotionEvent are actually more than 480 or 640. Are the dimensions of the android screen different than the image's? 有时我从MotionEvent获得的x,y坐标实际上大于480或640。android屏幕的尺寸是否不同于图像的尺寸? Or do the x,y positions from MotionEvent correspond to something else 还是MotionEvent的x,y位置对应于其他东西

The coordinates returned depend on the pointer being used but in general are in screen coordinates, so to get the coordinates relative from the view you need to offset by the views position. 返回的坐标取决于所使用的指针,但通常在屏幕坐标中,因此要从视图获取相对坐标,需要将视图位置偏移。

See: 看到:

http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_X http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_X

For offset of the view use: 对于视图的偏移量,请使用:

https://developer.android.com/reference/android/view/View.html#getLeft() https://developer.android.com/reference/android/view/View.html#getTop() https://developer.android.com/reference/android/view/View.html#getLeft() https://developer.android.com/reference/android/view/View.html#getTop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM