package com.moral.andbrickslib.utils;
|
|
import android.content.Context;
|
import android.content.res.Resources;
|
import android.util.DisplayMetrics;
|
import android.util.TypedValue;
|
|
/**
|
* 系统屏幕的一些操作
|
*/
|
public final class DensityUtils {
|
|
/**
|
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
|
*/
|
public static int dip2px(Context context, float dpValue) {
|
Resources r = context.getResources();
|
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
|
dpValue, r.getDisplayMetrics());
|
return (int) px;
|
}
|
|
/**
|
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
|
*/
|
public static int px2dip(Context context, float pxValue) {
|
final float scale = context.getResources().getDisplayMetrics().density;
|
return (int) (pxValue / scale + 0.5f);
|
}
|
|
/**
|
* 根据手机的分辨率从 px(像素) 的单位 转成为 sp
|
*/
|
public static int px2sp(Context context, float pxValue) {
|
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
|
return (int) (pxValue / fontScale + 0.5f);
|
}
|
|
/**
|
* 根据手机的分辨率从 sp 的单位 转成为 px
|
*/
|
public static int sp2px(Context context, float spValue) {
|
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
|
return (int) (spValue * fontScale + 0.5f);
|
}
|
}
|