I have a FrameLayout which holds an ImageView (SVG). 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.
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.
Apply matrix transformation (scale, translate) on the point we got from step 1. Now we know the coordinates inside the scaled/translated 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:
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 :
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 :
mt.reset() mt.postScale(1f, 1f) mt.postTranslate(0f, 0f) val arrayPointOriginal = floatArrayOf(arrayPoint, arrayPoint) mt.mapPoints(arrayPointOriginal)
But that doesn't work for some reason. The coordinates I get from
arrayPointOriginal are wrong. What have I missed?