Android中px与sp,dp之间的单位转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ShenQiXiaYang/article/details/80519095

屏幕的尺寸信息

Android的手机屏幕,不管是分辨率还是大小都是五花八门、千奇百怪,这在一定程度上造成了绘图时的难度。要想在不同的屏幕上保持绘图的准确性,那我们必须对这些屏幕有充分的认识。

屏幕参数

一块屏幕通常具有以下几个参数

  • 屏幕大小
    指屏幕对角线的长度,通常使用“寸”来度量,例如:4.7寸手机、5.5寸手机等。
  • 分辨率
    分辨率是指手机屏幕的像素点个数,例如720 * 1280就是指屏幕的分辨率,指宽有720个像素点,而高有1280个像素点。
  • PPI
    每英寸像素(Pixels per Inch)又被称为DPI(Dots per Inch)。它是由对角线的像素点数除以屏幕大小得到的,通常达到400PPI就已经是非常高的屏幕密度了。

系统屏幕密度

每个厂商的Android手机具有不同的大小尺寸和像素密度的屏幕。Android系统如果要精确到每种DPI的屏幕,那基本是不可能的。因此,系统定义了几个标准的DPI值,作为手机的固定DPI,如下表所示:

密度 ldpi mdpi hdpi xhdpi xxhdpi
密度值 120 160 240 320 480
分辨率 240 * 320 320 * 480 480 * 800 720 * 1280 1080 * 1920

独立像素密度dp

正是由于各种屏幕密度的不同,导致同样像素大小的长度,在不同密度的屏幕上显示长度不同。因为相同长度的屏幕,高密度的屏幕包含更多的像素点。Android系统使用mdpi即密度值为160的屏幕作为标准,在这个屏幕上1px = 1dp。其他屏幕则可以通过比例进行换算,例如同样是100dp的长度,在mdpi中为100px,而在hdpi中为150px。我们也可以得出在各个密度值中的换算公式,在mdpi中1dp = 1px,在hdpi中1dp = 1.5px,在xhdpi中1dp = 2px,在xxhdpi中1dp = 3px。由此我们也可以得到各个分辨率直接的换算比例,即1dpi:mdpi:hdpi:xhdpi:xxhdpi = 3:4:6:8:12。

单位换算

可将这些代码作为工具类保存到项目中。【代码可直接进行粘贴复制使用】

/**
 * dp转换成px
 */
private int dp2px(Context context,float dpValue){

    float scale=context.getResources().getDisplayMetrics().density;
    return (int)(dpValue*scale+0.5f);

}

/**
 * px转换成dp
 */
private int px2dp(Context context,float pxValue){

    float scale=context.getResources().getDisplayMetrics().density;
    return (int)(pxValue/scale+0.5f);

}
/**
 * sp转换成px
 */
private int sp2px(Context context,float spValue){

    float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (spValue*fontScale+0.5f);

}
/**
 * px转换成sp
 */
private int px2sp(Context context,float pxValue){

    float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (pxValue/fontScale+0.5f);

}

上面的公式转换方法中,有的小伙伴不太了解为什么要+0.5f。这里我来解释一下。
+0.5f就是为了数值取整,也就是我们常说的四舍五入。
咱们再来看看官网是如何解释的:https://developer.android.com/guide/practices/screens_support.html

Converting dp units to pixel units

The DisplayMetrics.density field specifies the scale factor you must use to convert dp units to pixels, according to the current screen density. On a medium-density screen, DisplayMetrics.density equals 1.0; on a high-density screen it equals 1.5; on an extra-high-density screen, it equals 2.0; and on a low-density screen, it equals 0.75. This figure is the factor by which you should multiply the dp units on order to get the actual pixel count for the current screen. (Then add 0.5f to round the figure up to the nearest whole number, when converting to an integer.) For more information, refer to the DisplayMetrics class.

翻译过来就是:

将dp单元转换为像素单位

密度字段指定你必须使用的比例因子,根据当前的屏幕密度,将dp单位转换成像素。在中等密度的屏幕上,displaymetrics.密度等于1.0;在高密度的屏幕上,它等于1.5;在一个超高密度的屏幕上,它等于0;在低密度屏幕上,它等于0.75。这个图是你应该将dp单元相乘以得到当前屏幕的实际像素计数的因素。(然后加上0.5 f,将这个数字四舍五入到最接近整数,当转换为整数时。)要了解更多信息,请参考DisplayMetrics类。

同时,系统也提供了TypedValue类帮助我们转换,代码如下所示:

/*
    dp2px
*/
private int dp2px(Context context,int dpValue){

        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,context.getResources().getDisplayMetrics());

  }
/*
    sp2px
*/
private int sp2px(Context context,int spValue){

    return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,context.getResources().getDisplayMetrics());

}

猜你喜欢

转载自blog.csdn.net/ShenQiXiaYang/article/details/80519095
今日推荐