尝试从 ImageView 的 onTouchListener 获取位图像素
我正在 ImageView 上显示位图。当点击 ImageView 时,我想获取点击它的位图的像素 x/y 坐标。我已经注册了ImageView的onTouchListener,并且在onTouch方法中,我使用getX和getY来获取触摸的位置。问题是 ImageView 中的图像可能比视图本身大,因此它会缩小以适合屏幕。那么,返回的 x 和 y 坐标是视图的坐标,但不一定是位图上相应像素的坐标。
我可以获得某种比例因子来了解其大小调整了多少吗?或者如果没有,有人可以建议我如何获取我需要的信息吗?最重要的是我可以获得像素坐标 - 如果我必须更改视图类型,那没关系。
另外,有时位图比屏幕小,因此会放大它。在这种情况下,从 MotionEvent 接收到的 x 和 y 可能超出实际位图的范围。
I am displaying a Bitmap on an ImageView. When the ImageView is tapped, I would like to get the pixel x/y coordinates for the Bitmap where it was tapped. I have registered the ImageView's onTouchListener, and within the onTouch method, I use getX and getY to get the location of the touch. The problem is that the image within the ImageView may be larger than the view itself, so it is scaled down to fit the screen. The x and y coordinates returned, then, are the coordinates of the view, but not necessarily the coordinates of the corresponding pixel on the Bitmap.
Can I get some sort of scale factor to know how much it was resized? Or if not, could someone suggest how I could go about getting the information I need? What's most important is that I can get the pixel coordinates - if I have to change the view type, that's alright.
Also, sometimes the bitmap is smaller than the screen, so it scales it up. In this scenario, it is possible that the x and y received from the MotionEvent are outside the bounds of the actual Bitmap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查 ScaleType 属性。您必须根据 getScaleType() 的结果对原始图像进行数学计算,但值得一试。
Check into the ScaleType property. You'll have to do the math on the original image, based on the results of getScaleType(), but it's worth checking out.
我可以遇到同样的情况。该线程已有一年多的历史,但认为它可能对某人有用。在设置为 ImageView 之前我必须缩放位图。
代码如下:
此后,触摸的坐标与位图的坐标相同。
I can across the same case. The thread is more than a year old, but thought it might be useful for someone. I had to scale the bitmap before setting to the ImageView.
Here is the code:
After this, the touched coordinates are the same as the coordinates of the bitmap.