Usage of the displaymetrics class in Android

Source: Internet
Author: User

Android can be set to adjust the scaling ratio with the window size, but even so, the mobile phone program designer must still 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
Display = new displaymetrics ();

Log. D ("yujian <", display. tostring (); // output directly here. The information is 0.

Getwindowmanager (). getdefaultdisplay (). getmetrics (Display); // put some information about the current window in the displaymetrics class,

Log. D ("yujian <", display. tostring (); // you can use the dispalymetrics class to obtain information about the current window.

Note: The constructor displaymetrics does not need to pass any parameters. The getdefadisplay display () method stores the obtained wide and high dimensions in the displaymetrics object, 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:

12-06 16:39:34. 946: D/yujian <(6731): displaymetrics
{Density = 1.0, width = 320, Height = 480, scaleddensity = 1.0,
Xdpi = 159.37254, ydpi = 160.42105}

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.