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?