package com.app.hubert.guide.util;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.res.Resources;
|
import android.graphics.Point;
|
import android.os.Build;
|
import android.util.DisplayMetrics;
|
import android.view.Display;
|
import android.view.KeyCharacterMap;
|
import android.view.KeyEvent;
|
import android.view.ViewConfiguration;
|
|
/**
|
* Created by hubert
|
* <p>
|
* Created on 2017/7/27.
|
*/
|
public class ScreenUtils {
|
|
private ScreenUtils() {
|
throw new AssertionError();
|
}
|
|
/**
|
* dp单位转成px
|
*
|
* @param context context
|
* @param dp dp值
|
* @return px值
|
*/
|
public static int dp2px(Context context, int dp) {
|
return (int) (dp * context.getResources().getDisplayMetrics().density);
|
}
|
|
/**
|
* 获取屏幕宽度
|
*/
|
public static int getScreenWidth(Context context) {
|
DisplayMetrics dm = context.getResources().getDisplayMetrics();
|
return dm.widthPixels;
|
}
|
|
/**
|
* 获取屏幕高度
|
*/
|
public static int getScreenHeight(Context context) {
|
DisplayMetrics dm = context.getResources().getDisplayMetrics();
|
return dm.heightPixels;
|
}
|
|
/**
|
* 获取状态栏高度
|
*/
|
public static int getStatusBarHeight(Context context) {
|
// 一般是25dp
|
int height = dp2px(context, 20);
|
LogUtil.i("common statusBar height:" + height);
|
//获取status_bar_height资源的ID
|
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
|
if (resourceId > 0) {
|
height = context.getResources().getDimensionPixelSize(resourceId);
|
LogUtil.i("real statusBar height:" + height);
|
}
|
LogUtil.i("finally statusBar height:" + height);
|
return height;
|
}
|
|
/**
|
* 虚拟操作拦(home等)是否显示
|
*/
|
public static boolean isNavigationBarShow(Activity activity) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
Display display = activity.getWindowManager().getDefaultDisplay();
|
Point size = new Point();
|
Point realSize = new Point();
|
display.getSize(size);
|
display.getRealSize(realSize);
|
return realSize.y != size.y;
|
} else {
|
boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
|
boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
|
if (menu || back) {
|
return false;
|
} else {
|
return true;
|
}
|
}
|
}
|
|
/**
|
* 获取虚拟操作拦(home等)高度
|
*/
|
public static int getNavigationBarHeight(Activity activity) {
|
if (!isNavigationBarShow(activity))
|
return 0;
|
int height = 0;
|
Resources resources = activity.getResources();
|
//获取NavigationBar的高度
|
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
|
if (resourceId > 0)
|
height = resources.getDimensionPixelSize(resourceId);
|
LogUtil.i("NavigationBar的高度:" + height);
|
return height;
|
}
|
}
|