对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理:
对Activity生效,意味着可以针对单个页面进行刘海屏适配,设置了该属性的Activity系统将不会做特殊处理:
方案二
对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的特殊下移或者是横屏场景的右移特殊处理
设置应用窗口在华为刘海屏手机使用刘海区我的NotchScreenTool中使用的就是方案二,如果需要针对Activity,建议自行修改。
/刘海屏全屏显示FLAG/
public static final int FLAG_NOTCH_SUPPORT=0x00010000
;
/**
设置应用窗口在华为刘海屏手机使用刘海区@param window 应用页面window对象
*/
public static void setFullScreenWindowLayoutInDisplayCutout(Window window) {
if (window == null) {
return;
}
WindowManager.LayoutParams layoutParams = window.getAttributes();
try {
Class layoutParamsExCls = Class.forName(“com.huawei.android.view.LayoutParamsEx”);
Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);
Object layoutParamsExObj=con.newInstance(layoutParams);
Method method=layoutParamsExCls.getMethod(“addHwFlags”, int.class);
method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException
| InvocationTargetException e) {
Log.e(“test”, “hw add notch screen flag api error”);
} catch (Exception e) {
Log.e(“test”, “other Exception”);
}
} 清除添加的华为刘海屏Flag,恢复应用不使用刘海区显示
/**
设置应用窗口在华为刘海屏手机使用刘海区@param window 应用页面window对象
*/
public static void setNotFullScreenWindowLayoutInDisplayCutout (Window window) {
if (window == null) {
return;
}
WindowManager.LayoutParams layoutParams = window.getAttributes();
try {
Class layoutParamsExCls = Class.forName(“com.huawei.android.view.LayoutParamsEx”);
Constructor con=layoutParamsExCls.getConstructor(LayoutParams.class);
Object layoutParamsExObj=con.newInstance(layoutParams);
Method method=layoutParamsExCls.getMethod(“clearHwFlags”, int.class);
method.invoke(layoutParamsExObj, FLAG_NOTCH_SUPPORT);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException
| InvocationTargetException e) {
Log.e(“test”, “hw clear notch screen flag api error”);
} catch (Exception e) {
Log.e(“test”, “other Exception”);
}
}
适配小米Android O设备
判断是否是刘海屏private static boolean isNotch() {
try {
Method getInt = Class.forName(“android.os.SystemProperties”).getMethod(“getInt”, String.class, int.class);
int notch = (int) getInt.invoke(null, “ro.miui.notch”, 0);
return notch == 1;
} catch (Throwable ignore) {
}
return false;
}
设置显示到刘海区域
@Override
public void setDisplayInNotch(Activity activity) {
int flag = 0x00000100 | 0x00000200 | 0x00000400;
try {
Method method = Window.class.getMethod(“addExtraFlags”,
int.class);
method.invoke(activity.getWindow(), flag);
} catch (Exception ignore) {
}
}
获取刘海宽高
public static int getNotchHeight(Context context) {
int resourceId = context.getResources().getIdentifier(“notch_height”, “dimen”, “android”);
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
}
return 0;
}
public static int getNotchWidth(Context context) {
int resourceId = context.getResources().getIdentifier(“notch_width”, “dimen”, “android”);
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
}
return 0;
}
适配oppoAndroid O设备
判断是否是刘海屏@Override
public boolean hasNotch(Activity activity) {
boolean ret = false;
try {
ret = activity.getPackageManager().hasSystemFeature(“com.oppo.feature.screen.heteromorphism”);
} catch (Throwable ignore) {
}
return ret;
}
获取刘海的左上角和右下角的坐标
/**
获取刘海的坐标属性形如:[ro.oppo.screen.heteromorphism]: [378,0:702,80]获取到的值为378,0:702,80(378,0)是刘海区域左上角的坐标(702,80)是刘海区域右下角的坐标
*/
private static String getScreenValue() {
String value = “”;
Class<?> cls;
try {
cls = Class.forName(“android.os.SystemProperties”);
Method get = cls.getMethod(“get”, String.class);
Object object = cls.newInstance();
value = (String) get.invoke(object, “ro.oppo.screen.heteromorphism”);
} catch (Throwable ignore) {
}
return value;
}
Oppo Android O机型不需要设置显示到刘海区域,只要设置了应用全屏就会默认显示。
因此Oppo机型必须适配。
适配总结
根据上述功能,我将其整理成了一个依赖库:NotchScreenTool
使用起来很简单:
// 支持显示到刘海区域
NotchScreenManager.getInstance().setDisplayInNotch(this);
// 获取刘海屏信息
NotchScreenManager.getInstance().getNotchInfo(this, new INotchScreen.NotchScreenCallback() {
@Override
public void onResult(INotchScreen.NotchScreenInfo notchScreenInfo) {
Log.i(TAG, "Is this screen notch? " + notchScreenInfo.hasNotch);
if (notchScreenInfo.hasNotch) {
for (Rect rect : notchScreenInfo.notchRects) {
Log.i(TAG, "notch screen Rect = " + rect.toShortString());
}
}
}
});
o notchScreenInfo) {
Log.i(TAG, "Is this screen notch? " + notchScreenInfo.hasNotch);
if (notchScreenInfo.hasNotch) {
for (Rect rect : notchScreenInfo.notchRects) {
Log.i(TAG, "notch screen Rect = " + rect.toShortString());
}
}
}
});