2012-09-25 132 views
0

我想實現這個方法:如何獲取ImageView的4個座標?

//#OnTouchEvent() 
public boolean onTouchEvent(MotionEvent event) { 
//Coordinates of the touch 
int x = (int) event.getX(); 
int y = (int) event.getY(); 

switch(event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
    // Check if the touch is within the coordinates of an image 
      if x isBetweenCoordinatesXofTheImage 
       if y isBetweenCoordinatesYofTheImage 
        //DoSomething 
      else 
       //DoNothing 
    break; 
    case MotionEvent.ACTION_MOVE: 
      //nothing 
    break; 
    case MotionEvent.ACTION_UP: 
    //check if the touch is within the coordinates of an image; 
      if x is betweenCoordinatesX of the image 
       y is betweenCoordinatesYofTheImage 
        //DoSomething 
      else 
       //DoNothing 
    break; 
    } 
    return true; 
} 

的ImageView的有4個coorners,我需要知道的4點座標(X,Y)之前做檢查。 左上角的X座標與左下角相同,右上角的X座標與右下角相同。對於Y座標是相似的。 我的問題是:我如何獲得ImageView的4個座標?

感謝大家!

+0

我知道我錯過了一些東西。 x,y,x +寬度,y +高度 – Simon

+1

爲什麼不在您的imageview上使用view.ontouchlistener? – njzk2

+0

我知道這種方法,但我需要按照我指出的方式來做。我需要在兩個圖像之間進行匹配移動。我需要按下屏幕並將手指拖到下一張圖像上,而不用擡起手指。如果我使用你的方法,方法OnTouchEvent不會執行。謝謝! – Adrian

回答

2

只是一個小問題: 爲什麼不把它設置爲觸摸監聽器? 如果這不是一個選項,然後嘗試:

int pos[] = new int[2]; 
yourImageView.getLocationOnScreen(pos); 

你會調用這個方法的數組將包含您的視圖的左上角座標後。 要獲得其他座標,只需使用視圖的高度和寬度:

yourImageView.getWidth(); 
yourImageView.getHeight(); 

確認後,方可觀點繪製在屏幕上被稱爲這三個方法 - (調用它們在OnCreate將返回0所有值)。

+0

ooohh !!謝謝!!!這似乎是我需要的! – Adrian

+0

我不想使用觸摸監聽器,因爲我需要實現「匹配」運動,而且我必須按下屏幕,拖動手指並將其從屏幕上擡起。 – Adrian

+0

並感謝您的最後評論,因爲我有你說的問題。你是我的救星hahahaha – Adrian

0

嘗試

getLocationOnScreen() 

和/或

getLocationInWindow() 

您ImageView的。