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

查看:32
本文介绍了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;
}

但是,如果我使用了

getLocationOnScreen()getLocationInWindow() ?

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

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

推荐答案

我认为 这个答案 不正确.如果我创建一个新项目,并通过添加以下代码段仅编辑 MainActivity:

I don't think this 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.这是使用运行 4.3 的 Nexus 7.我使用运行 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,然后添加窗口的左坐标和上坐标(它们也通过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天全站免登陆