Android Displaymetrics Get screen resolution

Source: Internet
Author: User

Android provides DISPLAYMETIRCS class for easy access to resolution. The Displaymetics class is described below:

The Displaymetrics class under the Andorid.util package provides a general information about the display, such as display size, resolution, and font.

To get the Displaymetrics member, first initialize an object as follows:

Displaymetrics metrics = new Displaymetrics ();

Getwindowmanager (). Getdefaultdisplay (). Getmetrics;

Note: The constructor displaymetrics does not need to pass any arguments; after calling Getwindowmanager (), the handle of the existing activity is obtained, at which point the Getdefaultdisplay () The Displaymetrics method stores the resulting width and height dimensions in the object, while the resulting width and height dimensions are in pixels (Pixel), and "Pixels" refers to "absolute pixels" rather than "relative pixels".

Most of the fields in Displaymetrics can be obtained through the toString () method of Displaymetrics, as shown in some output information in the case of a resolution of 480x320:

Where density is the logical resolution shown (many in the document, not very clear); width and height are the screen resolution (absolute width and height), consistent with Widthpixels and heightpixels in fields ; scaledensity is consistent with density, xdpi and ydpi are accurate physical pixels.

Let's compare the differences between the dip, DP, PX, SP in android:

Dip:device independent pixels (device independent pixels). Different devices have different display effects, this is related to the device hardware, generally we support WVGA, HVGA and QVGA recommend this, do not rely on pixels.
Px:pixels (pixels). Different devices show the same effect, generally we hvga represent 320x480 pixels, this use more.
Pt:point, is a standard length unit, 1pt=1/72 inches, for the printing industry, very simple to use;
sp:scaled pixels (enlarge pixels). Mainly used for font display best for textsize, according to Google's suggestion, the TextView of the size of the most used SP do units,

In the past, programmers typically designed computer user interfaces in pixels. For example, you define a form field with a width of 300 pixels, a space between columns of 5 pixels, an icon size of 16x16 pixels, and so on. The problem with this approach is that if you run the program on a new monitor with a higher dots per inch (dpi), the user interface will appear small. In some cases, the user interface may be too small to see the content.
Resolution-independent units of measure can solve this problem, and Android supports all of the following:
PX (pixels): The point on the screen.
In (inches): unit of length.
MM (mm): unit of length.
PT (lb): 1/72 inches.
DP (density-independent pixels): An abstract unit based on screen density. 1DP = 1px on a monitor 160 dots per inch.
Dip: Same as DP, more for android/ophone example.
SP (scale independent pixels): Similar to DP, but can be scaled based on the user's font size preference.

Android has a class displaymetrics can get information such as resolution, the method is as follows:

Displaymetrics Metrics;getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics);

Metrics.widthpixels Screen width

Metrics.heightpixels screen Height

metrics.density screen Density

A formula pixels = DPS * (density/160), mentioned in the SDK, need to explain the density and metrics.density in a little formula is not a thing, their relationship is: metrics.density = density/ The value of the 160,density is 120, 160, 240, 320 (which is also stated in the SDK);

When android:minsdkversion= "3″" is now supported android1.5, in Android 1.5 and earlier, only the HVGA (320x480) Resolution on the 3.2″ screen is supported, Developers also do not need to consider interface compatibility issues. After Android 1.6, the platform supports a variety of sizes and resolutions, which means that developers are designed to take into account the diversity of the screen.

Thus, in the android:minsdkversion= "3″, there is no concept of multi-resolution, so at this time, the width and height of displaymetrics is related to the density, that is, the width and height are to be DP or dip units, To convert to PX is the resolution we usually say. Since android1.6 and later, the width and height of displaymetrics are in PX units, no conversion is required.

The compatibility of Android means that the application API is degraded. The degradation of the API means that the display does not match up on high-end machines (with a high version configuration).

There are three ways to get a screen size as follows:
//acquired via WindowManager
displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);

//Acquired through Resources
displaymetrics dm2 = Getresources (). Getdisplaymetrics ();

//Get the default resolution of the screen
Display display = Getwindowmanager (). Getdefaultdisplay ();
System.out.println ("Width-display:" + display.getwidth ());
System.out.println ("Heigth-display:" + display.getheight ());

In the Android 4.2 system, the correct screen size value is obtained using method one, and the values obtained by using methods two and three are all zero. The reason is ominous. The Display.getwidth () and Display.getheight () in method three have been deprecated.


Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); //display = Getwindowmanager (). Getdefaultdisplay ();d isplay.getmetrics (DM) (Assigning screen size information to displaymetrics DM);
width = dm.widthpixels;
Height = dm.heightpixels;
xdpi = dm.xdpi;
ydpi = dm.ydpi;
density = dm.densitydpi;
fdensity = dm.density;

Also, we usually mean that the size of the int on the screen is a pixel unit, for example, I use Gethitrect () to get the value of the control's rectangle coordinates even if it matches the pixel size.

The relationship between Displaymetrics and display.
Display refers to the object that displays the area, which may be a real physical screen, or only the display area of the application, such as in non-full-screen activity, because the system has a status bar, so the display area is smaller than the physical screen. The Displaymetrics encapsulates the various attribute values for the display area . View source discovery, in displaymetrics the comments for each attribute value are described as true physical dimensions. It is also found that the function of Display.getmetrics (DM) is basically applied when the real screen size is obtained. Remember that is good.

In addition, XDPI does not refer to the size of the screen width in DP units. The screen width in DP is calculated using the formula: Dpwidth = widthpixels/fdensity = 540/1.5 =

The Displaymetrics class under the Andorid.util package provides a general information about the display, such as display size, resolution, and font.

//Get Resolution

displaymetrics dm = new Displaymetrics ();

Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);

        

int nowwidth = dm.widthpixels;//Current resolution width

int nowheigth = dm.heightpixels;//Current resolution height

Android Displaymetrics Get screen resolution

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.