Android on the dip, DP, PX, SP and other units description (turn)

Source: Internet
Author: User

Dip device independent pixels ( device independent pixels). Different device different display effect, this and device hardware, generally we support WVGA, HVGA and QVGA recommend using this, do not rely on pixels.

Programs developed on Android will run on phones with different resolutions. The concept of dip is introduced in order to make the program look less different. For example, define a rectangle of ten X 10dip. On a screen with a resolution of 160dpi, such as G1, it is exactly 10 pixels. The screen at the DPI is 15 pixels. The conversion formula is Pixs = dips * (density/160). Density is the resolution of the screen.

It is important to note that dip is related to screen density and that the screen density is related to specific hardware and that the hardware settings are incorrect and may cause the dip to not display properly. On the screen density of 160 display, 1dip=1px, sometimes your screen resolution is very large, such as 480*800, but the screen density is not set correctly for example or 160, then all the use of dip will show the anomaly, the basic display is too small.

Dip Conversion: dip (value) = (int) (PX (value)/1.5 + 0.5)

dp ( density-independent pixels): Same as dip. An abstract unit based on screen density. 1DP = 1px on a monitor 160 dots per inch. Dip is the same as DP and is used in the Android/ophone example.
This is the most common but also the most difficult to understand the size of units. It is closely related to pixel density, so let's first explain what pixel density is. Given a cell phone with a physical size of 1.5 inches x2 inches and a screen resolution of 240x320, we can calculate the number of pixels per inch on the screen of this phone as 240/1.5=160dpi (landscape) or 320/2=160dpi (portrait), 160DPI is the pixel density of this phone, and the pixel density unit dpi is the abbreviation for dots per inch, which is the number of pixels. This value is the same for both landscape and portrait, because most phone screens use a square pixel point.
Different cell phones/tablets may have different pixel densities, such as the same as 4-inch mobile phones, with 480x320 resolution also has 800x480 resolution, the former pixel density is relatively low. The Android system defines four pixel densities: low (120DPI), Medium (160DPI), High (240dpi), and Superelevation (320DPI), and their corresponding DP-to-PX coefficients are 0.75, 1, 1.5, and 2, which are multiplied by the DP length as the number of pixels. For example, the interface has a length of "100dp" picture, then it is actually displayed as 80x1.5=120px on the 240dpi mobile phone, on the 320dpi phone actually displayed as 80x2=160px. If you compare these two phones together, you will find the physical size of the picture "almost", which is the effect of using DP as unit

px pixels (pixels) on the screen point, different devices different display the same effect, this is absolute pixels, is how much will never change. Generally we HVGA represent 320x480 pixels, this is used more.

sp scaled pixels-best for text size ( magnification pixels) ( scale independent pixels): mainly handles font size. Similar to DP, but can be scaled based on the user's font size preference. Mainly used for font display of best for textsize. Thus, according to Google's suggestion, TextView's font size is best to use SP to do units, and view the source of TextView know that Android by default using the SP as the font size units.

Usage Recommendations:

According to Google's recommendation, pixels uniformly use the dip, the font unified use SP

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.

If the UI can provide the best design in terms of SP, if the design does not have the concept of an SP, then the developer can also take approximate values with appropriate conversions.

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.

Other units: (infrequently used)

pt(LB) point, is a standard length unit, 1pt=1/72 inch, for the printing industry, very simple to use; in Android, 1pt is roughly equal to 2.22SP

in inches (inches): unit of length.

mm millimeters (mm): unit of length.

To give an example of the difference between PX and dip:

PX is a pixel, if using PX, will use the actual pixel painting, compared to a bar, with a length of 240px of the horizontal line, in the 480 wide simulator is half the screen width, and in the 320 wide simulator is 2/3 of the screen width. Dip, that is, the height of the screen is divided into 480 points, the width divided into 320 points. For example, you make a 160dip horizontal line, whether you are in 320 or 480 of the simulator, is half the length of the screen.

Dip and PX Conversion methods:

     Public Static intDIP2PX (Context context,floatdipvalue) {FinalfloatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Dipvalue * scale +0.5f); }     Public Static intPx2dip (Context context,floatpxvalue) {FinalfloatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Pxvalue/scale +0.5f); }

Android on the dip, DP, PX, SP and other units description (turn)

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.