Android obtains the screen height, width, and Status Bar Height.

Source: Internet
Author: User

Background:

So far, Android has evolved from 1.5 to 3.2. When writing an application, we usually get the screen height, width, and Status Bar Height.

However, the android system has changed too fast. From the initial mobile phone operating system to the current 3.2 tablet computer system, there has also been a big change in obtaining this data.

It deserves our attention. Otherwise, many errors may occur.

Problem Analysis and Solution:

1. from Android 1.6 to Android 2.x

This is the Android mobile phone operating system. from 1.6 to 2.x, there is a unified way to get it.

You can use the android API to obtain the corresponding size,

Windowmanager WM = (windowmanager) This. getsystemservice (context. window_service );

Int width = WM. getdefadisplay display (). getwidth (); // screen width

Int Height = WM. getdefadisplay display (). getheight (); // screen height

Rect = new rect ();

This. getwindow (). getdecorview (). getwindowvisibledisplayframe (rect );

Int statusbarheight = rect. Top; // Status Bar Height

2. Android 3.0 tablet System

In the 3.0 system, the status bar is below the screen because the calculation method also changes.

The method for obtaining the screen height and width in the 3.0 system has not changed.

The status can be obtained as follows:

Rect = new rect ();

This. getwindow (). getdecorview (). getwindowvisibledisplayframe (rect );

Int statusbarheight = Window. getwindowmanager (). getdefadisplay display (). getheight ()-rect. bottom;

That is, the screen height minus the maximum height of the display area is the height of the status bar below.

3. Android 3.2 tablet System

In Android 3.2, there is a big change. When we call getwidth () and getheight () to obtain the width and height, the actual size of the screen is not returned,

Return the size of the display area of the screen, that is, the height of the status bar is subtracted.

The size read using these two API functions is definitely not what we want.

At this time, we find that it provides two hidden functions: getrealheight () and getrealwidth () to obtain the real screen size.

First, because it is a hidden function, we can only call these two functions through reflection, but this leads to poor reflection efficiency.

Display display = WM. getdefaultdisplay ();
Class C = Class. forname ("android. View. display ");

Method method = C. getmethod ("getrealheight ");
Int Height = (integer) method. Invoke (Display );

Rect = new rect ();

This. getwindow (). getdecorview (). getwindowvisibledisplayframe (rect );

Statusbarheight = height-rect. bottom;

Optimize the above Code. If we call the code frequently, the program performance will be affected.

We can save the information related to the first reflection and then call it directly later.

Private method = NULL; // used to save the method object

---------------------------------------------------------------------------------

Display display = WM. getdefaultdisplay ();

// Determine whether the method is null. If it is null, the method information is obtained through reflection. Otherwise, the old method object is used.
If (Method = NULL)
{

Method = display. getclass (). getmethod ("getrealheight"); // The display class information is directly used here.

}

Int Height = (integer) method. Invoke (Display );

Rect = new rect ();

This. getwindow (). getdecorview (). getwindowvisibledisplayframe (rect );


Related Article

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.