/**
* 获取手机底部的圆弧高度
*/
public static int getArcHeight(Activity activity) {
int arcHeight =0;
//获取屏幕高度(包含状态栏+底部圆弧高度)
int rootHeight =getScreenRealHeight(activity);
//获取屏幕高度(不包含状态栏+底部圆弧高度)
int screenHeight =getScreenHeight(activity);
//手机底部圆弧高度
arcHeight = rootHeight - screenHeight - getStatusBarSize(activity);
return arcHeight <0 ?0 : arcHeight;
}
private static final int PORTRAIT =0;
private static final int LANDSCAPE =1;
@NonNull
private volatile static Point[]mRealSizes =new Point[2];
/** 获取屏幕高度(包含状态栏+底部圆弧高度) */
public static int getScreenRealHeight(@Nullable Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return getScreenHeight(context);
}
int orientation = context.getResources().getConfiguration().orientation;
orientation = orientation == Configuration.ORIENTATION_PORTRAIT ? PORTRAIT : LANDSCAPE;
if (mRealSizes[orientation] == null) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (windowManager == null) {
return getScreenHeight(context);
}
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
mRealSizes[orientation] = point;
}
return mRealSizes[orientation].y;
}
/** 获取屏幕高度(不包含状态栏+底部圆弧高度) */
public static int getScreenHeight(@Nullable Context context) {
if (context != null) {
return context.getResources().getDisplayMetrics().heightPixels;
}
return 0;
}
/** 获取状态栏高度 */
public static int getStatusBarSize( @Nullable Context context ) {
Class<?> c;
Object obj;
Field field;
int x = 0, statusbarSize = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusbarSize = context.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusbarSize;
}
网友评论