Android中的单位转换一篇就够了

Android中的单位

android 中的单位主要是以下几种:

  • px
  • dip
  • sp:

1、px

像素,也就是我们常说的手机分别率单位,在不同设备上和不同显示器上的显示效果一样,大小值不变。
  1. px 转dp
public float px2dp(float pxValue, Context context){
        float density = context.getResources().getDisplayMetrics().density;
        return  pxValue / density + 0.5F;
}
  1. px 转 sp
public float px2sp(float pxValue, Context context){
        float scale = context.getResources().getDisplayMetrics().scaledDensity;
        return  pxValue / scale + 0.5F;
}

2、dp(dip)

设备独立像素 (device independent pixels),不同的设备会有不同的显示效果,跟屏幕密度有关,
	一般我们在做适配时这个单位用的比较多。
  1. dp 转 px
public float dp2px(float dpValue, Context context){
        float density= context.getResources().getDisplayMetrics().density;
        return  dpValue* density + 0.5F;
}

3、sp

放大像素(scaled pixels)主要用于字体显示。
  1. sp 转 px
public float sp2px(float spValue, Context context){
        float scale= context.getResources().getDisplayMetrics().scaledDensity;
        return  spValue* scale + 0.5F;
}

那么,是怎么知道这个单位转换方式呢?

接下来机遇是班门弄斧的时间了,copy 下源码出来,请移驾下看 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

系统提供给我们转换方法

TypedValue.applyDimension(int unit, float value,DisplayMetrics metrics);
	/**
     * Converts an unpacked complex data value holding a dimension to its final floating 
     * point value. The two parameters <var>unit</var> and <var>value</var>
     * are as in {@link #TYPE_DIMENSION}.
     *  
     * @param unit The unit to convert from.
     * @param value The value to apply the unit to.
     * @param metrics Current display metrics to use in the conversion -- 
     *                supplies display density and scaling information.
     * 
     * @return The complex floating point value multiplied by the appropriate 
     * metrics depending on its unit. 
     */
    public static float applyDimension(int unit, float value,DisplayMetrics metrics){
        switch (unit) {
        case COMPLEX_UNIT_PX:
            return value;
        case COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f/72);
        case COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f/25.4f);
        }
        return 0;
    }

这个我就不多说了,提供一下用这个方法 将 dp 转换为px的方法

public float dp2px(float dpValue, Context context) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpValue, context.getResources().getDisplayMetrics());
}

为什么转换的时候我们 +0.5F

为什么 ? 源码说明,没有说要 +0.5F 这个操作,为什么呢?

我觉得是为了更加精确的转换数值的,我也搜了下,在此做下记录,方便以后在此查看
  • 我们在什么时候要用到 转换单位 比如说:
	- view.setWidth(int width) ;

这个 width 参数时 int 类型,而我们转换单位后的值 是个float 类型,熟悉java的应该都知道
float 转 int 会丢失精度

  • 比如说上面提到的 px2dp: 这个方法
public float px2dp(float pxValue, Context context){
        float density = context.getResources().getDisplayMetrics().density;
        return  pxValue / density + 0.5F;
}

如果 pxValue / density = 4.1的话,那么4.1+0.5=4.64.6在类型转换时:(int)4.6 = 4;
而如果 4.5 以上的话,4.5+0.5F = 5.05.0在类型转换时:(int)5.0= 5;
这样就达到了一个四舍五入 的效果,使转换结果更加精准。

看完后是不是明白了,感谢各位老铁查阅,如有误,请大佬留言指正,在此感谢!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值