Android:如果图像视图按矩阵缩放,如何检测 ImageView 上的触摸位置? [英] Android: how to detect touch location on ImageView if the image view is scaled by matrix?

查看:27
本文介绍了Android:如果图像视图按矩阵缩放,如何检测 ImageView 上的触摸位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了 ImageView 的 OnTouchListener 并实现了 onTouch 方法,但是如果使用矩阵缩放图像,我如何计算触摸的位置?

I set OnTouchListener of an ImageView and implement onTouch method, but if the image is scaled using matrix, how do I calculate the location of the touch?

或者在返回 getX()getY() 时,运动事件会自动考虑到这一点吗?

Or does the motion event automatically takes that into account when returning getX() and getY() ?

推荐答案

getXgetY 将返回 ImageView 坐标系中的触摸位置.如果您正在图像坐标系内寻找点,您可以使用 ImageView 使用的矩阵的逆矩阵.我做了类似以下的事情:

getX and getY will return the touch location in the ImageView's coordinate system. If you're looking for the point within the image's coordinate system, you can use the inverse matrix of the matrix used by the ImageView. I've done something like the following:

// calculate inverse matrix
Matrix inverse = new Matrix();
imageView.getImageMatrix().invert(inverse);

// map touch point from ImageView to image
float[] touchPoint = new float[] {event.getX(), event.getY()};
inverse.mapPoints(touchPoint);
// touchPoint now contains x and y in image's coordinate system

这篇关于Android:如果图像视图按矩阵缩放,如何检测 ImageView 上的触摸位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆