Android在API 28上添加了notch支持,但是如何在运行API 27的设备(荣誉10,Huawei P20等)上处理它?
我试图使用DisplayCutoutCompat
,但是由于文档并没有真正指出如何创建一个实例,因此无法创建它的实例。
如何创建构造函数参数值:Rect safeInsets
,List<Rect> boundingRects
?
我还研究了构造函数的源代码,这让我有些困惑:
public DisplayCutoutCompat(Rect safeInsets, List<Rect> boundingRects) {
this(SDK_INT >= 28 ? new DisplayCutout(safeInsets, boundingRects) : null);
}
在运行 API <28 的设备上,这将始终返回null。 预先谢谢你。
答案 0 :(得分:11)
Google在Android P中提供了与缺口相关的API。缺口和API版本低于P的设备会实现自己的缺口API。您可以从设备指定的文档中查阅这些API。
我在官方文档中也没有看到DisplayCutoutCompat实例的创建,但是您可以按照以下步骤创建DisplayCutout:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
DisplayCutout displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
}
答案 1 :(得分:0)
我遇到了类似的问题,并且不得不使用反射来访问所需的内容。 我的问题是我根据屏幕尺寸进行了一些计算,虽然没有访问缺口空间,但计算错误并且代码无法正常工作。
public static final String CLASS_DISPLAY_CUTOUT = "android.view.DisplayCutout";
public static final String METHOD_GET_DISPLAY_CUTOUT = "getDisplayCutout";
public static final String FIELD_GET_SAFE_INSET_TOP = "getSafeInsetTop";
public static final String FIELD_GET_SAFE_INSET_LEFT = "getSafeInsetLeft";
public static final String FIELD_GET_SAFE_INSET_RIGHT = "getSafeInsetRight";
public static final String FIELD_GET_SAFE_INSET_BOTTOM = "getSafeInsetBottom";
try {
WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
if (windowInsets == null) {
return;
}
Method method = WindowInsets.class.getMethod(METHOD_GET_DISPLAY_CUTOUT);
Object displayCutout = method.invoke(windowInsets);
if (displayCutout == null) {
return;
}
Class clz = Class.forName(CLASS_DISPLAY_CUTOUT);
int top = (int) clz.getMethod(FIELD_GET_SAFE_INSET_TOP).invoke(displayCutout);
int left = (int) clz.getMethod(FIELD_GET_SAFE_INSET_LEFT).invoke(displayCutout);
int right = (int) clz.getMethod(FIELD_GET_SAFE_INSET_RIGHT).invoke(displayCutout);
int bottom = (int) clz.getMethod(FIELD_GET_SAFE_INSET_BOTTOM).invoke(displayCutout);
Rect rect = new Rect(left, top, right, bottom);
} catch (Exception e) {
Log.e(TAG, "Error when getting display cutout size");
}
答案 2 :(得分:0)
因此,您想在低于28的android API中处理notch(显示切口)。这很糟糕,因为不同的制造商具有不同的实现。尽管如此,所有人都使用Java reflection来获得刻痕信息。 Factory design pattern应该在这里使用。
interface ICutout {
public boolean hasCutout();
public Rect[] getCutout();
}
私有静态类HuaweiCutout实现ICutout {
private Context context;
public HuaweiCutout(@NonNull Context context) {
this.context = context;
}
@Override
public boolean hasCutout() {
try {
ClassLoader classLoader = context.getClassLoader();
Class class_HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method method_hasNotchInScreen = class_HwNotchSizeUtil.getMethod("hasNotchInScreen");
return (boolean) method_hasNotchInScreen.invoke(class_HwNotchSizeUtil);
} catch (Exception e) {
}
return false;
}
@Override
public Rect[] getCutout() {
try {
ClassLoader classLoader = context.getClassLoader();
Class class_HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method method_getNotchSize = class_HwNotchSizeUtil.getMethod("getNotchSize");
int[] size = (int[]) method_getNotchSize.invoke(class_HwNotchSizeUtil);
int notchWidth = size[0];
int notchHeight = size[1];
int screenWidth = DeviceUtil.getScreenWidth(context);
int x = (screenWidth - notchWidth) >> 1;
int y = 0;
Rect rect = new Rect(x, y, x + notchWidth, y + notchHeight);
return new Rect[] {rect};
} catch (Exception e) {
}
return new Rect[0];
}
}
Oppo display cutout
private static class OppoCutout implements ICutout {
private Context context;
public OppoCutout(@NonNull Context context) {
this.context = context;
}
@Override
public boolean hasCutout() {
String CutoutFeature = "com.oppo.feature.screen.heteromorphism";
return context.getPackageManager().hasSystemFeature(CutoutFeature);
}
@Override
public Rect[] getCutout() {
String value = getProperty("ro.oppo.screen.heteromorphism");
String[] texts = value.split("[,:]");
int[] values = new int[texts.length];
try {
for(int i = 0; i < texts.length; ++i)
values[i] = Integer.parseInt(texts[i]);
} catch(NumberFormatException e) {
values = null;
}
if(values != null && values.length == 4) {
Rect rect = new Rect();
rect.left = values[0];
rect.top = values[1];
rect.right = values[2];
rect.bottom = values[3];
return new Rect[] {rect};
}
return new Rect[0];
}
}
Vivo display cutout
private static class VivoCutout implements ICutout {
private Context context;
public VivoCutout(@NonNull Context context) {
this.context = context;
}
@Override
public boolean hasCutout() {
try {
ClassLoader clazz = context.getClassLoader();
Class ftFeature = clazz.loadClass("android.util.FtFeature");
Method[] methods = ftFeature.getDeclaredMethods();
for(Method method: methods) {
if (method.getName().equalsIgnoreCase("isFeatureSupport")) {
int NOTCH_IN_SCREEN = 0x00000020; // 表示是否有凹槽
int ROUNDED_IN_SCREEN = 0x00000008; // 表示是否有圆角
return (boolean) method.invoke(ftFeature, NOTCH_IN_SCREEN);
}
}
} catch (Exception e) {
}
return false;
}
@Override
public Rect[] getCutout() {
// throw new RuntimeException(); // not implemented yet.
return new Rect[0];
}
}
小米显示切口of Android Oreo,of Android Pie
private static class XiaomiCutout implements ICutout {
private Context context;
public XiaomiCutout(@NonNull Context context) {
this.context = context;
}
@Override
public boolean hasCutout() {
// `getprop ro.miui.notch` output 1 if it's a notch screen.
String text = getProperty("ro.miui.notch");
return text.equals("1");
}
@Override
public Rect[] getCutout() {
Resources res = context.getResources();
int widthResId = res.getIdentifier("notch_width", "dimen", "android");
int heightResId = res.getIdentifier("notch_height", "dimen", "android");
if(widthResId > 0 && heightResId > 0) {
int notchWidth = res.getDimensionPixelSize(widthResId);
int notchHeight = res.getDimensionPixelSize(heightResId);
// one notch in screen top
int screenWidth = DeviceUtil.getScreenSize(context).getWidth();
int left = (screenWidth - notchWidth) >> 1;
int right = left + notchWidth;
int top = 0;
int bottom = notchHeight;
Rect rect = new Rect(left, top, right, bottom);
return new Rect[] {rect};
}
return new Rect[0];
}
}
如果某些制造商不提供getNotchHeight()方法,则可以只使用状态栏的高度。 Android已保证缺口高度最多为状态栏的高度。
public static int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
Resources res = context.getResources();
int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = res.getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
对于Android Pie及更高版本(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
),您可以使用系统的API获取缺口信息。请注意,必须附加窗口Activity#onAttachedToWindow
,否则您将获得空的DisplayCutout。
DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
答案 3 :(得分:0)
要处理更低的API,则可以使用28 WindowInsetsCompat。 Kotlin示例:
WindowInsetsCompat.toWindowInsetsCompat(window.decorView.rootWindowInsets).displayCutout