I know the differences among px, dpi, dp, dip, sp, in, and pt, pxdpi

Source: Internet
Author: User

I know the differences among px, dpi, dp, dip, sp, in, and pt, pxdpi
Px:

That is, pixels. 1px indicates a physical pixel on the screen;

The unit of px is not recommended, because the actual size of the same px image may be different on different mobile phones.

Dpi:

The abbreviation of dot per inch is the number of dots per inch, also known as screen density. The larger the value, the clearer the screen.
Suppose there is a mobile phone with a physical screen size of 1.5 inch x 2 inch and a screen resolution of 240x320, then we can calculate it on the screen of this mobile phone, the number of pixels per inch is 240/1. 5 = 160 dpi (horizontal) or 320/2 = 160 dpi (portrait), 160dpi is the pixel density of this mobile phone,The Unit dpi of pixel density is the abbreviation of Dots Per Inch, that is, the number of pixels Per Inch.. The horizontal and vertical values are the same, because most mobile phone screens use square pixels.

Summary: Cell Phone pixel density (dpi) = (horizontal/vertical) Resolution/(horizontal/vertical) screen physical size Dp:

This is the most common but hard to understand unit of size. It is closely related to "pixel density.

Different mobile phones/tablets may have different pixel density. For example, for a 4-inch mobile phone, x320 resolution and X resolution, the former has a relatively low pixel density.The Android system defines four pixel density types: low (120 dpi), medium (160 dpi), high (240 dpi), and ultra (320 dpi ), the coefficients from dp to px are 0.75, 1, 1.5, and 2 respectively. The coefficient multiplied by the dp length is the number of pixels.For example, if there is an image with a length of "80dp" on the interface, it is displayed as 80x1.5 = 120px on a DPI mobile phone, the actual display on the 320dpi mobile phone is 80x2 = 160px. If you compare the two mobile phones, you will find that the physical size of the image is "similar", which is the effect of using dp as the unit.

Conclusion: dp = coefficient of cell phone pixel density (dpi)/pixel number (px) Dip:

It is exactly the same as dp, but the name is different. Dip was used in earlier Android versions. We recommend that you use the dp name for consistency with sp.

Sp:

Scale-independent Pixel ). Sp is similar to dp, but the only difference is that the Android system allows users to customize the text size (small, normal, large, large, and so on ), when the text size is "normal", 1sp = 1dp = 0.00625 inch, and when the text size is "large" or "super large", 1sp> 1dp = 0.00625 inch. It is similar to the effect after we adjust the font size in windows-the window size remains unchanged, only the text size changes. As follows:

The system font is-normal-super-large-hour-ultra-hour:

Conclusion: when the system font is normal, sp and dp have the same size but will change according to the system font size. In:

That is, inch, 1 inch ≈ 2.54 cm;

Pt:

1pt = 1/72 inch ≈ 0.035 cm;

Conversion of dp and px:

So why do we need + 0.5f?

In the official document, Then add 0.5f to round the figure up to the nearest whole number, when converting to an integer.
The F value is used to convert to the int type,Assign the calculation result to the nearest positive number.

Reference connection: [Android Q & A] What are the differences between px, dp, and sp?

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.