Android DPI computing and unit conversion

Source: Internet
Author: User

1. terms and concepts

Terms

Description

Remarks

Screen Size)

It refers to the actual physical size of the mobile phone, such as the commonly used 2.8 inch, 3.2 inch, 3.5 inch, 3.7 inch. The diagonal line length of the mobile phone screen.

Motorola's milestone mobile phone is 3.7 inch

Aspect Ratio (width/height ratio)

Refers to the actual physical size width/height ratio, divided into long and nolong

Milestone is and belongs to long

Resolution (resolution)

Similar to the definition of computer resolution, it refers to the number of pixels in the portrait and horizontal directions of the mobile phone screen.

Milestone is 854*480

DPI (DOT per inch)

Number of pixels per inch, such as 120 DPI and 320 DPI. Assume that the physical size of the qvga (240*2 inch) resolution screen is (1.5 inch * 160), DPI =

Can reflect the definition of the screen, used to scale the UI

Density (density)

The pixel value concentration in the screen. Resolution/screen size can reflect the cell phone density.

 

Density-independent pixel (DIP)

The Unit for calculating the logic density. The formula for the dip and the specific pixel value is dip/pixel = DPI/160.

 

 

2. DPI Value Calculation

For example, calculate the WVGA (800*480) Resolution, 3.7 inch density DPI, as shown in 1

 

Figure 1

Diagonal pixel indicates the pixel value of the diagonal line (=), DPI = 933/3. 7 = 252

 

 

 

3. Mobile phone screen Classification

 

3.1 is divided into the following three categories based on the cell phone screen density (DPI) or screen size, as shown in figure 2.

 

 

Figure 2

 

3. 2 The relationship between mobile phone screen classification and pixel density is shown in Table 1:

 

Lowdensity (120), ldpi

Medium Density (1, 160), mdpi

High density (240), hdpi

Small Screen

Qvga (240x320)

 

 

Normal Screen

Wqvga400 (240x400) wqvga432 (240x432)

Hvga (320x480)

Wvga800 (480x800) wvga854 (480X854)

Large Screen

 

Wvga800 * (480x800) wvga854 * (480X854)

 

Table 1

3.3 mobile phone size distribution (Http://developer.android.com/resources/dashboard/screens.htmlAs shown in figure 3, most mobile phone users with resolution 800*480 and 854*480

 Figure 3

4. uidesign

From the perspective of development, the application provides three sets of UI layout files based on the screen of three types of Android phones, but three sets of corresponding interface icons are also required, as shown in table 2.

Icon type

Standard asset sizes (in pixels), for Generalized screen Densities

 

Low Density screen (ldpi)

Medium Density screen (mdpi)

High density screen (hdpi)

Launcher

36x36 PX

48x48 PX

72X72 PX

Menu

36x36 PX

48x48 PX

72X72 PX

Status Bar

24x24 PX

32x32 px

48x48 PX

Tab

24x24 PX

32x32 px

48x48 PX

Dialog

24x24 PX

32x32 px

48x48 PX

List View

24x24 PX

32x32 px

48x48 PX

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.

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

Table 2

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.