2011-05-18 12 views

回答

39

getXgetY將ImageView的座標系中返回觸摸位置。如果您正在尋找圖像座標系中的點,則可以使用ImageView使用的矩陣的逆矩陣。我已經做了類似如下:

// 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 
0

嘗試使用getRawX()和getRawY()。請注意,這不會根據視圖的大小進行調整,因此如果應用程序不是全屏,則可能需要進行偏移。

6

Zorgbargle的答案是正確的,但還有另一種考慮,當從資源文件夾中加載圖像,這就是設備的密度。

的器件密度

的Android規模圖像的基礎,所以你如果你只有在MDPI文件夾的形象,你也必須由密度分割點找到圖像上的實際點:

float[] point = new float[] {event.getX(), event.getY()}; 

Matrix inverse = new Matrix(); 
imageView.getImageMatrix().invert(inverse); 
inverse.mapPoints(point); 

float density = getResources().getDisplayMetrics().density; 
point[0] /= density; 
point[1] /= density; 
相關問題