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

Android ImageView - Get coordinates of tap (click) regardless of scroll location or zoom scale(Android ImageView - 无论滚动位置或缩放比例如何,都获取点击(点击)的坐标)

本文介绍了Android ImageView - 无论滚动位置或缩放比例如何,都获取点击(点击)的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Background: I have an ImageView that I've modified to be scrollable (drag) and zoom-able (pinch zoom). I used the exact technique mentioned in the "Hello, Android" 3rd edition book that can also be found here. This technique uses matrix transformation to handle both scrolling and zooming.

我的问题:当用户点击图像时,我想要该点击相对于图像本身的坐标,无论图像如何滚动或放大.例如,如果我的图像是 1000x2000 并且我滚动和缩放图像.然后我在某个点点击图像,我想知道那个点与 1000x2000 的关系,而不仅仅是屏幕区域.我该怎么做?

My Problem: When a user taps on the image, I want the coordinates of that tap in relation to the image itself, regardless of how the image has been scrolled or magnified. For instance, if my image is 1000x2000 and I scroll and zoom the image. Then I tap on the image at a certain point, I want to know what that point is in relation to the 1000x2000, not just the screen area. How can I do this?

推荐答案

我使用从本网站上的其他问题拼凑起来的一些信息找到了解决方案.为了回馈社区,我认为分享我学到的东西是正确的.希望这可以帮助某人:

I found a solution to this using bits of info I've pieced together from other questions on this site. To give back to the community, I figured it was only right to share what I learned. Hope this helps somebody:

// 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];

感谢以下来源:来源 1 和 来源 2

这篇关于Android ImageView - 无论滚动位置或缩放比例如何,都获取点击(点击)的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android ImageView - 无论滚动位置或缩放比例如何,都获取点击(点击)的坐标

基础教程推荐