0

I have the following layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#ff000000">

<ImageView
    android:id="@+id/img_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_launcher" />
</RelativeLayout>

and the following listener:

img_view = (ImageView)findViewById(R.id.img_view);

img_view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int loc[] = new int[2];
            v.getLocationOnScreen(loc);
            Log.d(TAG, "loc[0]: " + loc[0] + ", loc[1]: " + loc[1] + 
                       ", event.getX(): " + (int)event.getX() + 
                       ", event.getY(): " + (int)event.getY());
        }
        return true;
    }
});

The ImageView contains a Bitmap and I want to detect the (x,y) coordinates of the point where the user touches into the bitmap. Loc[0], loc[1] are always 0 and 76, respectively. I think that they are the absolute coordinates of the upper-left corner of the layout. Absolute, I mean, as respect to the entire screen. Whereas, event.getX() and event.getY(), seem to be the absolute coordinates considering the region of the screen below the title of the activity. Is it correct? If so, how I can compute the relative coordinates of the touch point as respect to the bitmap?

user2923045
  • 369
  • 2
  • 6
  • 16
  • read about and play wirh ImageView.getImageMatrix() – pskink Jan 05 '14 at 09:00
  • Thanks @pskink. I've tried to understand how to exploit getImageMatrix method but it is not clear to me how to proceed. Could you please provide me with additional details? – user2923045 Jan 06 '14 at 17:05
  • After a quick pass I think that a more detailed solution [is reported here](http://stackoverflow.com/questions/4933612/how-to-convert-coordinates-of-the-image-view-to-the-coordinates-of-the-bitmap) – user2923045 Jan 06 '14 at 17:43

2 Answers2

0

i could not understand your question but what i think you are asking is that you want to know where on the bitmap does the user touched it

i think you are right with x = event.getX() and y = event.getY() just try this this it

lets say that your bitmap is at bx and by coordinate and its width and height are bw and bh

then check if your touch is within the coordinates i.e. bx < x < bx+bw and by < y < by+bh

Good Luck

Mayank
  • 1,364
  • 1
  • 15
  • 29
  • Thanks Mayank for your comment but unfortunately it not so simple as you think. The problem is that the bitmap is automatically re-scaled when rendered into the ImageView and converting absolute coordinates returned by event.getX() and event.getY() to relative coordinates into the Bitmap is not so clear. :-( – user2923045 Jan 06 '14 at 16:58
0

You want the location of the touch point relative to the image boundaries instead of the entire screen's? Just subtract the location of the image from your touch coordinates. For instance:

int imageRelativeX = event.getX() - loc[0];
int imageRelativeY = event.getY() - loc[1];

Then that makes the top left corner of the image (0,0) and the bottom right corner (image width,image height).

Kavan
  • 340
  • 2
  • 12