Android ImageView - Get coordinates of tap (click) regardless of scroll location or zoom scale(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 - 无论滚动位置或缩放比例如何,都获取点击(点击)的坐标
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01