Android gets screen resolution and displaymetrics Overview)

Source: Internet
Author: User

Android can be set to adjust the zoom ratio with the window size, but even so, the mobile phoneProgramDesigners still need to know the border of the mobile phone screen to avoid layout deformation caused by scaling.

The resolution information of the mobile phone is an important information of the mobile phone. It is good that android has provided the displaymetircs class to conveniently obtain the resolution. The displaymetics class is briefly introduced below:

The displaymetrics class under the andorid. util package provides general information about the display, such as the display size, resolution, and font.


To obtain displaymetrics members, first Initialize an object as follows:

Displaymetrics metrics = new displaymetrics ();

Getwindowmanager (). getdefaultdisplay (). getmetrics;

Note: The constructor displaymetrics does not need to pass any parameters. After getwindowmanager () is called, the handle of the existing activity is obtained. At this time, getdefaultdisplay () the method stores the obtained width and height dimensions in the displaymetrics object, and the obtained width and height dimensions are measured in pixels ), "pixels" refer to "absolute pixels" rather than "relative pixels ".

 

The displaymetrics tostring () method can be used to obtain most of displaymetrics's fields information. The following are some output information when the resolution is 480x320:

Among them, density is the display logic resolution (a lot is mentioned in the document, but it is not very clear); width and height are the screen resolution (absolute width and height ), consistent with widthpixels and heightpixels in fields, scaledensity and density, and xdpi and ydpi are accurate physical pixels.

 

Next we will compare the differences among dip, DP, PX, and SP in Android:

Dip: Device Independent pixels (device independent pixel ). different devices have different display effects, which are related to the hardware of the device. We recommend that you use this function to support WVGA, hvga, and qvga without pixels.

Px: pixels (pixels). Different devices have the same display effect. Generally, we use hvga to represent 320x480 pixels, which is usually used.

PT: point, a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use;

SP: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in fonts. According to Google's suggestion, it is best to use SP as the unit for textview font size,


In the past, programmers often designed computer user interfaces in pixels. For example, define a form field with a width of 300 pixels. the spacing between columns is 5 pixels, And the Icon size is 16 × 16 pixels. The problem with this processing is that if you run the program on a new display with a dot (DPI) higher per inch, the user interface will look small. In some cases, the user interface may be small to difficult to see the content.

The measurement units irrelevant to resolution can solve this problem,


Android supports all the following units:

Px (pixel): the point on the screen.

In (INCHES): the unit of length.

Mm (mm): the unit of length.

Pt (lbs): 1/72 inch.

DP (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.

Dip: it is the same as DP and is mostly used in Android/ophone examples.

SP (pixels irrelevant to the scale): similar to DP, but can be scaled based on the user's font size preferences.

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.