|
|
|
|
@ -9,23 +9,32 @@ import android.content.Context;
|
|
|
|
|
* desc : 屏幕相关
|
|
|
|
|
* </pre>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class ScreenUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
* @param dpValue 要转换的 dp 值
|
|
|
|
|
* @return 转换后的像素值
|
|
|
|
|
*/
|
|
|
|
|
public static int dip2px(Context context, float dpValue) {
|
|
|
|
|
final float scale = context.getResources().getDisplayMetrics().density;
|
|
|
|
|
return (int) (dpValue * scale + 0.5f);
|
|
|
|
|
// 获取设备的屏幕密度
|
|
|
|
|
final float scale = context.getResources().getDisplayMetrics().density;
|
|
|
|
|
// 将 dp 值乘以屏幕密度并四舍五入,转换为像素值
|
|
|
|
|
return (int) (dpValue * scale + 0.5f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
* @param pxValue 要转换的像素值
|
|
|
|
|
* @return 转换后的 dp 值
|
|
|
|
|
*/
|
|
|
|
|
public static int px2dip(Context context, float pxValue) {
|
|
|
|
|
final float scale = context.getResources().getDisplayMetrics().density;
|
|
|
|
|
return (int) (pxValue / scale + 0.5f);
|
|
|
|
|
// 获取设备的屏幕密度
|
|
|
|
|
final float scale = context.getResources().getDisplayMetrics().density;
|
|
|
|
|
// 将像素值除以屏幕密度并四舍五入,转换为 dp 值
|
|
|
|
|
return (int) (pxValue / scale + 0.5f);
|
|
|
|
|
}
|
|
|
|
|
}
|