package com.xt.deeptest5g.Utils;
import android.content.res.Resources;
/**
* author : ZJS
* e-mail : 15574564770@163.com
* date : 20-7-30下午9:02
* desc : dp与px相互转换
* version : 1.0
*/
public class DensityUtil {
public float density;
public DensityUtil() {
density = Resources.getSystem().getDisplayMetrics().density;
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*
* @param dpValue 虚拟像素
* @return 像素
*/
public static int dp2px(float dpValue) {
return (int) (0.5f + dpValue * Resources.getSystem().getDisplayMetrics().density);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*
* @param pxValue 像素
* @return 虚拟像素
*/
public static float px2dp(int pxValue) {
return (pxValue / Resources.getSystem().getDisplayMetrics().density);
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*
* @param dpValue 虚拟像素
* @return 像素
*/
public int dip2px(float dpValue) {
return (int) (0.5f + dpValue * density);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*
* @param pxValue 像素
* @return 虚拟像素
*/
public float px2dip(int pxValue) {
return (pxValue / density);
}
}
dp与px相互转换
最新推荐文章于 2022-01-14 13:56:37 发布