package com.netease.nim.uikit.common.util.sys; import android.content.Context; import android.content.res.Resources; import android.util.DisplayMetrics; import android.util.Log; import com.netease.nim.uikit.NimUIKit; import java.lang.reflect.Field; public class ScreenUtil { private static final String TAG = "Demo.ScreenUtil"; private static double RATIO = 0.85; public static int screenWidth; public static int screenHeight; public static int screenMin;// 宽高中,小的一边 public static int screenMax;// 宽高中,较大的值 public static float density; public static float scaleDensity; public static float xdpi; public static float ydpi; public static int densityDpi; public static int dialogWidth; public static int statusbarheight; public static int navbarheight; static { init(NimUIKit.getContext()); } public static int dip2px(float dipValue) { return (int) (dipValue * density + 0.5f); } public static int px2dip(float pxValue) { return (int) (pxValue / density + 0.5f); } public static int sp2px(float spValue) { return (int) (spValue * scaleDensity + 0.5f); } public static int getDialogWidth() { dialogWidth = (int) (screenMin * RATIO); return dialogWidth; } public static void init(Context context) { if (null == context) { return; } DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; screenMin = (screenWidth > screenHeight) ? screenHeight : screenWidth; density = dm.density; scaleDensity = dm.scaledDensity; xdpi = dm.xdpi; ydpi = dm.ydpi; densityDpi = dm.densityDpi; Log.d(TAG, "screenWidth=" + screenWidth + " screenHeight=" + screenHeight + " density=" + density); } public static int getDisplayWidth() { if (screenWidth == 0) { GetInfo(NimUIKit.getContext()); } return screenWidth; } public static int getDisplayHeight() { if (screenHeight == 0) { GetInfo(NimUIKit.getContext()); } return screenHeight; } public static void GetInfo(Context context) { if (null == context) { return; } DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; screenMin = (screenWidth > screenHeight) ? screenHeight : screenWidth; screenMax = (screenWidth < screenHeight) ? screenHeight : screenWidth; density = dm.density; scaleDensity = dm.scaledDensity; xdpi = dm.xdpi; ydpi = dm.ydpi; densityDpi = dm.densityDpi; statusbarheight = getStatusBarHeight(context); navbarheight = getNavBarHeight(context); Log.d(TAG, "screenWidth=" + screenWidth + " screenHeight=" + screenHeight + " density=" + density); } public static int getStatusBarHeight(Context context) { if (statusbarheight == 0) { try { Class<?> c = Class.forName("com.android.internal.R$dimen"); Object o = c.newInstance(); Field field = c.getField("status_bar_height"); int x = (Integer) field.get(o); statusbarheight = context.getResources().getDimensionPixelSize(x); } catch (Exception e) { e.printStackTrace(); } } if (statusbarheight == 0) { statusbarheight = ScreenUtil.dip2px(25); } return statusbarheight; } public static int getNavBarHeight(Context context) { Resources resources = context.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { return resources.getDimensionPixelSize(resourceId); } return 0; } }