简体   繁体   English

Android:如何获取图像/ ImageView中的xy坐标?

[英]Android: How do I get the x y coordinates within an image / ImageView?

I have an image (ImageView) on my screen. 我的屏幕上有一个图像(ImageView)。 The image takes up roughly half of the screen which means that you can click both on the image and on the side of the image. 图像大约占据屏幕的一半,这意味着您可以同时单击图像和图像的侧面。

How to I get the x -and y coordinates on the screen when I click on the image? 单击图像时如何在屏幕上获取x和y坐标?

I tried using onTouchEvent but it is only called when I click outside of the image, not when I click on the image. 我尝试使用onTouchEvent,但仅在单击图像外部时才会调用它,而在单击图像时不会调用它。

Regards, Mattias 问候,Mattias

You can use getLocationOnScreen() 您可以使用getLocationOnScreen()

imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            int[] values = new int[2]; 
            view.getLocationOnScreen(values);
            Log.d("X & Y",values[0]+" "+values[1]);
        }
    });

Also, this answer of mine will be useful as well. 同样,我的这个答案也将是有用的。

I ended up not setting any OnClickListener on the ImageView at all. 我最终根本没有在ImageView上设置任何OnClickListener。 Instead I only implement "public void onTouch(MotionEvent event)" and "public void onWindowFocusChanged(boolean hasFocus)" like this: 相反,我只实现“ public void onTouch(MotionEvent event)”和“ public void onWindowFocusChanged(boolean hasFocus)”,如下所示:

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;
            }
        });

在ImageView上使用OnClickListener来接收单击事件,或从ImageView中抽象on类并覆盖函数“ public void onTouch(MotionEvent evt)”以从该视图获取X / Y坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM