5

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;

    }
});
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Manikandan
  • 1,479
  • 6
  • 48
  • 89

2 Answers2

22

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
Adam Monos
  • 4,287
  • 1
  • 23
  • 25
  • thanks... I used this code. I touched the image, I got in event.getX(),event.getY() as 87,176. In imageX,imageY 87,78. In the second activity the tag image is placed at 87,78. But my image is at the bottom of the screen. – Manikandan Jul 03 '12 at 14:15
  • 2
    You should get the screen coordinates of the `ImageView` in your 2nd activity with `int[] viewCoords = new int[2]; imageView.getLocationOnScreen(viewCoords);` as well, then add the `imageX` and `imageY` to the respective viewCoords values, and place the tag there. It should work that way. – Adam Monos Jul 03 '12 at 14:19
  • I used like this in the second activity. int[] viewCoords = new int[2];im.getLocationOnScreen(viewCoords); int imageX = "x_ac1"- viewCoords[0]; int imageY = "y_ac1"- viewCoords[1]; and set the imageX,imageY position to place the tags. But I got negative imageY value. – Manikandan Jul 03 '12 at 14:28
  • 2
    Yes, beacuse in the first activity you have to subtract the touch coordinates and the screen coordinates, but in the second activity you have to add them. Replace `imageX = x_ac1 - viewCoords[0]; int imageY = y_ac1 - viewCoords[1];` with `imageX = x_ac1 + viewCoords[0]; int imageY = y_ac1 + viewCoords[1];` in your second activity, and you will get the right coordinates. – Adam Monos Jul 03 '12 at 14:33
  • But its not placed in the exact position where i clicked in the first activity. – Manikandan Jul 03 '12 at 15:07
  • I have added the code what I used in first and second activity. pls check that. – Manikandan Jul 03 '12 at 15:13
  • Try using `imageX` and `imageY` instead of `x` and `y` in your first activity at these lines: `params.leftMargin = x; params.topMargin = y;`. Right now, you computed the coordinates on the `ImageView` in your first activtiy, but you don't use them there to put on the tag, only in your second activity, this might cause inconsistencies. – Adam Monos Jul 04 '12 at 07:11
  • If I use imageX,imageY instead of x,y the tag is placed in the different position in the first activity. – Manikandan Jul 04 '12 at 08:25
  • 2
    Oh yes, my bad. I checked the code again, but I don't really see any error in the logic. Try debugging the x,y coordinates you use, from that you should be able to figure out where the error is. – Adam Monos Jul 04 '12 at 08:37
  • finally, it get worked. Previously I had different size for the images in first and second activity. Now I have same sized imageview. works fine. thanks... – Manikandan Jul 04 '12 at 13:58
0

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