12

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.

Sterling Duchess
  • 1,970
  • 16
  • 51
  • 91
  • where are your receiving the event from? is the event coming from a touch event from a parent of the image view, or from the image view itself? – Gil Moshayof Jul 27 '15 at 11:14
  • I think you should refer to this link: [How to get actionbar height](http://stackoverflow.com/questions/12301510/how-to-get-the-actionbar-height). It will help to compute the correct touch X and Y coordinate. I've encountered this problem too. – Joey Jul 28 '15 at 07:09
  • So you're starting with the absolute coordinates on screen and need to convert to coordinates relative to the top-left of the image? – Jacob Phillips Jul 31 '15 at 02:49
  • @GilMoshayof event is received from the onTouch listener. – Sterling Duchess Aug 02 '15 at 08:58
  • @Joey Does not map points correctly it seems the coordinates are bound to ImageView which fills up the entire layout starts right bellow ActionBar. – Sterling Duchess Aug 02 '15 at 08:59
  • Sorry man. I've searched previous question and I found this [How to convert coordinates](http://stackoverflow.com/questions/4933612/how-to-convert-coordinates-of-the-image-view-to-the-coordinates-of-the-bitmap). Hope this will help you now :) – Joey Aug 03 '15 at 02:01

6 Answers6

6

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

justHooman
  • 3,044
  • 2
  • 17
  • 15
4

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.

Tobias Ritzau
  • 3,327
  • 2
  • 18
  • 29
3

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()

2

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

Jithu P.S
  • 1,843
  • 1
  • 19
  • 32
2

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 )

Joseph Mekwan
  • 1,082
  • 1
  • 15
  • 28
2

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.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98