在imageview中查找坐标?

时间:2012-08-09 22:32:43

标签: android imageview coordinates

我试图找到坐标,用户只在图像视图内触摸。

到目前为止,我已通过以下代码设法完成此操作:

    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,这意味着它只能在每次触摸后找到坐标,我想要做的是创建它,以便在用户用手指围绕图像视图时不断找到坐标。我通过以下代码在整个屏幕上实现了这个目标:

@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中使用此代码。

enter image description here

1 个答案:

答案 0 :(得分:2)

问题是你在onTouch事件结束时return false

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

true返回onTouchEvent,只要手指握在屏幕上,您就会继续收到一系列事件,并且只要手指被释放就会继续接收。

相关问题