goback add

Android开发相关:(07)dp与px转换

2770 点击·0 回帖
灯火互联
楼主


px:像素。
in:英寸。
mm:毫米。
pt:磅。
dp:与密度无关的像素,基于160dpi(每英寸的像素数)屏幕(尺寸适应屏幕密度)。
sp:与比例无关的像素(这种尺寸支持用户调整大小,适合在字体中使用)。
[java]/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}
  
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
}


喜欢0 评分0