My Android Advanced tour------>android about DP (DIP), SP turn PX Tool class

Source: Internet
Author: User

The following is a tool class that provides a way to convert between DP, SP, and PX.

Import android.content.context;/** * DP, SP convert to PX tool class <br> * <a href= "Http://blog.csdn.net/ouyang_peng" >  CSDN ouyangpeng</a> */public class Displayutil {/** * turns from px (pixel) units to DP */public static int Px2dip (CONTEXT) based on the resolution of the phone Context, float Pxvalue) {final float scale = context.getresources (). Getdisplaymetrics (). Density;return (int) (pxvalue/s Cale + 0.5f);} /** * Change from DP unit to PX (pixel) */public static int dip2px (context context, float dipvalue) According to the resolution of the phone {final float scale = Contex T.getresources (). Getdisplaymetrics (). Density;return (int) (Dipvalue * scale + 0.5f);} /** * Convert PX value to SP value, keep text size constant */public static int px2sp (context context, float Pxvalue) {final float Fontscale = context.getre Sources (). Getdisplaymetrics (). Scaleddensity;return (int) (Pxvalue/fontscale + 0.5f);} /** * Convert SP value to PX value, keep text size constant */public static int sp2px (context context, float Spvalue) {final float Fontscale = context.getre Sources (). Getdisplaymetrics (). Scaleddensity;return (int) (Spvalue * fontscale + 0.5f);}} 


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

Ouyangpeng welcome reprint, sharing with people is the source of progress!

Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng

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

Copyright NOTICE: This article for Bo Master original article, welcome reprint, reprint please indicate source Http://blog.csdn.net/ouyang_peng

My Android Advanced tour------>android about DP (DIP), SP turn PX Tool class

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.