Android Dev Dip, DP, PX, SP difference

Source: Internet
Author: User

Shows the difference between units px and dip and SP

Dip:device independent pixels (device independent pixels). Different devices have different display effects, this is related to the device hardware, generally we support WVGA, HVGA and QVGA recommend this, do not rely on pixels.

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

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

sp:scaled pixels (enlarge pixels). Mainly used for font display of best for textsize.

As a result, according to Google's advice, TextView's font size is best to use SP to do units, and view
TextView
The source shows that Android uses the SP as the font size unit by default.

###################################################################

About conversions (Take SP and PT for example)
See TextView and other types of source code, you 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: Assuming that 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

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

For reference, if the UI can provide the best design in SP, if there is no SP in the design
, developers can also take approximate values with appropriate conversions.



Reprinted content: http://hi.baidu.com/lfcaolibin/blog/item/f3f60d1e438deefee0fe0bae.html
What are dip and SP

In the past, programmers typically designed computer user interfaces in pixels. For example, you define a form field with a width of 300 pixels, a space between columns of 5 pixels, an icon size of 16x16 pixels, and so on. The problem with this approach is that if you run the program on a new monitor with a higher dots per inch (dpi), the user interface will appear small. In some cases, the user interface may be too small to see the content.

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

PX (pixels): The 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. 1DP = 1px on a monitor 160 dots per inch.

Dip: Same as DP, more for android/ophone example.

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

In order for the user interface to display properly on the current and future display types, we recommend that you always use the SP as the unit of text size and dip as the unit of other elements. Of course, you can also consider using vector graphics instead of bitmaps

From: http://***/edu/906.html

Android Dev Dip, DP, PX, SP difference

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.