通过将Imageview左上角视为(0,0)得到Imageview的X,Y坐标

时间:2014-08-02 03:20:54

标签: android coordinates android-imageview ontouchlistener

在我的应用程序中,我使用edges of a Image检测到Android opencv methods。根据得到的坐标(四个坐标),我在四个角上绘制了一条线和圆。这些坐标是针对所选图像返回的(通过将图像的左上角视为(0,0))。我需要添加任务,用户可以通过触摸Imageview中绘制的圆圈来更改OnTouch()中绘制的线条。因为我需要(x,y)在ImageView中选择的触摸部分的坐标,将ImageView左上角视为(0,0)。我使用此代码event.getX()event.getY()获得了触摸部分的x,y坐标,但它返回了基于屏幕的坐标。如果我在ImageView中触摸左上角的圆圈将返回变化的(x,y)坐标,它与我通过边缘检测方法绘制的圆圈不同。

我尝试过的代码

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


    public static  float ptX1 = 0;
    public static float ptY1 = 0;
    public static float ptX2 = 0;
    public static float ptY2 = 0;
    public static float ptX3 = 0;
    public static float ptY3 = 0;
    public static float ptX4 = 0;
    public static float ptY4 = 0;
 /* ptx1,pty1,ptx2,ptY2 ,ptX3,ptY3,ptX4,ptY4     will get values from Opencv returned coordinates */


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

            imageView.getLocationOnScreen(fieldImgXY);

        }

@Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            float x1 = motionEvent.getX();

            float y1 = motionEvent.getY();

             float xOnField = x1 - fieldImgXY[0];
             float yOnField = y1 - fieldImgXY[1];


            if((x1 >= ptX1 - 10 && x1 <= ptX1 + 10)
                    && (y1 >= ptY1 - 10 && y1 <= ptY1 + 10)) {
                bool = true;
                selectedPos = 1;
            }else if((x1 >= ptX2 - 10 && x1 <= ptX2 + 10)
                    && (y1 >= ptY2 - 10 && y1 <= ptY2 + 10)) {
                bool = true;
                selectedPos = 2;
            }
else if((x1 >= ptX3 - 10 && x1 <= ptX3 + 10)
                    && (y1 >= ptY3 - 10 && y1 <= ptY3 + 10)) {
                bool = true;
                selectedPos = 3;
            }


            else if((x1 >= ptX4 - 10  && x1 <= ptX4 + 10)
                    && (y1 >= ptY4 - 10 && y1 <= ptY4 + 10)) {
                bool = true;
                selectedPos = 4;
            }
            else
            {
                bool = false;
                selectedPos = 0;
            }
            return true;
}
else if(motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
//          float x1 = motionEvent.getX();
//          float y1 = motionEvent.getY();
//          Log.i(LOGCAT, "x1=" + x1 + "::y1=" + y1);
            if (bool) {
                bool1 = true;
                bool = false;
            }
            return true;
        } else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
            float x1 = motionEvent.getX();
            float y1 = motionEvent.getY();
            Log.i(LOGCAT, "x1=" + x1 + "::y1=" + y1);
            if (bool1) {
                clearLine();// for clear the canvas and lines in that image.
                if(selectedPos == 1)
                {
                    ptX1 = x1;
                    ptY1 = y1;
                }
                else if(selectedPos == 2)
                {
                    ptX2 = x1;
                    ptY2 = y1;
                }
                else if(selectedPos == 3)
                {
                    ptX3 = x1;
                    ptY3 = y1;
                }
                else if(selectedPos == 4)
                {
                    ptX4 = x1;
                    ptY4 = y1;
                }
                drawLine(); // draw new lines and circles for latest values
                bool1 = false;
            }
            return true;
        }
}

1 个答案:

答案 0 :(得分:1)

我使用 dp = px *(160 / dpi)公式将OnTouch事件motionEvent.getX(),motionEvent.getY()返回的坐标(像素坐标)转换为dp coordinates。现在我可以使用MotionEvent.ACTION_MOVE在屏幕上移动圆圈。

DisplayMetrics dm = getResources().getDisplayMetrics();             

                        float  x1 = event.getX()*(160f/dm.densityDpi);
                        float  y1 = event.getY()*(160f/dm.densityDpi);