背景: 我有一个 ImageView,我已将其修改为可滚动(拖动)和可缩放(捏合缩放)。我使用了“Hello, Android”第 3 版书中提到的确切技术,该书也可以找到 here .该技术使用矩阵变换来处理滚动和缩放。
我的问题: 当用户点击图像时,无论图像如何滚动或放大,我都需要该点击相对于图像本身的坐标。例如,如果我的图像是 1000x2000 并且我滚动和缩放图像。然后我在某个点点击图像,我想知道那个点与 1000x2000 的关系,而不仅仅是屏幕区域。我该怎么做?
最佳答案
我使用从本网站上的其他问题中拼凑出来的一些信息找到了解决此问题的方法。为了回馈社区,我认为分享我学到的东西是正确的。希望这对某人有帮助:
// 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];
关于Android ImageView - 无论滚动位置或缩放比例如何,都获取点击(单击)的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8002298/