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

女生的网名这么多〃 提交于 2019-11-28 02:42:05

问题


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

I have this:

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

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.

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.


回答1:


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.




回答2:


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]);



回答3:


Get the View's location on the screen, then you can calculate the rest using simply arithmetic. How to get the absolute coordinates of a view



来源:https://stackoverflow.com/questions/11832333/how-to-get-screen-cordinates-corresponding-to-the-whole-screen-and-not-the-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!