Share Android on dip, DP, PX, SP and other units detailed

Source: Internet
Author: User

[Dip:device independent pixels (device independent pixel).

Different devices have different display effects, this is related to the device hardware, we generally support the WVGA, HVGA and QVGA recommended to use this, do not rely on pixels.

Px:pixels (Pixel). Different devices display the same effect, generally we hvga represent 320x480 pixels, this use more.

Pt:point, is a standard length unit, 1pt=1/72 inches, used in the printing industry, is very simple and easy to use;

sp:scaled pixels (zoom in pixels). Mainly used for font display best for textsize.

=========================

About conversions (Take SP and PT for example)

See TextView and other types of source code, know:

Case COMPLEX_UNIT_PX:

return value;

Case COMPLEX_UNIT_SP:

return value * metrics.scaleddensity;

Case COMPLEX_UNIT_PT:

return value * metrics.xdpi * (1.0F/72);

--------------------------

Scaleddensity = Density_device/(float) density_default;

xdpi = Density_device;

--------------------------

Density_default = Density_medium = 160;

============================================

So: If the PT and SP take the same value of 1, you can set the coefficient of x between 1pt and 1SP,

1 * density_device/72 = x * 1 * density_device/160 =>

x = 160/72 = 2.2222

Which means that in Android, 1pt is about 2.22SP.

===================================================================

What is dip and SP

In the past, programmers used to design computer user interfaces in pixel units. For example, define a table with a width of 300 pixels

A single field, the spacing between columns is 5 pixels, the icon size is 16x16 pixels, and so on. The problem with this is that, as

If you run the program on a new monitor that is higher in dots per inch (dpi), the user interface appears small. In a

In some cases, the user interface may be so small that it is difficult to see the content.

Resolution-independent units of measurement can solve this problem. Android supports all of the following units.

PX (pixels): A point on the screen.

In (inches): unit of length.

MM (mm): unit of length.

PT (lb): 1/72 inches.

DP (density-independent pixels): An abstract unit based on screen density. At 160 dots per inch, the 1dp= 1px.

Dip: Same as DP, used in android/ophone example.

SP (Pixel-independent): Similar to a DP, but can be scaled based on the user's font size preference.

In order for the user interface to be displayed properly on the current and future display types, it is recommended that you always use the SP as the size of the text, and dip as the unit of the other elements. Of course, you can also consider using vector graphics, rather than using bitmaps


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.