px, pt, dp and sp

I have been learning Android for a few months, but I still can't distinguish px, pt, dp and sp. It gets confused every time, so this time I just made my own notes.

px means pixel, that is, the smallest element unit that can be displayed on the screen. Anything visible in our application is composed of pixels.

pt means points, 1 point is equal to 1/72 of an inch, and pt is generally used as a unit of font.

        In the past, when using px and pt on PC, it can be said that it is very handy and can dress up the program beautifully. But now when it comes to mobile phones, these two units seem to be a bit powerless, because mobile phones have different resolutions, a 200px wide button may occupy nearly the full screen on a low-resolution mobile phone, and a high-resolution mobile phone. may only occupy half of the screen.

Google, of course, realized this headache and introduced a new set of units dp and sp for Android.

dp stands for Density Independent Pixel, also known as dip. Compared with px, its display ratio will remain the same on screens of different densities.

sp means scalable pixel. It adopts the same design concept as dp and solves the problem of text size adaptation.

Here is a new term that needs our attention. What is density ? Density in Android is the number of pixels per inch of the screen, usually measured in dpi. For example, a mobile phone screen has a width of 2 inches and a length of 3 inches. If its resolution is 320*480 pixels, then the density of the screen is 160dpi, and if its resolution is 640*960, then the density of the screen is 320dpi , so a screen with a higher density value will display a finer effect.

         According to Android regulations, on a 160dpi screen, 1dp is equal to 1px, and on a 320dpi screen, 1dp is equal to 2px. Therefore, using dp to specify the width and height of the control can ensure that the display ratio of the control in screens of different densities remains consistent. (I was reading "The First Line of Code" recently, which is also summarized in the book)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325861909&siteId=291194637