Android ImageView - 无论滚动位置或缩放比例如何,都可获取点击(点击)坐标

And*_*dyB 11 android matrix coordinates imageview

背景: 我有一个ImageView,我已经修改为可滚动(拖动)和缩放(缩放缩放).我使用了"Hello,Android"第3版中提到的确切技术,也可以在这里找到.此技术使用矩阵变换来处理滚动和缩放.

我的问题: 当用户点击图像时,无论图像如何滚动或放大,我都希望该拍子的坐标与图像本身相关.例如,如果我的图像是1000x2000,我滚动并缩放图像.然后我在某个点点击图像,我想知道与1000x2000相关的那个点,而不仅仅是屏幕区域.我怎样才能做到这一点?

And*_*dyB 32

我找到了一个解决方案,使用了我在本网站上的其他问题拼凑而成的信息.为了回馈社区,我认为分享我学到的东西是正确的.希望这有助于某人:

// Get the values of the matrix
float[] values = new float[9];
matrix.getValues(values);

// values[2] and values[5] are the x,y coordinates of the top left corner of the drawable image, regardless of the zoom factor.
// values[0] and values[4] are the zoom factors for the image's width and height respectively. If you zoom at the same factor, these should both be the same value.

// event is the touch event for MotionEvent.ACTION_UP
float relativeX = (event.getX() - values[2]) / values[0];
float relativeY = (event.getY() - values[5]) / values[4];
Run Code Online (Sandbox Code Playgroud)

感谢这些来源: 来源1来源2

  • "希望这有助于某人" - 绝对 (3认同)