getLocationOnScreen vs getLocationInWindow

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 的源码不必看了。

所以

这两个方法的区别是,getLocationOnScreengetLocationInWindow 多添加了一个 AttachInfo
通常情况下,这两个方法的效果是相同的。然而,当 View 在 Dialog 等不能铺满全屏幕的 Window 中时,结果便不同了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值