Android px、dp、sp之间相互转换 系统默认12 sp

 

px  就是像素 

sp=dpX字体比例(1.25f)

一、dp(或者dip device independent pixels)

一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp=1px。不同设备有不同的显示效果,这个和设备硬件有关。

android里的代码如下:

 

[java] view plain copy
 
  1. <span style="font-size: 14px; color: rgb(51, 51, 51);">// 文件位置:android4.0\frameworks\base\core\java\android\util\DisplayMetrics.java  
  2.     public static final int DENSITY_DEVICE = getDeviceDensity();  
  3.     public float density;  
  4.       
  5.     public void setToDefaults() {  
  6.         widthPixels = 0;  
  7.         heightPixels = 0;  
  8.         density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; // 这里dp用的比例   
  9.         densityDpi = DENSITY_DEVICE;  
  10.         scaledDensity = density; // 这是sp用的比例  
  11.         xdpi = DENSITY_DEVICE;  
  12.         ydpi = DENSITY_DEVICE;  
  13.         noncompatWidthPixels = 0;  
  14.         noncompatHeightPixels = 0;  
  15.     }  
  16.   
  17.     private static int getDeviceDensity() {  
  18.         // qemu.sf.lcd_density can be used to override ro.sf.lcd_density  
  19.         // when running in the emulator, allowing for dynamic configurations.  
  20.         // The reason for this is that ro.sf.lcd_density is write-once and is  
  21.         // set by the init process when it parses build.prop before anything else.  
  22.         return SystemProperties.getInt("qemu.sf.lcd_density",  
  23.                 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); // 从系统属性ro.sf.lcd_density里获取屏幕密度  
  24.                   
  25. // 文件位置:android4.0\packages\inputmethods\latinime\java\src\com\android\inputmethod\latin\Utils.java  
  26.     public static float getDipScale(Context context) {  
  27.         final float scale = context.getResources().getDisplayMetrics().density;  
  28.         return scale;  
  29.     }  
  30.   
  31.     public static int dipToPixel(float scale, int dip) {  
  32.         return (int) (dip * scale + 0.5); // dip到px的换算公式  
  33.     }                </span>  


二、sp(Scaled Pixels)

 

主要用于字体显示,与刻度无关的一种像素,与dp类似,但是可以根据用户的字体大小首选项进行缩放。

 

[java] view plain copy
 
    1. <span style="color:#333333;">// 文件位置:android4.0\packages\apps\settings\src\com\android\settings\Display.java  
    2.     private Spinner.OnItemSelectedListener mFontSizeChanged  
    3.                                     = new Spinner.OnItemSelectedListener() {  
    4.         public void onItemSelected(android.widget.AdapterView av, View v,  
    5.                                     int position, long id) {  
    6.             if (position == 0) {  // 下面是设置字体比例的代码  
    7.                 mCurConfig.fontScale = .75f;  
    8.             } else if (position == 2) {  
    9.                 mCurConfig.fontScale = 1.25f;  
    10.             } else {  
    11.                 mCurConfig.fontScale = 1.0f;  
    12.             }  
    13.   
    14.             updateFontScale();  
    15.         }  
    16.   
    17.         public void onNothingSelected(android.widget.AdapterView av) {  
    18.         }  
    19.     };  
    20.       
    21.     private void updateFontScale() {  
    22.         mDisplayMetrics.scaledDensity = mDisplayMetrics.density *  
    23.                 mCurConfig.fontScale; // 将设置的字体比例代码合到scaledDensity里去  
    24.   
    25.         float size = mTextSizeTyped.getDimension(mDisplayMetrics);  
    26.         mPreview.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);  
    27.     }                 </span>  

 

 

转换代码如下

[java] view plaincopy

    /** 
     * dp、sp 转换为 px 的工具类 
     *  
     * @author fxsky 2012.11.12 
     * 
     */  
    public class DisplayUtil {  
        /** 
         * 将px值转换为dip或dp值,保证尺寸大小不变 
         *  
         * @param pxValue 
         * @param scale 
         *            (DisplayMetrics类中属性density) 
         * @return 
         */  
        public static int px2dip(Context context, float pxValue) {  
            final float scale = context.getResources().getDisplayMetrics().density;  
            return (int) (pxValue / scale + 0.5f);  
        }  
      
        /** 
         * 将dip或dp值转换为px值,保证尺寸大小不变 
         *  
         * @param dipValue 
         * @param scale 
         *            (DisplayMetrics类中属性density) 
         * @return 
         */  
        public static int dip2px(Context context, float dipValue) {  
            final float scale = context.getResources().getDisplayMetrics().density;  
            return (int) (dipValue * scale + 0.5f);  
        }  
      
        /** 
         * 将px值转换为sp值,保证文字大小不变 
         *  
         * @param pxValue 
         * @param fontScale 
         *            (DisplayMetrics类中属性scaledDensity) 
         * @return 
         */  
        public static int px2sp(Context context, float pxValue) {  
            final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;  
            return (int) (pxValue / fontScale + 0.5f);  
        }  
      
        /** 
         * 将sp值转换为px值,保证文字大小不变 
         *  
         * @param spValue 
         * @param fontScale 
         *            (DisplayMetrics类中属性scaledDensity) 
         * @return 
         */  
        public static int sp2px(Context context, float spValue) {  
            final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;  
            return (int) (spValue * fontScale + 0.5f);  
        }  
    } 


//第二中转换方法

private int dp2px(int value) {
float v = getContext().getResources().getDisplayMetrics().density;
return (int) (v * value + 0.5f);
}

private int sp2px(int value) {
float v = getContext().getResources().getDisplayMetrics().scaledDensity;
return (int) (v * value + 0.5f);
}


posted @ 2017-02-22 10:39  陈都  阅读(348)  评论(0编辑  收藏  举报