Android 在imageview中查找坐标?

Android 在imageview中查找坐标?,android,imageview,coordinates,Android,Imageview,Coordinates,我试图找到用户仅在imageview内触摸的坐标 到目前为止,我通过以下代码实现了这一点: img = (ImageView) findViewById(R.id.image); img.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { x = (int) event.getX();

我试图找到用户仅在imageview内触摸的坐标

到目前为止,我通过以下代码实现了这一点:

    img = (ImageView) findViewById(R.id.image);
    img.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            x = (int) event.getX();
            y = (int) event.getY();
            int[] viewCoords = new int[2];
            img.getLocationOnScreen(viewCoords);
            int imageX = (int) (x - viewCoords[0]); // viewCoods[0] is the X coordinate
            int imageY = (int) (y - viewCoords[1]); // viewCoods[1] is the y coordinate
            text.setText("x:" +x +"y"+y);

            return false;
        }
    });
然而,这是一个onTouchListener,这意味着它只能在每次触摸后找到坐标,我想做的是创建它,以便它在用户在imageview上移动手指时不断找到坐标。我通过以下代码在整个屏幕上实现了这一点:

@Override
public boolean onTouchEvent(MotionEvent event) {
    x = (float)event.getX();
    y = (float)event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
    }



    text.setText("x:"+ x +" y:" +y);

return false;
}
但是,我不知道如何使此代码仅在imageview中工作


您的问题是在onTouch事件结束时返回false

当您
返回false
时,您告诉操作系统您不再对与此特定手势相关的任何事件感兴趣,因此它将停止通知您对未来活动的看法(例如动作移动和动作向上)


true
返回到
onTouchEvent
,只要将手指放在屏幕上,您将继续收到一系列事件,并在释放时收到最后一个事件。

非常感谢!这么简单,但你在那里为我节省了很多时间。