Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get screen cordinates corresponding to the whole screen, and not the View touched?

Im having some trouble obtaining the real (x,y) from my screen by handling the touch events of one View Item.

I have this:

enter image description here

When I touch the View item, it gives me coordinates with the origin located in his upper-left corner, and if i slide the finger out the view, it gives me negative coordinates (when sliding up or left). enter image description hereenter image description here

I would like to obtain the View location in the screen, to add it to the (x,y) coordinates that i get, so the final result is that the (0,0) is in the upper-left corner of the whole screen. enter image description here

I also need the total screen size, so that i know when the finger is located in the lower-right corner.

How can I achieve this?

Thanks.

like image 416
Alex Avatar asked Aug 06 '12 16:08

Alex


People also ask

How can you find the coordinates of any position on the screen?

Point Position (for Windows) is a simple tool that lets you pick the coordinates for any point on your screen (using X,Y axis). Simply point one of the four corner arrows at the spot on your screen that you want to define and click the button to display the X/Y coordinates.

How do I get touch coordinates on Android?

You can retrieve the coordinates by using getX() and getY() .


2 Answers

MotionEvent class has getX() and getY() methods which return the coordinates relative to the view, as you have discovered.

However it also has getRawX() and getRawY() methods, which according to the docs say:

Returns the original raw X (or Y) coordinate of this event. For touch events on the screen, this is the original location of the event on the screen, before it had been adjusted for the containing window and views.

Sounds like this is what you're after.

like image 183
Tim Avatar answered Nov 15 '22 15:11

Tim


Try using this after view loaded on screen.

int[] location = new int[2];
View.getLocationOnScreen(location); 
Log.v(Common.TAG,"left: "+ location[0]);
Log.v(Common.TAG,"top: "+ location[1]);
like image 31
Vikram Avatar answered Nov 15 '22 15:11

Vikram