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

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

问题描述

背景: 我有,我已经修改为可滚动(拖)和缩小能(双指缩放)的ImageView的。我曾经在你好,Android的第三版书中提到的具体技术,也可以找到<一href="http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2/1747?tag=content;siu-container">here.这种技术使用矩阵变换同时处理滚动和缩放。

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

由于这些来源: <一href="http://stackoverflow.com/questions/4933612/how-to-convert-coordinates-of-the-image-view-to-the-coordinates-of-the-bitmap">source 1 和<一href="http://stackoverflow.com/questions/5286174/getting-coordinates-and-width-height-from-a-matrix">source 2

Thanks to these sources: source 1 and source 2

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

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