Android:如何获取图像/图像视图中的x-y坐标?

Android:如何获取图像/图像视图中的x-y坐标?,android,Android,我的屏幕上有一个图像视图。图像占据了大约一半的屏幕,这意味着您可以单击图像和图像侧面 当我点击图像时,如何获得屏幕上的x和y坐标 我尝试使用onTouchEvent,但它仅在我单击图像外部时调用,而不是在单击图像时调用 问候,, Mattias使用ImageView上的OnClickListener接收单击事件,或从ImageView中提取on类并覆盖函数public void onTouchMotionEvent evt以从此视图获取X/Y坐标。您可以使用 另外,我的也会很有用。我最终没有在I

我的屏幕上有一个图像视图。图像占据了大约一半的屏幕,这意味着您可以单击图像和图像侧面

当我点击图像时,如何获得屏幕上的x和y坐标

我尝试使用onTouchEvent,但它仅在我单击图像外部时调用,而不是在单击图像时调用

问候,,
Mattias

使用ImageView上的OnClickListener接收单击事件,或从ImageView中提取on类并覆盖函数public void onTouchMotionEvent evt以从此视图获取X/Y坐标。

您可以使用


另外,我的也会很有用。

我最终没有在ImageView上设置任何OnClickListener。相反,我只在TouchMotionEvent事件上实现public void,在WindowFocusChangedBoolean上实现public void的焦点如下:

private int fieldImgXY[] = new int[2];

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    // Use onWindowFocusChanged to get the placement of
    // the image because we have to wait until the image
    // has actually been placed on the screen  before we
    // get the coordinates. That makes it impossible to
    // do in onCreate, that would just give us (0, 0).
    fieldImage.getLocationOnScreen(fieldImgXY);
    Log.i(LOG_TAG, "fieldImage lockation on screen: " + 
            xyString(fieldImgXY[0], fieldImgXY[1]));
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        Log.i(LOG_TAG, "touch event - down");

        int eventX = (int) event.getX();
        int eventY = (int) event.getY();
        Log.i(LOG_TAG, "event (x, y) = " + xyString(eventX, eventY));

        int xOnField = eventX - fieldImgXY[0];
        int yOnField = eventY - fieldImgXY[1];
        Log.i(LOG_TAG, "on field (x, y) = " + xyString(xOnField, yOnField));
    }
    return super.onTouchEvent(event);
}

我似乎只知道图像在屏幕上的位置。无论我在图像的何处单击,我都会返回0,205,这是图像在屏幕上左上角的位置。如何获取坐标,但在单击事件之外,因为我得到0。我最终没有在ImageView上设置任何OnClickListener。相反,我只在TouchMotionEvent事件中实现public void,如下所示:我解决了问题,但由于本网站的政策,我无法在接下来的6小时内提交我自己问题的答案。我稍后会发布解决方案。你能发布xyString方法吗?声明'x必须是@Manikandan的应用程序FC:我认为应该是这样的:公共字符串xyStringint x,int y{return x:+String.valueOfx+y:+String.valueOfy;}
private int fieldImgXY[] = new int[2];

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    // Use onWindowFocusChanged to get the placement of
    // the image because we have to wait until the image
    // has actually been placed on the screen  before we
    // get the coordinates. That makes it impossible to
    // do in onCreate, that would just give us (0, 0).
    fieldImage.getLocationOnScreen(fieldImgXY);
    Log.i(LOG_TAG, "fieldImage lockation on screen: " + 
            xyString(fieldImgXY[0], fieldImgXY[1]));
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        Log.i(LOG_TAG, "touch event - down");

        int eventX = (int) event.getX();
        int eventY = (int) event.getY();
        Log.i(LOG_TAG, "event (x, y) = " + xyString(eventX, eventY));

        int xOnField = eventX - fieldImgXY[0];
        int yOnField = eventY - fieldImgXY[1];
        Log.i(LOG_TAG, "on field (x, y) = " + xyString(xOnField, yOnField));
    }
    return super.onTouchEvent(event);
}
imageview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                }
                return false;
            }
        });