Android-----Get a displaymetrics Introduction to screen resolution.

Source: Internet
Author: User

Citation: http://blog.csdn.net/zhangqijie001/article/details/5894872

Android can be set to scale as the window resizes, but even so, the phone program designer must know the boundaries of the phone screen to avoid the layout distortion caused by scaling.

Cell phone Resolution information is an important information of the phone, very good,Android has provided Displaymetircs class can be very convenient to obtain the resolution. The following is a brief introduction to the Displaymetics class:

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 () method stores the resulting wide-height dimension in the Displaymetrics object, while the resulting wide-height dimension is 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-----Get a displaymetrics Introduction to screen resolution.

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.