Android -- describe that dip has nothing to do with screen Density

Source: Internet
Author: User

DIP/DP is commonly used in the UI layout of Android, according to the SDK documentation and various materials, "using a unit of length such as DP can be compatible with screens of different density" -- however, the understanding of this sentence is not understood until today, I am very ashamed of myself. I am here to record my blog. The following table lists the X horizontal direction of a layout to analyze the relationship between DP, PX, and visual senses:

The Conversion Relationship between DP and PX is: Px = (density/160) DP

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "250dp"
Android: layout_height = "100dp" Android: Background = "@ Android: color/White">
</Linearlayout>

Dpi dp px x-total X-inch visual ratio physical length

160 100 100 320 2-inch 0.3125 0.625 inch

240 100 150 480 2-inch 0.3125 0.625 inch

120 100 75 240 2-inch 0.3125 0.625 inch

120 100 75 480 4-inch 0.15625 0.625 inch

192 100 120 768 4-inch 0.15625 0.625 inch

192 100 120 800 4.16-inch 0.15 0.625 inch

Conclusion: the physical size of the screen actually reflected by the formula of PX and DP varies with the physical density. DPI, X-total, and X-inch are closely related, if one of the three remains unchanged, the other two will inevitably change synchronously. According to the classification of these changes, the so-called DP has nothing to do with the screen density and has two meanings:

1. It refers to the same physical size (for example, 4-inch or 2-inch ), ui components with the same Dp value are visually proportional to human eyes on the screen with different physical density. For example, in the two screens with a physical length of 4-inch but a density of 120 and 192 respectively, the UI parts with a length of 15.625% DP are visually proportional to in the horizontal direction.

2. In different physical sizes (for example, a screen with 4-inch and a screen with 2-inch ), ui components with the same Dp value occupy the same physical size on the screen with different physical density. For example, all the UI components in the table above are DP, regardless of DPI, the physical length of X-inch is always 0.625 inch.However, in this case, the visual proportions and senses of the UI parts on various screens are different. In the past, when I encountered such a situation, I wondered the relationship between DP and screen compatibility..

Therefore, when setting the simulator parameters and the layout preview view configuration in the ADT plug-in, you should correctly set the parameters based on the above association conditions, in order to make the UI layout in the simulator and preview interface share the visual senses

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.