Unit and resolution in Android Layout

Source: Internet
Author: User

First copy

Px: The pixel of the screen.

In: inches

MM: mm

PT: LB, 1/72 inch

DP: an abstract unit based on density. If a screen of DPI is displayed, 1dp = 1px.

Dip: equivalent to DP

SP: similar to DP, but it also scales according to the user's font size preference.

We recommend that you use SP as the unit of text, and dip for others.

Then I understand it myself:

Android screen density is based on 160,

When the screen density (densitydpi) is 160, 1 inch is divided into 160 parts, each of which is 1 pixel. if the screen density (densitydpi) is 240, 1 inch is divided into 240 parts, each of which is 1 pixel. 1 inch/160 (Machine X) = 1 inch/240 (machine y) = 1px

For example, if the screen density (densitydpi) of a 3 inch display machine is 160, that is, the density ratio (density) is 1.0, draw a line of 160dip and 160px, both are 1 inch. if the screen density (densitydpi) is 3 inch, that is, when the density ratio (density) is 320, 2.0 is 1 inch pixels, and 320 PX is half an inch. 160dip display is still 1 inch, because 1dip = 1px * density.

 

But have you noticed that for a screen with the same width, for example, 3 inch, if the density is 1.0, the resolution is 3 inch dip * X or 1.5 PX. If the resolution is, if the density is, the resolution is 480*1.5 = 720px * X or 480dip * X. (Here X represents the screen height ). if it is the same as 240dip, the two machines will show half the length, thus ensuring the proportion. in this case, if the resolution is 480 or 720, you can use the same layout file.

 

However, I have seen only 320*240 (0.75), 480*320 (1.0), 800*480 (1.5), 854*480 (1.5 ), for versions 480 and 800, the same line of 480 dip, 800 is displayed in full screen, and 480 is displayed in 800/1/(.5. 800/1. 5 is the total dip of the screen. we usually say that the resolution is in pixel PX units.

Therefore, even if dip is used, the layout ratio of machines with different resolutions cannot be exactly the same. but it is still beneficial. if PX is used, sometimes some la s are directly outside the screen, which is very troublesome for debugging.

Also, GoogleCodeThe unit used inside is the default unit of PX.

In the APK resource package, when the screen density is 240, the resources using the hdpi label

Resources that use the mdpi label when the screen density is 160

Ldpi tag resources are used when the screen density is 120.

1dp = 1px on a display at 160 o'clock per inch.

 

The following describes the mutual conversion of several different units.

Public static int dip2px (context, float dipvalue ){
Final float scale = context. getresources (). getdisplaymetrics (). density;
Return (INT) (dipvalue * scale + 0.5f );
}
Public static int px2dip (context, float pxvalue ){
Final float scale = context. getresource (). getdisplaymetrics (). density;
Return (INT) (pxvalue/scale + 0.5f );
}
Public static int dip2px (context, float dipvalue ){
Final float scale = context. getresources (). getdisplaymetrics (). density;
Return (INT) (dipvalue * scale + 0.5f );
}
Public static int px2dip (context, float pxvalue ){
Final float scale = context. getresource (). getdisplaymetrics (). density;
Return (INT) (pxvalue/scale + 0.5f );
}

The following describes how to obtain the Resolution:

Write the following code in the oncreate method of an activity:
Displaymetrics metric = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (metric );
Int width = metric. widthpixels; // screen width (pixels)
Int Height = metric. heightpixels; // screen height (pixels)
Float density = metric. density; // screen density (0.75/1.0/1.5)
Int densitydpi = metric. densitydpi; // screen density DPI (120/160/240)
This is quite simple, but have you tried it on a 800*480 machine? Is it 533 in width? Because the default density of Android is 1.0 at the beginning, you can add it to manifest. xml.

1. Uses-SDK node, <uses-SDK Android: minsdkversion = "4"/>, indicates that your APK cannot be installed on machines earlier than sdk1.6.

2. Supports-screens node.

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.