DOCUMENT
getLocationOnScreen(int[] location) : void
Added in API level 1
Computes the coordinates of this view on the screen. The argument must be an array of two integers. After the method returns, the array contains the x and y location in that order.
Parameters
location - an array of two integers in which to hold the coordinates
.
getLocationInWindow(int[] location) : void
Added in API level 1
Computes the coordinates of this view in its window. The argument must be an array of two integers. After the method returns, the array contains the x and y location in that order.
Parameters
location - an array of two integers in which to hold the coordinates
两个方法都是
用来获取 View 在屏幕上的位置的。文档完全相同。然而这是两个不同的方法,而且没有任何一个 Deprecated。肯定有什么不同吧。
SOURCE
/**
* <p>Computes the coordinates of this view on the screen. The argument
* must be an array of two integers. After the method returns, the array
* contains the x and y location in that order.</p>
*
* @param location an array of two integers in which to hold the coordinates
*/
public void getLocationOnScreen(@Size(2) int[] location) {
getLocationInWindow(location);
final AttachInfo info = mAttachInfo;
if (info != null) {
location[0] += info.mWindowLeft;
location[1] += info.mWindowTop;
}
}
显然 getLocationInWindow
的源码不必看了。
所以
这两个方法的区别是,getLocationOnScreen
比 getLocationInWindow
多添加了一个 AttachInfo
。
通常情况下,这两个方法的效果是相同的。然而,当 View 在 Dialog 等不能铺满全屏幕的 Window 中时,结果便不同了。