Explanation of dpi/screen-size on android

Source: Internet
Author: User

Explanation of dpi/screen-size on android
Android defines four screen-size: small normal large xlarge and six dpi levels: ldpi (low )~ 120 dpimdpi (medium )~ 160 dpihdpi (high )~ 240 dpixhdpi (extra-high )~ 320 dpixxhdpi (extra-high )~ 480 dpixxxhdpi (extra-high )~ 640 dpi defines these levels to better organize resources to adapt to different devices. See: The http://developer.android.com/guide/practices/screens_support.html#range system will automatically load the appropriate resources based on the current device level when running our app, and we need to prepare their own resources for different levels, for example, a layout with two views in parallel is provided for a large screen, while only one view is displayed on a small screen at a time. To achieve this, we must know how to determine the level of a device? Then, we can put the corresponding resources in the correct directory. First, check the screen size. This level affects the selection of layout templates, that is, specify an R. layout. in id_XXX, is it loaded from res/layout or from res/layout-large? First look at the official figure: the first row is about the size of the file, where each file overlaps, that is, a rough range. However, we know that the loading logic when the program runs must be accurate, and a definite value must be obtained for each device to determine the level of the device before knowing which resources to load. In other words, if we have a 3.3 mobile phone, is it small or normal? It cannot be judged. Therefore, a more accurate calculation method is required, and this method involves dpi. Dpi: dot per inch refers to the number of pixels per inch. to calculate this value, you must know its physical size and resolution (that is, the actual number of pixels in the full screen, for example, if we build a virtual device, the physical resolution is 720x1280, and the physical size is 4.7. "You may be wondering, does physical size have width and height? Why is it a value? This 4.7 "is the diagonal line length. Use diagonal lines for nominal purposes. The advantage is to allow a wider combination of width and height. To calculate the dpi, We can't calculate it according to common sense, because one diagonal value cannot determine the unique width and height. But in another way, the area density is changed to the line density, that is, the number of pixels on the diagonal line is divided by the diagonal line length. The value should be consistent, so the dpi calculation is as follows: dpi = sqrt (pixel_width ^ 2 + pixel_height ^ 2)/diagonal = sqrt (720 ^ 2 + 1280 ^ 2)/4.7 = 312.5 as shown in the table above, the dpi level is xhdpi, it is consistent with that displayed on the tool interface. Once dpi is known, the directory for loading image resources will be known. In this example, res/drawable-xhdpi is used. But what about layout resources? Layout's selection criteria are different from those of image resources. The dpi level is not the standard, but the screen size level. The above explanation of dpi is inserted when we talk about the screen size classification method, so that we can clarify its meaning here. In fact, the official screen size classification standards are as follows: xlarge screens are at least 960dp x 720 dplarge screens are at least 640dp x 480 dpnormal screens are at least 470dp x 320 dpsmall screens are at least limit DP x 320dp here the width and high value are actually given and there is no separate comparison significance, the final result is the product of it. That is to say, the minimum screen must have 426*320 = 136,320 dp, and at least 470*320 = 150,400 dp to become a medium screen. The list is as follows: smal = 136,320 normal = 150,400 large = 307,200 xlarge = 691,200 now the question is, what is dp and what is the relationship with dpi? For the above example, how many dp does this device have? Let's look back at dpi, which is a ratio value, which refers to the number of pixels per inch. This is an attribute that describes the capabilities of devices. However, in many cases, the absolute value of the ability is not intuitive for the average person's cognitive understanding (although this example is not suitable, dpi is still intuitive), and its relative value is more meaningful. Then, the first android device is selected as the benchmark, and its dpi is 160. The dpi of other devices is larger than 160, that is, the (dp-) scale of the device, the device's dp is defined as: dpx = pixel_width/scale dpy = pixel_height/scale in this example, the scale = 312.5/160 = 1.95, dpx = 720/scale = 369.2, dpy = 1280/scale = 646.4 is measured in dp units. This virtual device has a screen of 369x646 and can now calculate its screen size level: dp = dpx * dpy = 369*646 = 238,374 the preceding table shows that the screen size is between 150,400 and 307,200. Therefore, the screen size is normal. When layout is loaded, priority is given to resources in res/layout (rather than small/large/xlarge ). After the definition and calculation process are clear, let's look back at the meaning of dp. Why do we need to define such a unit in such a tortuous way. 1. In fact, from physical resolution to dp (that is, screen size), the goal is to solve the problem of "how many points are there on the screen, then how many points do we have to do on the ui panel. 2. Why not directly use "physical pixels? Because the physical size and resolution ratio of different devices are very different, the extreme situation is that two devices with the same resolution have half the size difference, for example: if the size of a panel is defined as 400*4.7 Px in physical pixels, that is, half the full screen, the size of the Panel will look right on the 10 "tablet, but on the" phone, that is a small one-dimensional structure. The physical footprint of this dimension is only 1/16 square meters on the tablet! If the visual effects on the tablet are reasonable, it is hard to imagine how the naked eye adapts to these 1/16 homogeneous elements. But what is the result if we use dp as the unit? (The calculation results can be easily obtained on this page: https://www.sven.de/dpi/) on the former, dpi = 149.5, scale ~ = 1.0, screen size (dp) = 800*1280 for the latter, dpi = 317.6, scale ~ = 2.0, screen size (dp) = 384*640 if we need an area covering 1/4 square meters (1/2 square meters in width and height) in tablet, the dp size should be 400*640. Now, the occupied space on the phone is calculated. It is first converted to the pixel size, w = 400*2.0 = 800, h = 640*2.0 = 1280 that is, this is a 800*1280 pixel area, which is almost the size of the entire Phone screen (considering the rounding error during scale Calculation above, the physical size is exactly the same as the size of 1/4 on the tablet. It can be seen that the advantage of using dp to describe the size is that the physical area of the area is the same as that of the screen with different resolutions and physical sizes! The physical area is an important measure of the comfort level observed by the naked eye-not just the resolution. I believe everyone has some experience. Although many HD mobile phones are delicate enough, however, if you are looking at an e-book, it is far less eye-catching than a tablet with a lower resolution but a larger size.) now, after the meaning and benefits of dp are clear, there is another question: how much dp should a panel be designed? Dp is a virtual unit. How can we estimate the size of a virtual unit intuitively?

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.