Android displaymetrics Get screen-related information

Source: Internet
Author: User

Android source code in this way to describe the displaymetrics.

/** * A structure describing general information on a display, such as its * size, density, and font scaling. * <p>to access the Displaymetrics members, initialize a object like this:</p> * <pre> displaymetrics Me Trics = new Displaymetrics (); * Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics);</pre> * *

As you can see in the displaymetrics note, we just write an example to test everything. I'm using Xiaomi 3:

Displaymetrics metrics =NewDisplaymetrics ();        Getactivity (). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics); /*** The logical density of the display. This was a scaling factor for the * Density independent Pixel Unit, where one DIP was one Pixel on an * appr oximately dpi screen (for example a 240x320, 1.5 "x2" screen), * Providing the baseline of the system ' s display . Thus on a 160dpi screens * This density value would be 1; On a-DPI screen it would is. 75;         etc. * * <p>this value does not exactly follow the real screen size (as given by * {@link#xdpi} and {@link#ydpi}, but rather was used to scale the size of * The overall UI in steps based on gross changes in the DISPL  Ay dpi. For * example, a 240x320 screens would have a density of 1 even if their width is * 1.8 ", 1.3", etc. However, if the screen resolution are increased to * 320x480 but the screens size remained 1.5 "x2" then the density         Would is * increased (probably to 1.5).         * * Density is a logical screen density, agreed on a 160dpi device, 1px=1dip. * Density = densitydpi/160 * *@see#DENSITY_DEFAULT*/        floatDensity =metrics.density; /*** The screen density expressed as dots-per-inch. May is either * {@link#DENSITY_LOW}, {@link#DENSITY_MEDIUM}, or {@link#DENSITY_HIGH}. * DENSITYDPI indicates the number of dots per inch (not the number of pixels)*/        intDENSITYDPI =metrics.densitydpi; /*** The absolute height of the display in pixels.         * The absolute height of the screen, in pixels. */        intHeightpixels =Metrics.heightpixels; /*** The absolute width of the display in pixels.         * Again, this is the absolute width of the screen, in pixels. */        intWidthpixels =Metrics.widthpixels; /*** The exact physical pixels per inch of the screen in the X dimension.         * Horizontally every inch of the exact physical pixel, we can try to calculate the width of the screen by this value and Widthpixels. */        floatXDPI =metrics.xdpi; /*** The exact physical pixels per inch of the screen in the Y dimension.         * Horizontally every inch of the exact physical pixel, we can try to calculate the height of the screen by this value and Heightpixels. */        floatYDPI =metrics.ydpi; /*** A scaling factor for fonts displayed on the display. this is the same * as {@link#density}, except that it is adjusted in smaller * increments at runtime based on a user preference for T         He font size. * Scaleddensity This value from the above comment, is the font-related factor, usually this value and density are equal, but if the user manually adjusted the * system font size, then this value is likely to change (in the case of Millet 3, the standard font, this value         =3, maximum font this value =3.25).         * So now in many cases, we write the font units as DP, although the fonts suggested by Google are written to sp. */        floatScaleddensity =metrics.scaleddensity; LOGUTIL.LOGD (TAG,"Metrics.density =" +density); LOGUTIL.LOGD (TAG,"METRICS.DENSITYDPI =" +densitydpi); LOGUTIL.LOGD (TAG,"Metrics.heightpixels =" +heightpixels); LOGUTIL.LOGD (TAG,"Metrics.widthpixels =" +widthpixels); LOGUTIL.LOGD (TAG,"METRICS.XDPI =" +xdpi); LOGUTIL.LOGD (TAG,"METRICS.YDPI =" +ydpi); LOGUTIL.LOGD (TAG,"Metrics.scaleddensity =" +scaleddensity); //to calculate how many inches the phone is.        floatPixelstodipwidth = widthpixels/xdpi; floatPixelstodipheight = heightpixels/ydpi; LOGUTIL.LOGD (TAG,"Pixelstodipwidth =" +pixelstodipwidth); LOGUTIL.LOGD (TAG,"Pixelstodipheight =" +pixelstodipheight); DoubleMobileinch = math.sqrt (pixelstodipheight * pixelstodipheight + pixelstodipwidth *pixelstodipwidth); //I use the millet 3, get the value is 4.917646062686045LOGUTIL.LOGD (TAG, "mobileinch =" + Mobileinch);

With this example, we understand what the dip and px represent, and we can write a way to convert the dip and PX to each other. (Dip = px/density)

    /*** Switch from DP unit to PX (pixel) According to the resolution of the phone*/     Public Static intDIP2PX (Context context,floatdpvalue) {        Final floatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Dpvalue * scale + 0.5f); }    /*** According to the resolution of the phone from PX (pixel) to the unit to become DP*/     Public Static intPx2dip (Context context,floatpxvalue) {        Final floatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Pxvalue/scale + 0.5f); }

The above added to this 0.5f reason, Google official documents in fact there are:

Then add 0.5f to round the number of the nearest whole number, when converting to an integer.

To be rounded up ...

For example, I have the above examples:

Mobileinch = 4.917646062686045, I +0.5f (=5.417646062686045), and then after rounding, I get 5.


Android displaymetrics Get screen-related information

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.