Android units and the screen resolution detailed

Source: Internet
Author: User
Tags final relative

One, commonly used units: relative units mainly: PX, SP, DP
absolute units mainly include: PT, in, MM

Second, the Unit application summary: The general use relative unit, but not absolute unit
1, the size of the general use of the SP, the font of this unit can be set according to the user font size and automatic scaling

2, space and other relative distances generally use DP (DIP), with the density changes, the corresponding number of pixels also change, but there is no direct relative proportion of the change.

3, PX and the actual pixel-related, and density-related! DP and SP are not related to the actual pixel, for a certain resolution but different density of the screen, PX unit application may lead to a change in the relative proportions of the length.

Third, density and resolution:
the density value represents the number of display points per inch, and the resolution is two concepts.

Its screen density standard is: HVGA screen DENSITY=160;QVGA screen DENSITY=120;WVGA screen DENSITY=240;WQVGA screen density=120

Specific application operational relationship: Assuming the resolution is x*y, the density is D, the screen actual size is a*b so the relationship is X*y = d * A * B (approximately equals)

The screen resolution information under different density, take 480dip*800dip WVGA (density=240) as an example density=120 when the actual resolution of the screen is 240px*400px (two points corresponding to a resolution)

Four, the comparison summary:
1, in the same density (that is, the same physical screen) in the case of different resolution, the entity density independent of the relative unit SP and DP display normal

2, in the same resolution of different densities, because the general situation, are used in the standard density, so the significance of the analysis is not very large

Other information:
px: Is the pixel point of the screen
In: Inch
MM: MM
PT: lb, 1/72 inches
DP: An abstract unit based on density, if a 160dpi screen, 1dp=1px
Dip: Equivalent to DP
SP: similar to DP, but also scaled according to the user's font size preference.
It is recommended to use SP as the unit of text, others with dip
for the dip and PX relationship, do the following overview:
HVGA Screen DENSITY=160;QVGA screen DENSITY=120;WVGA screen DENSITY=240;WQVGA screen density=120
The density value represents the number of display points per inch, and the resolution is two concepts.
Screen resolution information under different density, taking 480dip*800dip's WVGA (density=240) as an example

density=120 when the actual screen resolution is 240px*400px (two points corresponding to a resolution)
The status bar and title bar are 19px or 25dip high
Horizontal screen is screen width 400px or 800dip, working area height 211px or 480dip
Screen width 240px or 480dip, height of work area 381px or 775dip in vertical screen

Density=160 the actual resolution of the screen is 320px*533px (3 points correspond to two resolutions)
Status bar and title bar tall 25px or 25dip
Horizontal screen is screen width 533px or 800dip, working area height 295px or 480dip
Screen width 320px or 480dip, height of work area 508px or 775dip in vertical screen

Density=240 the actual resolution of the screen is 480px*800px (one point for a resolution)
Status bar and title bar tall 38px or 25dip
Horizontal screen is screen width 800px or 800dip, working area height 442px or 480dip
Screen width 480px or 480dip, height of work area 762px or 775dip in vertical screen

Resources in the APK resource bundle that use the hdpi tag when the screen is density=240
Resources that use MDPI tags when the screen is density=160
Use LDPI label resources when the screen is density=120.
Resources that are not tagged are shared in a variety of resolution scenarios.
The layout of the use of unit dip, less use px

Here are the transitions between several different units.

Copy Code code as follows:


public static int dip2px (context context, float Dipvalue) {


Final Float scale = context.getresources (). Getdisplaymetrics (). density;


return (int) (Dipvalue * scale + 0.5f);


}


public static int Px2dip (context context, float Pxvalue) {


Final Float scale = Context.getresource (). Getdisplaymetrics (). density;


return (int) (Pxvalue/scale + 0.5f);


}


public static int dip2px (context context, float Dipvalue) {


Final Float scale = context.getresources (). Getdisplaymetrics (). density;


return (int) (Dipvalue * scale + 0.5f);


}


public static int Px2dip (context context, float Pxvalue) {


Final Float scale = Context.getresource (). Getdisplaymetrics (). density;


return (int) (Pxvalue/scale + 0.5f);


}


here's how to get the resolution:

In the OnCreate method of an activity, write the following code:

Copy Code code as follows:


displaymetrics metric = new Displaymetrics ();


Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric);


int width = metric.widthpixels; Screen width (pixel)


int height = metric.heightpixels; Screen height (pixel)


float density = metric.density; Screen density (0.75/1.0/1.5)


int densitydpi = metric.densitydpi; Screen density dpi (120/160/240)


It's quite simple, but have you ever tried it on a 800*480 machine, and the width is 533? Since Android started with the default density is 1.0, you can add manifest.xml

1.USES-SDK node, <uses-sdk android:minsdkversion= "4"/>, means that the following machines are not sdk1.6 to install your apk.

2.supports-screens node.

Copy Code code as follows:


<supports-screens
Android:smallscreens= "true"
Android:normalscreens= "true"
Android:largescreens= "true"
Android:resizeable= "true"
Android:anydensity= "true"/>

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.