I have a FrameLayout which holds an ImageView (SVG).
(我有一个持有ImageView(SVG)的FrameLayout。)
when I pinch-to-zoom and drag the image, I essentially perform Scale and Translate on the image view. (当我捏缩放并拖动图像时,实际上是在图像视图上执行“缩放”和“平移”。)
Now, when user taps on the screen, I need to know the coordinates of that touch inside the ImageView in original mode (not scaled/translated), because I want to map this point to some Bitmap that I store which is a copy of the imageview that is displayed.
(现在,当用户点击屏幕时,我需要知道原始模式(未缩放/转换)的ImageView中触摸的坐标,因为我想将此点映射到我存储的某些位图,这是显示的imageview。)
Theoretically, in order to achieve that what I tried to do is:
(从理论上讲,为了实现这一目标,我要做的是:)
Take the touch event from the FrameLayout, and translate it to coordinates in the ImageView.
(从FrameLayout中获取touch事件,并将其转换为ImageView中的坐标。)
Apply matrix transformation (scale, translate) on the point we got from step 1. Now we know the coordinates inside the scaled/translated ImageView.
(在从步骤1得到的点上应用矩阵变换(缩放,平移)。现在我们知道缩放/平移的ImageView内部的坐标。)
- Now I need to apply some sort of "reverse" tranformation to an original state (unscaled, untranslated) in order to get the point on the original image drawable.
(现在,我需要对原始状态(未缩放,未平移)进行某种“逆向”转换,以获取可绘制原始图像上的点。)
So for step 1:
(所以对于步骤1:)
val ratioX = width / it.width
val ratioY = height / it.height
val touchX = e.getX(pointerIndex) / ratioX
val touchY = e.getY(pointerIndex) / ratioY
Then for step 2 :
(然后对于步骤2:)
val mt = Matrix()
mt.reset()
mt.postScale(scale, scale)
mt.postTranslate(translationX, translationY)
val arrayPoint = floatArrayOf(touchX, touchY)
mt.mapPoints(arrayPoint)
Then for step 3 :
(然后对于步骤3:)
mt.reset()
mt.postScale(1f, 1f)
mt.postTranslate(0f, 0f)
val arrayPointOriginal = floatArrayOf(arrayPoint[0], arrayPoint[1])
mt.mapPoints(arrayPointOriginal)
But that doesn't work for some reason.
(但这由于某些原因不起作用。)
The coordinates I get from arrayPointOriginal
are wrong. (我从arrayPointOriginal
获得的坐标是错误的。)
What have I missed? (我错过了什么?)
ask by BVtp translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…