Android px, DP, SP convert to and from each other

Source: Internet
Author: User

DP (DIP): device independent pixels (unit 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 is also a dip, which is basically similar to SP.

You can use a DP or SP if you set properties that represent length, height, and so on. However, if you set the font, you need to use the SP. The DP is not density-independent, and the SP is not related to scale, except for density. If the screen density is 160, then the DP and SP and PX are the same. 1DP=1SP=1PX, but if you use PX as the unit, if the screen size is constant (assuming 3.2 inches), the screen density becomes 320.

So the original width of the TextView is set to 160px, in the density of 320 of the 3.2-inch screen is more than the density of 160 of the 3.2-inch screen to see the half-short. But if it is set to 160DP or 160SP. The Width property value is automatically set to 320px. That means 160 * 320/160. 320/160 of these are called density scale factors. That is, if you use the DP and SP, the system will automatically convert based on the change in screen density.


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.

The conversion code is as follows

DP (DIP): device independent pixels (unit 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 is also a dip, which is basically similar to SP. You can use a DP or SP if you set properties that represent length, height, and so on. However, if you set the font, you need to use the SP. The DP is not density-independent, and the SP is not related to scale, except for density. If the screen density is 160, then the DP and SP and PX are the same. 1DP=1SP=1PX, but if you use PX as the unit, if the screen size is constant (assuming 3.2 inches), the screen density becomes 320. So the original width of the TextView is set to 160px, in the density of 320 of the 3.2-inch screen is more than the density of 160 of the 3.2-inch screen to see the half-short. But if it is set to 160DP or 160SP. The Width property value is automatically set to 320px. That means 160 * 320/160. 320/160 of these are called density scale factors. That is, if you use the DP and SP, the system will automatically convert based on the change in screen density. 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 (magnification pixels). Mainly used for font display of best for textsize. The conversion code is as follows/** * DP, SP conversion to PX Tool class * * @author Fxsky 2012.11.12 * */public class Displayuti          L {/** * converts PX value to dip or DP value, guaranteed size unchanged * * @param pxvalue * @param scale * (attribute density in Displaymetrics Class) * @return * * public static int Px2dip (context context         , float Pxvalue) {     Final float scale = context.getresources (). Getdisplaymetrics (). density;          return (int) (Pxvalue/scale + 0.5f);          /** * Convert dip or DP value to PX value, ensure size unchanged * * @param dipvalue * @param scale * (attribute density in Displaymetrics Class) * @return * * public static int dip2px (context Co              ntext, float dipvalue) {final float scale = context.getresources (). Getdisplaymetrics (). density;          return (int) (Dipvalue * scale + 0.5f);          /** * Convert PX value to SP value, keep text size unchanged * * @param pxvalue * @param fontscale * (Attribute scaleddensity in Displaymetrics Class) * @return * * public static int PX2SP (Contex               T context, float Pxvalue) {final float Fontscale = context.getresources (). Getdisplaymetrics (). scaleddensity;          return (int) (Pxvalue/fontscale + 0.5f);             }   /** * Convert SP value to PX value, keep text size unchanged * * @param spvalue * @param fontscale * (Attribute scaleddensity in Displaymetrics Class) * @return */public static int sp2px (context context, Flo              At Spvalue) {final float Fontscale = context.getresources (). Getdisplaymetrics (). scaleddensity;          return (int) (Spvalue * fontscale + 0.5f);   }      }

Android px, DP, SP convert to and from each other

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.