Android multi-screen adaptation (1)

Source: Internet
Author: User

Based on the official Android doc, we try to describe the android multi-screen adaptation technology in a concise way.

Screen Size)
Physical size of the diagonal line of the screen. Android divides all screen sizes into four categories: small, normal, large, and extra large.

Screen Density)
The number of pixels in the unit (screen) physical area, usually expressed by DPI (dots per inch), that is, the number of pixels per inch.
For example, a low-density screen contains fewer screens than a high-density screen.
Android classifies screen density into four types: low, medium, high, and extra high.

Orientation)
The screen direction can be landscape or portrait.

Resolution)
Total number of physical pixels on the screen. To support multiple screens, the application does not directly deal with the resolution, but only needs to care about the screen size and screen density.

Density independent pixel (density-independent pixel, abbreviated as DP)
DP is a virtual pixel unit. when defining the UI layout, you should use DP to define the layout in a mode with no screen density.
A dp is equal to a pixel on a 160dpi screen. At runtime, the system automatically scales the dimensions described by DP Based on the screen density.
The Conversion Relationship between DP and PX: Px = DP * (DPI/160 ). For example, on a DPI screen, 1dp = 1.5px

----------------------------------------

Formula for Calculating screen density = (resolution length ^ 2 + resolution width ^ 2) ^ (1/2)/screen size

That is to say, the number of records on the diagonal line is calculated based on the length and width of the Resolution (using the hook theorem), and the number of records on the diagonal line is divided by the diagonal line length (that is, the screen size)

For more information, see Android applications.DPI Calculator

The following uses several real machines to deepen the cognition of related concepts.

  Samsung i5801 HTC wildfire S (G13) HTC sensation (G14) Meizu M9
Resolution 240*400 320*480 540*960 640*960
Screen Size 3.2 inch 3.2 inch 4.3 inch 3.5 inch
Screen Density 145.77 180.27 256.15 329.65
DPI obtained by displaymetrics 120 160 240 320

The yellow part is the screen density calculated using the formula above, and the green part is the DPI obtained using displaymetrics.

There are two reasons for the inconsistency between the two data.

1. The screen size parameter provided by the vendor is not too precise

2. The density defined in displaymetrics is only 120, 160, 213, 240, and 320.

----------------------------------------

Here, we can push back the screen size from another angle. Taking meizu M9 as an example, through displaymetrics, we can get that both xdpi and ydpi are 325.12.

In this way, the screen width is 640/325. 12 = 1.9685 inch, and the screen height is 960/325. 12 = 2.9527 inch.

The diagonal line size is 3.5487 inch, calculated using the hook theorem.

There is a bit of doubt here, HTC wildfire S (G13) xdpi = 159.37254, ydpi = 160.42105, through the above method to calculate its screen size is 3.6 inch, and the actual phone screen size does not match. For other models, the calculation result does not match the actual size !?

----------------------------------------

Well, since DP is the recommended unit for Android layout and the height and width are specified in the layout in the unit of DP, why?

In http://developer.android.com/reference/android/util/displaymetrics.html, this example is provided when densityis used.

The logical density of the display. this is a scaling factor for the density independent pixel unit, where one dip is one pixel on an approximately 160 DPI screen (for example a 240x320, 1.5 "x 2" screen ), providing
The baseline of the system's display. Thus on a 160 DPI screen this density value will be 1; on a 120 DPI screen it wocould be. 75; etc.

This value does not exactly follow the real screen size (as givenxdpiAndydpi,
But rather is used to scale the size of the overall UI in steps based on gross changes in the display DPI. for example, a 240x320 screen will have a density of 1 even if its width is 1.8 ", 1.3", etc.

That is to say, on a 240*320 resolution screen, the density of the 1.5 "x 2", 1.8 "X2", and 1.3 "X2" screens is 1. The screen size is different and the number of pixels is the same. The pixel size must be different. If a fixed Dp value is specified, the corresponding height and width values must be different. However, android can ensure to the maximum extent that the difference is not too large when DP is used (this step is done for the dazzling screen size categories of Android hardware manufacturers, ).


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.