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); } /** * 将px值转换为sp值,保证文字大小不变 * * @param pxValue * @param fontScale(DisplayMetrics类中属性scaledDensity) * @return */ public static int px2sp(float pxValue, float fontScale) { return (int) (pxValue / fontScale + 0.5f); } /** * 将sp值转换为px值,保证文字大小不变 * * @param spValue * @param fontScale(DisplayMetrics类中属性scaledDensity) * @return */ public static int sp2px(float spValue, float fontScale) { return (int) (spValue * fontScale + 0.5f); } /** * 获得屏幕密度 * @param context * @return */ public static int getScreenDensityDpi(Activity context) { DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); int density = metric.densityDpi; CLog.d("DensityUtil density=", density+""); return density; } /** * 获得屏幕高度 * @param context * @return */ public static int getScreenHeight(Activity context) { DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); int screenHeight = metric.heightPixels; CLog.d("DensityUtil screenHight=", screenHeight+""); return screenHeight; } /** * 获得屏幕宽度 * @param context * @return */ public static int getScreenWidth(Activity context) { DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); int screenWidth = metric.widthPixels; CLog.d("DensityUtil screenWidth=",screenWidth+""); return screenWidth; } /** * 返回状态栏/通知栏的高度 * * @param activity * @return */ public static int getStatusHeight(Activity activity) { Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; return statusBarHeight; } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/11072.html