Android 如何获取ImageView的4个坐标?

Android 如何获取ImageView的4个坐标?,android,android-layout,Android,Android Layout,我想实现这个方法: //#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 i

我想实现这个方法:

//#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个坐标,我需要知道4个坐标(x,y)才能进行检查。 左上角与左下角具有相同的X坐标,右上角与右下角具有相同的X坐标。对于Y坐标是类似的。 我的问题是:如何获得ImageView的4个坐标

谢谢大家

试试看

getLocationOnScreen() 
和/或

getLocationInWindow()
在您的imageView上。

只是一个小问题: 为什么不将ImageView本身设置为触摸式侦听器? 如果这不是一个选项,请尝试:

int pos[] = new int[2];
yourImageView.getLocationOnScreen(pos);
调用此方法后,数组将包含视图的左上角坐标。 要获取其他坐标,只需使用视图的高度和宽度:

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

确保只有在视图在屏幕上绘制之后才调用这三个方法(在onCreate中调用它们将使所有值返回0)。

我知道我遗漏了什么。x、 y,x+宽度,y+高度为什么不是一个视图。在imageview上点击Listener?我知道这个方法,但我需要按照我指示的方式来做。我需要在两个图像之间进行匹配运动。我需要按下屏幕并将手指拖动到下一个图像,而不必抬起手指。如果我使用您的方法,则不会执行OnTouchEvent方法。谢谢我不想使用触摸式监听器,因为我需要执行“匹配”动作,我必须按下屏幕,将手指从屏幕上拖拽并抬起。谢谢你最后的评论,因为我有你说的问题。你是我的救世主,哈哈哈,如果答案是好的,那就太好了。如果您在上一篇博文中遇到了这样的问题:由于尚未绘制视图,您的get 0:0。通过这个坐标+视图的宽度和高度,你可以得到所有四个坐标:(x,y),(x+宽度,y),(x,y+高度),(x+宽度,y+高度)谢谢大家!!!!是的,我点击接受答案,但我不能点击并表明答案非常有用,因为我有足够的声誉,所以很抱歉。我希望其他人也能这样做,因为这对更多人有用。