Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get touch event coordinates relative to the parent

Tags:

android

I have a linear layout that has an image in it I am trying to get the touch event coordinates of the image relative to the parent that contains it. Do you know I can I get these? getX and getY are returning position relative to self

like image 520
Snake Avatar asked Mar 08 '12 03:03

Snake


1 Answers

The MotionEvent class has methods getX() to get X touch point corresponding to that view and getRawX() to get X touch point corresponding to screen.

So to get X touch point corresponding to parent you can get that by a simple calculation:

     view.getLeft() + motionEvent.getX()

The getLeft() returns Left position of this view relative to its parent

like image 98
Sadeshkumar Periyasamy Avatar answered Oct 29 '22 03:10

Sadeshkumar Periyasamy