先给工具类:
public class NotchUtil {
/**
* 小米刘海屏判断.
*/
public static int getInt(String key, Activity activity) {
int result = 0;
if (isXiaomi()) {
try {
ClassLoader classLoader = activity.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = int.class;
Method getInt = SystemProperties.getMethod("getInt", paramTypes);
//参数
Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new Integer(0);
result = (Integer) getInt.invoke(SystemProperties, params);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return result;
}
// 是否是小米手机
public static boolean isXiaomi() {
return "Xiaomi".equals(Build.MANUFACTURER);
}
/**
* 华为刘海屏判断
*/
public static boolean hasNotchAtHuawei(Context context) {
boolean ret = false;
try {
ClassLoader cl = context.getClassLoader();
Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
ret = (boolean) get.invoke(HwNotchSizeUtil);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
} finally {
return ret;
}
}
/**
* VIVO刘海屏判断
*/
public static final int VIVO_NOTCH = 0x00000020;//是否有刘海
public static final int VIVO_FILLET = 0x00000008;//是否有圆角
public static boolean hasNotchAtVivo(Context context) {
boolean ret = false;
try {
ClassLoader classLoader = context.getClassLoader();
Class FtFeature = classLoader.loadClass("android.util.FtFeature");
Method method = FtFeature.getMethod("isFeatureSupport", int.class);
ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
} finally {
return ret;
}
}
/**
* OPPO刘海屏判断
*/
public static boolean hasNotchAtOPPO(Context context) {
return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}
public static int getFullActivityHeight(@Nullable Context context) {
if (!isAllScreenDevice()) {
return getScreenHeight(context);
}
return getScreenRealHeight(context);
}
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 != null
? context.getResources().getConfiguration().orientation
: App.getApp().getResources().getConfiguration().orientation;
orientation = orientation == Configuration.ORIENTATION_PORTRAIT ? PORTRAIT : LANDSCAPE;
if (mRealSizes[orientation] == null) {
WindowManager windowManager = context != null
? (WindowManager) context.getSystemService(Context.WINDOW_SERVICE)
: (WindowManager) App.getApp().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;
}
private volatile static boolean mHasCheckAllScreen;
private volatile static boolean mIsAllScreenDevice;
public static boolean isAllScreenDevice() {
if (mHasCheckAllScreen) {
return mIsAllScreenDevice;
}
mHasCheckAllScreen = true;
mIsAllScreenDevice = false;
// 低于 API 21的,都不会是全面屏。。。
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
WindowManager windowManager = (WindowManager) App.getApp().getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
float width, height;
if (point.x < point.y) {
width = point.x;
height = point.y;
} else {
width = point.y;
height = point.x;
}
if (height / width >= 1.97f) {
mIsAllScreenDevice = true;
}
}
return mIsAllScreenDevice;
}
/**
* 是否有刘海屏
*
* @return
*/
public static boolean hasNotchInScreen(Activity activity) {
// // android P 以上有标准 API 来判断是否有刘海屏
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
// if (displayCutout != null) {
// // 说明有刘海屏
// return true;
// }
// } else {
// // 通过其他方式判断是否有刘海屏 目前官方提供有开发文档的就 小米,vivo,华为(荣耀),oppo
// String manufacturer = Build.MANUFACTURER;
//
// if (TextUtils.isEmpty(manufacturer)) {
// return false;
// } else if (manufacturer.equalsIgnoreCase("HUAWEI")) {
// return hasNotchHw(activity);
// } else if (manufacturer.equalsIgnoreCase("xiaomi")) {
// return hasNotchXiaoMi(activity);
// } else if (manufacturer.equalsIgnoreCase("oppo")) {
// return hasNotchOPPO(activity);
// } else if (manufacturer.equalsIgnoreCase("vivo")) {
// return hasNotchVIVO(activity);
// } else {
// return false;
// }
// }
// return false;
// 通过其他方式判断是否有刘海屏 目前官方提供有开发文档的就 小米,vivo,华为(荣耀),oppo
String manufacturer = Build.MANUFACTURER;
if (TextUtils.isEmpty(manufacturer)) {
return false;
} else if (manufacturer.equalsIgnoreCase("HUAWEI")) {
return hasNotchHw(activity);
} else if (manufacturer.equalsIgnoreCase("xiaomi")) {
return hasNotchXiaoMi(activity);
} else if (manufacturer.equalsIgnoreCase("oppo")) {
return hasNotchOPPO(activity);
} else if (manufacturer.equalsIgnoreCase("vivo")) {
return hasNotchVIVO(activity);
} else {
return false;
}
}
/**
* 判断vivo是否有刘海屏
* https://swsdl.vivo.com.cn/appstore/developer/uploadfile/20180328/20180328152252602.pdf
*
* @param activity
* @return
*/
private static boolean hasNotchVIVO(Activity activity) {
try {
Class> c = Class.forName("android.util.FtFeature");
Method get = c.getMethod("isFeatureSupport", int.class);
return (boolean) (get.invoke(c, 0x20));
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 判断oppo是否有刘海屏
* https://open.oppomobile.com/wiki/doc#id=10159
*
* @param activity
* @return
*/
private static boolean hasNotchOPPO(Activity activity) {
return activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}
/**
* 判断xiaomi是否有刘海屏
* https://dev.mi.com/console/doc/detail?pId=1293
*
* @param activity
* @return
*/
private static boolean hasNotchXiaoMi(Activity activity) {
try {
Class> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("getInt", String.class, int.class);
return (int) (get.invoke(c, "ro.miui.notch", 1)) == 1;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 判断华为是否有刘海屏
* https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114
*
* @param activity
* @return
*/
private static boolean hasNotchHw(Activity activity) {
try {
ClassLoader cl = activity.getClassLoader();
Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
return (boolean) get.invoke(HwNotchSizeUtil);
} catch (Exception e) {
return false;
}
}
}
使用:
2. activity 初始化中
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
View decorView = window.getDecorView();
decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
return defaultInsets.replaceSystemWindowInsets(
defaultInsets.getSystemWindowInsetLeft(),
0,
defaultInsets.getSystemWindowInsetRight(),
defaultInsets.getSystemWindowInsetBottom());
}
});
ViewCompat.requestApplyInsets(decorView);
//将状态栏设成透明,如不想透明可设置其他颜色
window.setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
}
//5.0往上采用全屏显示 if (Build.VERSION.SDK_INT > 19) {
if (NotchUtil.hasNotchInScreen(ActivityStack.getInstance().topActivity())) {
mHeight = NotchUtil.getFullActivityHeight(App.getApp()); }else { mHeight = context.getResources().getDisplayMetrics().heightPixels;
} } else { //4.4及以下采用去除标题栏 mHeight = context.getResources().getDisplayMetrics().heightPixels - BarUtils.getStatusBarHeight();
}