2012-10-01 98 views

回答

2

您无法直接获取位图的坐标。你需要自己计算。 使用ImageView的位置,并且可以处理它。

当然,当您在Activity onCreate之后,您可以访问任何虚增(活动)视图参数。 例如。 ImageView a; a.getPaddingBottom(); 像所有的协调员(左右等...) 在此之后,你需要ImageView的高度和宽度。那么,当你知道意见的位置时,你可以计算出你的答案和宽度。

例子:

final ImageView iv_YourImage = (ImageView) findViewById(R.id.iv_imageview); 
public boolean onTouch(View v, MotionEvent event){ 
int topParam = iv_YourImage.getPaddingTop(); 
int rightParam = iv_YourImage.getPaddingRight(); 
int maxTopParam = topParam+iv_YourImage.getMaxHeight(); 
int maxRightParam = rightParam + iv_YourImage.getMaxWidth(); 
if(event.getX>topParam&&event.getX<maxTopParam){ 
    //the x coordinate is in your image... do the same to Y 
} 
return true; 
} 
+1

你能解释一下吗...... – URAndroid

0

看看如果答案是太短了下面的代码:

Android: Detect touch on actual image in ImageView

的代码替换这个答案的填充和最大高度编码,并替换它使用getWidth()和getHeight()显示图像的实际宽度和高度。并使用屏幕宽度和高度来计算其位置。如果你想要更详细地解答你的问题,你应该提供更多的信息,比如你在哪里绘制位图...你是在活动XML中定义它,还是在随机位置绘制它,例如X和Y坐标触摸事件的答案取决于该信息。

package com.example.img; 

imports.... 

public Image extends Activity { 

ImageView imgYourImage; 
// int imgWidth; 
// etc... 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_image); 

    imgYourImage = (ImageView) findViewById(R.id.yourImage); 
    imgDimension(); 
} 

private void imgDimension() { 
    imgWidth = imgYourImage.getWidth(); 
    // at newer api's you can use getLeft() and getTop() of imageView 
    // if getLeft() works you have the leftX coordinate of image already. 
    // do same with height 
    // also get the screen Width and Height by display.getWidth() (depreciated) 
    // Or the newer code display.getSize() 
    leftX = (screenwidth - imgWidth)/2; 
    rightx = leftx + imgWidth; 
} 

请注意,此代码仅在图像显示在屏幕水平中心时起作用。或者如果图像显示在屏幕的垂直中心,则使用顶部和底部。

相关问题