Today, I accidentally asked my colleague a question about the DP unit. Then, a series of questions caused by this question completely overturned my theory system on the DP.
My question is: Since the nature of DP is physical size, why not replace it with traditional length units such as cm or mm?
Then he replied that my DP is irrelevant to the pixel density... I did not care about this answer, but his next sentence completely shocked me. The sentence is like this: 320dp on your mobile phone is just full, 310dp is a little bit full screen.
My mobile phone Is HTC Desire. I have never heard of this theory, and then I immediately did a small experiment. In fact, it is true that I set a textview background to red, and the width to 320dp. I can see that the screen is full, 310dp is a little worse.
When I saw this test result, I collapsed again. I hope my colleague could say the second sentence is a beautiful mistake. I cannot accept what I have understood for so long, but the truth is cruel.
Android Developers I have read the DP document N times. I remember the PX and DP conversion formula: Px = DP * (DPI/160 ), however, after turning over the source code, we found that the DPI here is the normalized DPI, which may be 120 (low), 160 (medium), 240 (high) and 320 (xhigh). What I previously understood was the actual DPI of the device!
The actual DPI of G7 is 252. According to my previous understanding, the conversion of 310dp to PX should be: 310 * (252/160) = 488 pixels, while the horizontal direction of G7 is PX, in fact, Android does not use 252 as DPI for computing. Instead, G7 is regarded as an hdpi device, and then DPI is used to calculate the final pixel, therefore, on G7, 320dp is: 320 * (240/160) = 480 pixels, which is just full screen. 310dp is really a little worse.
After figuring out this problem, I felt a little better, but another problem came one after another:
The nature of DP is still physical, isn't it? Although the Android system treats DP as four types of devices: 120 (low), 160 (medium), 240 (high), 320 (xhigh), and 160dpi, 160dp is 1 inch. On 240dpi, 160dp is 1 inch, 120dpi and 320dpi are also 1 inch, although they occupy different segments, however, the final results are all within 1 inch of the screen. This system is actually very reasonable. The physical size occupied by a DP button on all devices should be the same, in this way, they are as big as the angle of the human eye, and the feeling of watching or reading is the same (let alone whether the background of the button is as detailed ). This should be the reason why the DP concept is introduced in the Android system, because the pixel density difference between the mobile phone screen is too big, and the Web is not applicable at all, what is the difference in pixel density between computer screens?
The ultimate question is, is there really only four devices with different pixel density in real life? Impossible. Although all these devices can be roughly divided into four density types: low, medium, high, and xhigh, for two devices with different pixel density in the same range, the physical sizes used by the same DP are different. For example, the screen size of G7 (HTC Desire) is 3.7 inch, the resolution is 480*800, the pixel density is 252 DPI, and the screen size of G10 (HTC Desire HD) is 4.3 inch, the resolution is 480*800, and the pixel density is 217 DPI. Assume that there is a button whose width is set to 100 DP. Since G7 and G10 are divided into hdpi, the actual width of the button is on the two devices.
* (240/160) = 150 pixels. Because the actual pixel density of the two devices is different, the actual display effect is as follows: the actual physical size of the button on the two devices is different. This is contrary to the concept that android entered DP.
You can say that this difference is very small for devices that belong to the same hdpi, but it is obvious between ldpi and hdpi. I agree very much, but if you do not use normalized DPI (that is, 120 (low), 160 (medium), 240 (high) when converting DP to PX) and 320 (xhigh), but the actual pixel density of the device. Although the number of pixels is different, the physical size shown in the end is indeed the same, in my opinion, this calculation method is loyal to the theory that the pixel density is irrelevant.
Finally, I want to say that if Android wants a button with a width of 1 inch DP to be big on any device, why not use inches as the measurement unit directly?
If you have a good idea, please leave a message.
Update:
When I answered a question from a factar user this afternoon, I finally found a reasonable answer to the "ultimate question" above. In the factar website, I found an important sentence:
"However, bitmap scaling can result in blurry or pixelated bitmaps, which you might notice in the above screenshots ."
This statement indicates that image blurring occurs when image resources are scaled. According to my analysis above, to ensure that the same image resources are identical in size on devices with different pixel density (this can be done completely, when DP is converted to PX, the physical pixel density parameter of the device is used.) the scaling factor of the image on different devices must be different, which leads to blurred images! Therefore, I guess Google has "almost the same size" of the same DP on different devices to ensure that the image will not be blurred ".
In addition, this answer also corrected my misunderstanding. Many application developers only use one hdpi resource or one xhdpi resource to reduce the installation package size, instead of mdpi resources or ldpi resources, it is feasible to implement scaling on the mdpi and ldpi devices, however, we should not ignore the image blur and poor display effects caused by scaling.