天天看點

Android之px、dp、sp之間互相轉換

在做Android的UI開發時,通常會用到三種度量機關——px、dp和sp。其中:

px:pixels,像素。不同裝置顯示效果相同。

dp(dip):device independent pixels,裝置獨立像素。 不同裝置有不同的顯示效果,和裝置硬體有關,不依賴像素。如果設定表示長度、高度等屬性時可以使用dp,與密度無關

sp:scaled pixels,放大像素。主要用于字型顯示(best for textsize)。設定字型,需要使用sp,sp除了與密度無關外,還與scale無關。

px、dp和sp之間的互相轉換如下:

/** 
     * 将px值轉換為dp(dip)值,保證尺寸大小不變 
     *  
     * @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 + f);  
    }  
           
/** 
     * 将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 + f);  
    } 
           
/** 
     * 将dp(dip)值轉換為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 + f);  
    }  
           
/** 
     * 将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 + f);  
    }  
           

其中在DisplayMetrics類中屬性density如下定義:

/**
     * The logical density of the display.  This is a scaling factor for the
     * Density Independent Pixel unit, where one DIP is one pixel on an
     * approximately  dpi screen (for example a x320, "x2" screen), 
     * providing the baseline of the system's display. Thus on a dpi screen 
     * this density value will be ; on a  dpi screen it would be ; etc.
     *  
     * <p>This value does not exactly follow the real screen size (as given by 
     * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
     * the overall UI in steps based on gross changes in the display dpi.  For 
     * example, a x320 screen will have a density of  even if its width is 
     * ", 1.3", etc. However, if the screen resolution is increased to 
     * x480 but the screen size remained "x2" then the density would be 
     * increased (probably to ).
     *
     * @see #DENSITY_DEFAULT
     */
    public float density;
           

在DisplayMetrics類中屬性scaledDensity如下定義:

/**
     * A scaling factor for fonts displayed on the display.  This is the same
     * as {@link #density}, except that it may be adjusted in smaller
     * increments at runtime based on a user preference for the font size.
     */
    public float scaledDensity;
           
上一篇: RSA加解密