Detailed screen size acquisition in Android applications and conversion of DP and PX values _android

Source: Internet
Author: User

Get screen Size

Get through WindowManager

Displaymetrics dm = new Displaymetrics ();

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

where display = Getwindowmanager () Getdefaultdisplay () obtains a Defaultdisplay object; Then Display.getmetrics (DM) to the screen size information assigned to displaymetrics DM

//Note: WindowManager sometimes need to pass the context. Getsystemservice get: WindowManager wm = (WindowManager) context.getsystemservice (context. Window_service);

The relationship between Displaymetrics and display.

Display refers to the object that displays the area, it may be a real physical screen, or it may refer only to the display area of the application, for example, in a non full-screen activity, because the system has a status bar, so the display area is smaller than the physical screen. The Displaymetrics encapsulates various property values for the display area. Check the source found that in displaymetrics the annotation for each attribute value is described as a real physical dimension. It is also found that the function of Display.getmetrics (DM) is basically applied to get the real screen size. It is good to remember that.

Note: The constructor displaymetrics does not need to pass any arguments, and after calling Getwindowmanager () the handle of the existing activity is obtained. Diplay then stores the resulting wide-high dimension in the Displaymetrics object, and the resulting wide-high dimension is in pixels (Pixel), and "pixel" refers to "absolute pixels" rather than "relative pixels."

The following information can be obtained through the Displaymetrics object DM

width = dm.widthpixels;

Height = dm.heightpixels;

xdpi = dm.xdpi;

ydpi = dm.ydpi;

density = dm.densitydpi;

fdensity = dm.density;

Converts DP and PX to an instance of the corresponding PX value:

int padding =4

padding = (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 4,

Context.getresources (). Getdisplaymetrics ());


Explain:
The unit of padding is: dip, the size of padding is: 4

Although it is 4dip, it is not the real unit of the final padding.

He calculates a value by multiplying the 4DP and the density factor, and the padding unit is actually the pixel, which is its width. At different DPI screens, this value is not the same.

That is, the DP is converted to PX, which returns the PX value corresponding to a DP.

If this is a complex_unix_sp, it is said that SP translates to DP.

The conversion between units is used in this way and can be encapsulated into a tool method.

Further understanding:
The Applydimension method converts 4 pixels to 6DP (480x800), 4DP (320x800), 3DP (240x320), so the returned values correspond to different resolutions (obtained by Getdisplaymetrics) are 6, 4, 3

Source:

public static float applydimension (int unit, float value,

displaymetrics metrics)

{

switch (unit) {

Case COMPLEX_UNIT_PX: Return

value;

Case COMPLEX_UNIT_DIP: Return

value * metrics.density;

Case COMPLEX_UNIT_SP: Return

value * metrics.scaleddensity;

Case COMPLEX_UNIT_PT: Return

value * metrics.xdpi * (1.0F/72);

Case COMPLEX_UNIT_IN: Return

value * METRICS.XDPI;

Case COMPLEX_UNIT_MM: Return

value * metrics.xdpi * (1.0f/25.4f);

}

return 0;

}

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.