dp和px互转

来源:互联网 发布:中国人爱吃 知乎 编辑:程序博客网 时间:2024/04/29 11:59
     */
public static int dip2qx(Context context, floatdpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;  //获取屏幕的密度
        return (int) (dpValue * scale + 0.5f); //+0.5f四舍五入   3.7  3   3.7+0.5 = 4.2   4
    }
 
    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
public static int px2dip(Context context, floatpxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
0 0