Sterling Duchess
Sterling Duchess

Reputation: 2080

Get real x and y coordinates of users touch

I need to map onTouch event's X, Y coordinates to the Bitmap X, Y coordinates inside the ImageView to do this I use the following approach.

However this approach seems to only work when I either:
a) Fully zoom the image (all the way in)
b) Works in any case if I make my application full screen

  final int index = event.getActionIndex();
  touchLocation = new float[] {
    event.getX(index), event.getY(index)
  };

  Matrix matrix = new Matrix();
  ImageView view = getImageView();
  view.getImageMatrix().invert(matrix);

  matrix.postTranslate(view.getScrollX(), view.getScrollY());
  matrix.mapPoints(touchLocation);
  // touchLocation[0] is real x and [1] is real y  

However my activity is an ActionBar activity so at I get a bit wrong position on the Y axis. I tried deducting the height of ActionBar and StatusBar but this does not always work.

What is odd is that on full screen it does not matter if I fully zoom my image in or out I always get correct coordinates calculated however with any other Activity type this will not map points correctly.

Upvotes: 12

Views: 4392

Answers (6)

Narendra Kothamire
Narendra Kothamire

Reputation: 973

Make sure that your Actionbar and status heights are in pixel and not dp and also you can try using getRawX() and getRawY() instead of getX(), getY()

Upvotes: 3

justHooman
justHooman

Reputation: 3054

I use this code to get the actually axis of touch on screen:

PointF p = new PointF(event.getX(), event.getY());
View v = this;// your view
View root = v.getRootView();
while (v.getParent() instanceof View && v.getParent() != root) {
  p.y += v.getTop();
  p.x += v.getLeft();
  v = (View) v.getParent();
}

Hope this works for you as well

Upvotes: 6

MKJParekh
MKJParekh

Reputation: 34301

Step 1 : Create one custom CustomFrameLayout extending FrameLayout in your app that can take one interface object in constructor. (interface will be created in step 3)

Step 2 : Write/Override onInterceptTouchEvent method.

Step 3 : Make one interface to send/receive desired params.

Step 4 : Implement interface and method created in step 3 in your activity/fragment

Step 5 : In your activity/fragment create object of CustomFrameLayout with implement interface.

Step 6 : In custom layout's overridden method, pass value to activity through the interface

Hope this will work!


Making a demo would take a bit longer, would have rather post as comment but still posting as answer so that I can someday improve with some example code.

Upvotes: 2

Joseph Mekwan
Joseph Mekwan

Reputation: 1092

Override onTouchEvent(MotionEvent event) and then call event.getX() and event.getY() to get the coordinate positions of where the user touched [they will be floats]. ( by : @antonyt )

Upvotes: 2

Jithu P.S
Jithu P.S

Reputation: 1843

Try this way

int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);
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

you will get the x and y coordinates using this way

Upvotes: 2

Tobias Ritzau
Tobias Ritzau

Reputation: 3327

I can't tell from the pasted code what the exact problem is, but it looks like you should use: http://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[]) and take that into account for your calculation.

Upvotes: 4

Related Questions