How to get exact position in pixels of my TextView?

*爱你&永不变心* 提交于 2019-12-31 04:54:05

问题


I have a smartphone of 2560 x 1440 px. Now I am using this function for my TextView:

int[] locationOnScreen = new int[2];
txtAp.GetLocationInWindow(locationOnScreen);

It is supposed to give me the total x and y coordinates in pixels. My TextView is pretty much in the middle of the screen, so supposedly at (1280,770). But the function returns [69, 1111].

How can that be? If that is not the way of doing that, then what is?


回答1:


I have a smartphone of 2560 x 1440 px... My TextView is pretty much in the middle of the screen, so supposedly at (1280,770).

Not correct, unless you talk about the center of the TextView. Any view is rendered on screen within its rectangle, so its coordinates are considered to be of [left, top, right, bottom].

The GetLocationInWindow() method returns (left, top) coordinates of a view. So (x = 69, y = 1111) looks to be meaningful for the left top corner of your TextView located in the middle of screen.

Important note: GetLocationInWindow() returns the coordinates w.r.t the root view, not actual window. You should take the status bar height into consideration. And here is how you can calculate Height of status bar in Android.



来源:https://stackoverflow.com/questions/47312677/how-to-get-exact-position-in-pixels-of-my-textview

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