public class DisplayHelper {
/**
* 屏幕密度,系统源码注释不推荐使用
*/
public static final float DENSITY = Resources.getSystem()
.getDisplayMetrics().density;
/**
* 获取 DisplayMetrics
*
* @return
*/
public static DisplayMetrics getDisplayMetrics(Context context) {
return context.getResources().getDisplayMetrics();
}
/**
* 把以 dp 为单位的值,转化为以 px 为单位的值
*
* @param dpValue 以 dp 为单位的值
* @return px value
*/
public static int dpToPx(int dpValue) {
return (int) (dpValue * DENSITY + 0.5f);
}
/**
* 把以 px 为单位的值,转化为以 dp 为单位的值
*
* @param pxValue 以 px 为单位的值
* @return dp值
*/
public static int pxToDp(float pxValue) {
return (int) (pxValue / DENSITY + 0.5f);
}
/**
* 单位转换: sp -> px
*
* @param sp
* @return
*/
public static int sp2px(Context context, int sp) {
return (int) (getFontDensity(context) * sp + 0.5);
}
/**
* 单位转换:px -> sp
*
* @param px
* @return
*/
public static int px2sp(Context context, int px) {
return (int) (px / getFontDensity(context) + 0.5);
}
public static float getDensity(Context context) {
return context.getResources().getDisplayMetrics().density;
}
public static float getFontDensity(Context context) {
return context.getResources().getDisplayMetrics().scaledDensity;
}
/**
* 获取屏幕宽度
*
* @return
*/
public static int getScreenWidth(Context context) {
return getDisplayMetrics(context).widthPixels;
}
/**
* 获取屏幕高度
*
* @return
*/
public static int getScreenHeight(Context context) {
int screenHeight = getDisplayMetrics(context).heightPixels;
if(QMUIDeviceHelper.isXiaomi() && xiaomiNavigationGestureEnabled(context)){
screenHeight += getResourceNavHeight(context);
}
return screenHeight;
}
/**
* 获取状态栏高度
*
* @param context
* @return
*/
public static int getStatusBarHeight(Context context) {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
if(x > 0){
return context.getResources().getDimensionPixelSize(x);
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
private static int getResourceNavHeight(Context context){
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
}
return -1;
}
/**
* 判断设备是否存在NavigationBar
*
* @return true 存在, false 不存在
*/
public static boolean deviceHasNavigationBar() {
boolean haveNav = false;
try {
//1.通过WindowManagerGlobal获取windowManagerService
// 反射方法:IWindowManager windowManagerService = WindowManagerGlobal.getWindowManagerService();
Class<?> windowManagerGlobalClass = Class.forName("android.view.WindowManagerGlobal");
Method getWmServiceMethod = windowManagerGlobalClass.getDeclaredMethod("getWindowManagerService");
getWmServiceMethod.setAccessible(true);
//getWindowManagerService是静态方法,所以invoke null
Object iWindowManager = getWmServiceMethod.invoke(null);
//2.获取windowMangerService的hasNavigationBar方法返回值
// 反射方法:haveNav = windowManagerService.hasNavigationBar();
Class<?> iWindowManagerClass = iWindowManager.getClass();
Method hasNavBarMethod = iWindowManagerClass.getDeclaredMethod("hasNavigationBar");
hasNavBarMethod.setAccessible(true);
haveNav = (Boolean) hasNavBarMethod.invoke(iWindowManager);
} catch (Exception e) {
e.printStackTrace();
}
return haveNav;
}
}
Android设备显示相关帮助类
最新推荐文章于 2023-03-16 14:22:00 发布