Get the touch position inside the imageview in android

微笑、不失礼 提交于 2019-11-26 07:43:56

问题


I have a imageview in my activity and I am able to get the position where the user touch the imageview, through onTouchListener. I placed another image where the user touch over that image. I need to store the touch position(x,y), and use it in another activity, to show the tags. I stored the touch position in the first activity. In the first activity, my imageview at the top of the screen. In the second activity its at the bottom of the screen. If I use the position stored from the first acitvity, it place the tag image at the top, not on the imageview, where I previously clicked in the first activity. Is there anyway to get the position inside the imageview.

FirstActivity:

cp.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            x = (int) event.getX();
            y = (int) event.getY();

            Log.v(\"touched x val of cap img >>\", x + \"\");
            Log.v(\"touched y val of cap img >>\", y + \"\");

            tag.setVisibility(View.VISIBLE);

            int[] viewCoords = new int[2];
            cp.getLocationOnScreen(viewCoords);

            int imageX = x - viewCoords[0]; // viewCoods[0] is the X coordinate
            int imageY = y - viewCoords[1]; // viewCoods[1] is the y coordinate
            Log.v(\"Real x >>>\",imageX+\"\");
            Log.v(\"Real y >>>\",imageY+\"\");

            RelativeLayout rl = (RelativeLayout) findViewById(R.id.lay_lin);
            ImageView iv = new ImageView(Capture_Image.this);
            Bitmap bm = BitmapFactory.decodeResource(getResources(),
                    R.drawable.tag_icon_32);
            iv.setImageBitmap(bm);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.leftMargin = x;
            params.topMargin = y;
            rl.addView(iv, params);

            Intent intent= new Intent(Capture_Image.this,Tag_Image.class);
            Bundle b=new Bundle();
            b.putInt(\"xval\", imageX);
            b.putInt(\"yval\", imageY);
            intent.putExtras(b);
            startActivity(intent);

            return false;
    }
});

In TagImage.java I used the following:

im = (ImageView) findViewById(R.id.img_cam22);
b=getIntent().getExtras();
xx=b.getInt(\"xval\");
yy=b.getInt(\"yval\");

im.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
         int[] viewCoords = new int[2];
            im.getLocationOnScreen(viewCoords);

            int imageX = xx + viewCoords[0]; // viewCoods[0] is the X coordinate
            int imageY = yy+ viewCoords[1]; // viewCoods[1] is the y coordinate
            Log.v(\"Real x >>>\",imageX+\"\");
            Log.v(\"Real y >>>\",imageY+\"\");
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.lay_lin);
        ImageView iv = new ImageView(Tag_Image.this);
        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.tag_icon_32);
        iv.setImageBitmap(bm);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                30, 40);
        params.leftMargin =imageX ;
        params.topMargin = imageY;
        rl.addView(iv, params);
        return true;

    }
});

回答1:


You can get the top left corner of your view as follows:

int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);

From this and the touch coordinates you can calculate the point inside the ImageView:

int touchX = (int) event.getX();
int touchY = (int) event.getY();

int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate



回答2:


Add these lines to ImageView

android:scaleType="fitXY" 

and

android:adjustViewBounds="true"

to your image view it is because image are not having their original dimensions in IV



来源:https://stackoverflow.com/questions/11312128/get-the-touch-position-inside-the-imageview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!