Android-The DIP,SP,PX knowledge you need to learn about the development page, and the transitions between them

Source: Internet
Author: User

Work, there will always be a task to develop a new page, the general process is the product manager to determine the page and function to develop, and then the designer to provide the design manuscript, then by our developers to complete the development work.

Usually, the designer provides the design of the dimension will be very detailed, such as when it comes to words, word size, location, color, font, etc., will be one by one mark. Note that the designer offers all dimensions in px(pixels).

However, we can not use the PX directly in the development, according to the Android specification, the spacing needs to use DIP (DP), the font size needs to use the SP.

Then we need to make the PX and dip (DP), SP conversion.

I. Understanding DIP (DP), SP,PX

First, let's look at it.

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 .

DP: generally considered DP = dip, generally we use dip

sp:scaled pixels (enlarge pixels). Mainly used for font display. Ask a question, why don't we use dip to control font size? Instead, use the SP. This is described below.

PX: pixels, is an absolute value, different devices display the same effect, is how much is how much, will not change.

Therefore, to support different resolutions, we should use dip instead of PX to make page spacing. But there are special exceptions, such as we often need to draw a line in the page, as a split line, then the height of the line directly with 1px, here to use PX.

Two. Dip (DP), SP and PX conversion

When converting, the new concepts involved are resolution and density values, such as

  

Conversion formula:dip = px/mdpi Reference scale,SP = px/mdpi Reference Scale, note that the base scale for different resolutions is different.

For example, in the resolution of 1080, the scale value is 3, the design of the gap if it is 40px, then the corresponding dip is 13.3333dip (try to use a decimal point, more accurate). This display will be consistent with the design draft.

However, when I access the data on the Internet, I found that the SP calculation formula is slightly different, there is a formula, SP = (int) (px/mdpi + 0.5f), so that the last to get an integer, is the font size generally use an integer? After the practice found, or direct sp=px/mdpi, such as the font size is 40px, then 13.3333SP is the most accurate answer.

Three. When developing, ensure that the calculated spacing is consistent with the design draft?

At this time, we recommend that you use the tool is hierarchyviewer, through it, you can see the true px size at a glance, to ensure the correct development. For example

  

Four. A small question, why is the font size recommended for use with SP instead of dip?

This is because we usually have a font size adjustment in the phone settings, when the user adjusts the font size, such as from the standard to oversized, if your font using the SP, is the corresponding adjustment according to the conversion, if the dip, it will remain unchanged, the effect is not what we want.

-kevin Song

2016.5.29

Android-The DIP,SP,PX knowledge you need to learn about the development page, and the transitions between them

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.