It’s a unit in Android. Dip and DP are the same.
The official statement of Google is as follows:
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a “medium” density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application’s UI, to ensure proper display of your UI on screens with different densities.
That is to say, on a 160dpi screen, 1dip = 1px.
It is related to the screen density. If the screen density is large, 1dip represents more px. For example, on a 320dpi screen, 1dip = 2px.
Why should we use dip instead of PX in layout?
Because there are many mobile phones with different screen density in the world, what is the screen density? DPI is the number of pixels per unit length.
Imagine, if the size of these mobile phones is the same, and the screen density varies greatly, does it mean that one mobile phone has few pixels in the horizontal direction, and the other mobile phone has many pixels in the horizontal direction? When we draw the same number of pix, it will show up
Will the length of the indicator be different?
For example, the two mobile phones in the figure below, with 2px buttons set at the same time, will display smaller in the mobile phone with higher screen density.
Meanwhile, the button with the length of 2dip set at the same time will display the same size on both mobile phones.
So if you use PX as the unit in the app layout, then your app will appear strange phenomenon when running on various devices.
Let’s take a look at the effect on the emulator. I define two buttons, PX and dip.
That’s what it says in the layout file
The interface is as follows:
Getresources (). Getdisplaymetrics (). Densitydpi is the screen density.
Getresources (). Getdisplaymetrics (). Density can also be understood as how many Pxs does 1dip equal.
The DPI above is 240, 1dip = 1.5px
You see, 100dip button is 1.5 times longer than 100pxbutton.