Android中dp与px之间的转换

来源:互联网 发布:黑莓q10软件 编辑:程序博客网 时间:2024/06/08 09:23

1. px :屏幕像素点

2. dp :一个基于density的抽象单位,如果这个屏幕,像素密度是160dpi,此时1dp=1pxdip

3、转换方式

public class DensityUtil {     // 根据手机的分辨率从 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
原创粉丝点击