Android Get window viewable area size: Getwindowvisibledisplayframe ()

Source: Internet
Author: User

Getwindowvisibledisplayframe () method

Getwindowvisibledisplayframe () is a method under the View class, which can be seen from the name of the method, which is used to obtain the size of the visible area of the current window. Is the height of Contentparentview +actionbar.

The prototype of this method is

public void getWindowVisibleDisplayFrame(Rect outRect);
    • 1

It takes a Rect object as a parameter, which updates the value of Outrect according to the size of the current window's viewable area, and after execution, it can determine the size of the window's viewable area based on the updated outrect. So as Outrect's name shows, it is an output parameter, and later, if the return result of the Getwindowvisibledisplayframe () method is mentioned, it is also the result of the parameter Outrect update, The Getwindowvisibledisplayframe () itself has no return value. In addition, because it is an output parameter, the outrect must not be null, and a Rect object with no size will be passed to the Getwindowvisibledisplayframe () method as a parameter before being used.

Rect rect = new Rect();view.getWindowVisibleDisplayFrame(rect);
    • 1
    • 2

The relationship between the execution result of the Getwindowvisibledisplayframe () and the View object selection

Because the Getwindowvisibledisplayframe () method is a method under the view class, it can only be called through the View object. The return result of multiple view,getwindowvisibledisplayframe () methods in a window is not related to the view selected in the window. At some point, using any view in the current window to perform getwindowvisibledisplayframe () returns the same result. It is also easy to understand that the Getwindowvisibledisplayframe () method returns the size of the visible area of the window, not the viewable area of a view, so it is no different to perform with any view in the window. In general, you can use the root view of the current window to execute this method, which is called the Getdecorview (). Getwindowvisibledisplayframe () of the Windows object. In acitivity and dialog, you can use GetWindow () to get the Window object, which is how it fits together.

Rect rect = new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
    • 1
    • 2

It is important to note that because the Getwindowvisibledisplayframe () method is used to get the viewable area size of a window, the view that calls the Getwindowvisibledisplayframe () method must be included in the window , if it's an isolated view, or contained in another window, it doesn't make sense. For example

new Rect();// 这个new出来的View并没有add到任何窗口上,所以调用它的getWindowVisibleDisplayFrame()方法是没有意义的。new View(this).getWindowVisibleDisplayFrame(rect);
    • 1
    • 2
    • 3

The relationship between the execution result of the Getwindowvisibledisplayframe () and the state of the View object

Although the execution results of getwindowvisibledisplayframe () are not related to the selection of the view in the window, it is related to the state of the view when the method is executed. Because this method is used to get the viewable area size of the window, if this method is called, the call to the View object is not attached (attach) to any window, then executing this method will not get the actual size of the viewable area of one of the windows. This method is called only after the View object has been attach to the window to get the visible area size of the real windows. When the calling view object is not attach to the window, the Getwindowvisibledisplayframe () method estimates a possible viewable area size, which is usually the screen size of the device in pixels. Since it does not represent the actual window viewable area size, this value is not very meaningful.

Because the View object is not attach to the window in the OnCreate () method of Activity/fragment/dialog, the OnCreate () The Getwindowvisibledisplayframe () method that executes a view in a method does not get the actual viewable area size of the current window.

Executing getwindowvisibledisplayframe () in the Onattachedtowindow () method of the activity is not the actual viewable area size of the current window. Because the activity's Onattachedtowindow () method executes, the current window is attach to Window manager, and the view in window is still not attach to the window.

After the view attach to window, the Onattachedtowindow () method of the View object is executed, in theory, the Onattachedtowindow () in the custom view The execution of Getwindowvisibledisplayframe () in the method should give the actual viewable area size of the current window, but this is not the case. Performing Getwindowvisibledisplayframe () in the Onattachedtowindow () method of a custom view can have different results in probability, sometimes the size of the Rect object returned is 0, sometimes the screen size of the device, But neither is the actual viewable area size of the current window. The specific reason is unknown, not carefully studied. Therefore, do not perform getwindowvisibledisplayframe () in the Onattachedtowindow () method of the View object.

To get the actual viewable area size of the current window, you can execute Getwindowvisibledisplayframe () in the Activity/fragment/dialog onwindowfocuschanged () method. The View object in the window is now attach to the window.

It is also important to note that the execution result of Getwindowvisibledisplayframe () is not related to whether the view is visible. The view, whether visible or invisible or gone, has no effect on the execution of Getwindowvisibledisplayframe (). When view is invisible, its OnDraw () method is not called, and when View is gone, its OnDraw () and OnLayout () methods are not called. But either invisible or Gone,onattachedtowindow () will be called, which means the view will be attach to the window, so even if the view is invisible or gone, Getwindowvisibledisplayframe () is also able to return correctly.

Analysis of execution results of Getwindowvisibledisplayframe ()

The Getwindowvisibledisplayframe () execution results described here refer to the actual viewable area size of the current window, which is not attach to the window when the View object is called. The Getwindowvisibledisplayframe () method estimates the size of the viewable area without discussion.

Getwindowvisibledisplayframe () execution results are related to the following factors

  1. System Status Bar

    The system status bar affects the value of the top property in the Getwindowvisibledisplayframe () execution result outrect.

    If the window is full-screen, that is, flags is set to WindowManager.LayoutParams.FLAG_FULLSCREEN, or Android:windowfullscreen is set to True, The top property in Outrect is not affected by the status bar, and its value is always 0. Otherwise, the value of the top property in Outrect will be affected by the system status bar.

    If the height of the window's layoutparams is set to WindowManager.LayoutParams.MATCH_PARENT, the top value in Outrect equals the height of the system status bar. If the height of the window's layoutparams is set to WindowManager.LayoutParams.WRAP_CONTENT or a specific value, the top value in Outrect equals the height of the system status bar and the window overlap area. If there is no overlap, it is 0.

    For example, the screen height is 1920, the window height is set to 1900, and the window is centered. The window has a distance of 10 pixels from the screen up and down. If the system status bar height is 60, the height of the overlapping area of the window and status bar is 50. Therefore, the top value in the Outrect returned by Getwindowvisibledisplayframe () is 50.

    The above points can be attributed to a point where the top value in Outrect equals the effect of the system status bar on the position above the window in theory.

    If the window is full-screen, the system status bar will not affect the top position of the window, so the top value in Outrect is always 0. If the height of the window's layoutparams is set to WindowManager.LayoutParams.MATCH_PARENT, the theoretical window will reach the top of the screen, but due to the presence of the status bar, the window position will be pressed below the status bar. Therefore, the top value in Outrect equals the height of the system status bar. If the height of the window's layoutparams is set to WindowManager.LayoutParams.WRAP_CONTENT or a specific value, and the window and status bar overlap, then the status bar also tries to suppress the window position below the status bar , whose displacement is the height of the overlapping area, so the top value in the Outrect is equal to the height of the overlapping area. It is important to note that the effect of the status bar on the position of the window will not actually take effect, that is, the window will still overlap with the status bar, so the effect of the status bar on the position of the window is theoretical and not necessarily effective.

  2. Virtual keyboard

    The virtual keyboard affects the value of the bottom property in the Getwindowvisibledisplayframe () execution result outrect.

    If the virtual keyboard is hidden, the value of the bottom property in Outrect will always be equal to the screen height (actually subtracting the height of the virtual key bar, ignoring the virtual keys first). If the virtual keyboard is displayed, the value of the bottom property in Outrect will be equal to the screen height minus the theoretical effect that the virtual keyboard will have on the window position. If the window height is match_parent, the value of the bottom property in Outrect is equal to the screen height minus the height of the virtual keyboard.

    In the same example, the screen height is 1920, the window height is set to 1900, and the window is centered. The window has a distance of 10 pixels from the screen up and down. If the virtual keyboard height is 600, the height of the overlapping area of the window and the virtual keyboard is 590. Therefore, the bottom value in the Outrect returned by Getwindowvisibledisplayframe () is 1920-590.

  3. Virtual Key Bar

    The virtual key bar affects the value of the bottom property in the Getwindowvisibledisplayframe () execution result outrect.

    The only consideration here is that the soft keyboard is hidden, and if the soft keyboard is displayed, the effect of the soft keyboard and the virtual key bar on the value of the bottom property in Outrect will be superimposed.

    If the virtual key bar is hidden, the value of the bottom property in Outrect will always be equal to the screen height. If the virtual key is displayed, the value of the bottom property in Outrect will be equal to the screen height minus the effect that the theoretically virtual key will have on the window position. If the window height is match_parent, the value of the bottom property in Outrect is equal to the screen height minus the height of the virtual key.

    There is no longer an illustrative example.

In summary, Getwindowvisibledisplayframe () execution results will be affected by the system status bar, System soft keyboard, system virtual keys.

It is important to note that the result of Getwindowvisibledisplayframe () is not the actual size of the window (although it has a certain relationship to the size of the window). For example, a Center Display dialog box, its actual height is only 500px, it and the system status bar, System soft keyboard, System virtual key bar are not overlapping, then the result of Getwindowvisibledisplayframe () is the size of the device, Instead of the actual size of the dialog box.

In addition, although there is a visible in the method name, the result of Getwindowvisibledisplayframe () is not affected by whether the window is obscured by other windows. Even if the window has been switched to the background, the result will not change as long as the window is not dettach,getwindowvisibledisplayframe ().

Application of Getwindowvisibledisplayframe ()

In the Android system, there is no API to get the system status bar, System soft keyboard and system virtual key bar height, but in the application sometimes need to get these values. Because the Getwindowvisibledisplayframe () return result will be affected by the system status bar, System soft keyboard, system virtual key. Therefore, this API is often used to obtain the system status bar, System soft keyboard and system virtual key bar height.

For the system status bar height, gets a non-full screen, and the height of the window's layoutparams is set to the size of the WindowManager.LayoutParams.MATCH_PARENT window viewable area, and its top value is the level of the status bar.

To the system soft keyboard, get a height is match_parent window in the soft keyboard display and hide two different states of the visual area size, subtract the bottom value will be able to get the height of the soft keyboard.

To the system system virtual key bar, get a height is match_parent window in the virtual key display and hide two different states of the visual area size, the bottom value can be subtracted to get the height of the virtual button.

Performance issues with Getwindowvisibledisplayframe ()

In the comments of the Getwindowvisibledisplayframe () method, there is a paragraph

Description the Getwindowvisibledisplayframe () method obtains this information from window Manager through IPC, which is relatively expensive and therefore not suitable for calls where performance is a high requirement. For example, the view draws the code.

Android Get window viewable area size: Getwindowvisibledisplayframe ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.