getLocationOnScreen将()与getLocationInWindow() [英] getLocationOnScreen() vs getLocationInWindow()

查看:369
本文介绍了getLocationOnScreen将()与getLocationInWindow()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这两种方法的背景画面和视图之间的区别是什么?

What is the difference between screen and view in the context of these two methods?

我有一个按钮,我想在X的中心坐标。

I have a button and I want to get the x coordinate of its center.

我想这将是足够的:

public int getButtonXPosition() {
    return (button.getLeft()+button.getRight())/2;
}

但随后,它会做,如果我会用什么区别

but then, what difference would it make if I would have used

getLocationOnScreen将() getLocationInWindow()

(当然,加入一半按钮的宽度是的,)

(adding half of the button's width to that, of course)

推荐答案

注意 - 这个答案是不接受的答案,当它被写,这就是为什么它是措辞反驳对方的回答

我不认为接受的答案是正确的。如果我创建一个新的项目,并通过添加下面的代码片段只编辑MainActivity:

I don't think the accepted answer is correct. If I create a new project, and edit only the MainActivity by adding the following snippet:

public boolean dispatchTouchEvent(MotionEvent ev) {
    View contentsView = findViewById(android.R.id.content);

    int test1[] = new int[2];
    contentsView.getLocationInWindow(test1);

    int test2[] = new int[2];
    contentsView.getLocationOnScreen(test2);

    System.out.println(test1[1] + " " + test2[1]);

    return super.dispatchTouchEvent(ev);
}

我会看到打印到控制台 108 108 。这是使用的Nexus 7运行4.3。我在使用运行Android版本,早在2.2的模拟器类似的结果。

I will see printed to the console 108 108. This is using a Nexus 7 running 4.3. I have similar results using emulators running android versions as far back as 2.2.

正常活动的窗口将具有FILL_PARENTxFILL_PARENT作为其WindowManager.LayoutParams,这导致他们铺设到整个屏幕的尺寸。窗口的布局下(在问候z顺序,而不是y坐标),状态条等装饰,所以我认为更准确的图表将是:

Normal activity windows will have FILL_PARENTxFILL_PARENT as their WindowManager.LayoutParams, which results in them laying out to the size of the entire screen. The Window is laid out underneath (in regards to z-order, not y-coordinates) the statusbar and other decorations, so I believe a more accurate chart would be:

|--phone screen-----activity window---| 
|--------status bar-------------------| 
|                                     | 
|                                     | 
|-------------------------------------| 

如果您通过这两种方法的来源步骤,你将看到 getLocationInWindow 穿过你的视图的视图层次到RootViewImpl,总结视图坐标和 减去母公司滚动偏移。在这种情况下我上面描述的ViewRootImpl越来越从WindowSession状态栏的高度,并通过fitSystemWindows到ActionBarOverlayLayout,这将增加该值的动作条的高度传递下来。 ActionBarOverlayLayout然后把这个总和值,并将其应用于它的内容来看,这是你的布局的父,作为保证金。

If you step through the source of these two methods, you will see that getLocationInWindow traverses up your view's view hierarchy up to the RootViewImpl, summing view coordinates and subtracting parent scroll offsets. In the case I described above the ViewRootImpl is getting the status bar height from the WindowSession, and passing it down through fitSystemWindows to the ActionBarOverlayLayout, which adds this value to the actionbar height. ActionBarOverlayLayout then takes this summed value and applies it to its content view, which is the parent of your layout, as a margin.

所以,你的内容被布置低于状态栏不作为窗口的结果,开始在较低的比状态栏y坐标,而是作为一个余量的结果被应用到活动的内容图。

So, your content is laid out lower than the status bar not as a result of the window starting at a lower y coordinate than the status bar, but instead as a result of a margin being applied to your activity's content view.

如果你窥视 getLocationOnScreen将源代码,你会看到它仅仅是调用 getLocationInWindow ,然后添加窗口的左侧和顶部COORDS(也传递给看ViewRootImpl,它从WindowSession获取它们)。在正常情况下,这些值都将是零。也有一些情况下,这些值可以是非零的,例如一个对话窗口,被放置在屏幕的中间

If you peer into the getLocationOnScreen source you'll see it merely calls getLocationInWindow and then adds the Window's left and top coords (which are also passed to the View by ViewRootImpl, which fetches them from the WindowSession). In the normal case, these values will both be zero. There are some situations where these values may be non-zero, for example a dialog window that is placed in the middle of the screen.

因此​​,要总结:一个正常活动的窗口填满整个屏幕,即使在状态栏和装饰的空间。有问题的两种方法将返回相同的x和y坐标。只有在特殊情况下,如对话框,其中窗口实际上偏移量这两个值不同。

So, to summarize: A normal activity's window fills the entire screen, even the space under the status bar and decorations. The two methods in question will return the same x and y coordinates. Only in special cases such as dialogs where the Window is actually offset will these two values differ.

这篇关于getLocationOnScreen将()与getLocationInWindow()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆