17

Android 在 API 28 上添加了缺口支持,但是如何在运行 API 27 的设备(荣耀 10、华为 P20 等)上处理它?

我试图使用DisplayCutoutCompat但我无法创建它的实例,因为文档并没有真正指出如何创建一个。

如何创建构造函数参数值:Rect safeInsetsList<Rect> boundingRects

我还查看了构造函数的源代码,这让我有点困惑:

public DisplayCutoutCompat(Rect safeInsets, List<Rect> boundingRects) {
        this(SDK_INT >= 28 ? new DisplayCutout(safeInsets, boundingRects) : null);
    }

这将始终在运行API < 28的设备上返回 null 。先感谢您。

4

4 回答 4

15

Google在Android P中提供了notch相关的API。notch和API版本低于P的设备实现了自己的notch API。您可以从设备指定的文档中查阅API。

我也没有在官方文档中看到 DisplayCutoutCompat 实例的创建,但是您可以按如下方式创建 DisplayCutout:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            DisplayCutout displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
}
于 2018-08-28T18:02:15.147 回答
5

所以你想在低于 28 的 android API 中处理 notch(display cutout)。这太可怕了,因为不同的制造商有不同的实现。尽管如此,都使用Java 反射来获取缺口信息。这里应该使用工厂设计模式。

interface ICutout {
    public boolean hasCutout();

    public Rect[] getCutout();
}
  1. 华为显示屏抠图

    private static class HuaweiCutout implements 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];
    }}
    
  2. Oppo显示屏开孔

    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];
    }}
    
  3. Vivo显示屏镂空

    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];
    }}
    
  4. Android Pie的 Android Oreo的小米显示屏抠图

    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();
于 2019-06-15T09:18:32.020 回答
2

我有类似的问题,不得不使用反射来访问我需要的东西。我的问题是我根据屏幕大小进行了一些计算,虽然没有访问缺口空间,但计算错误并且代码无法正常工作。

    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");
        }
于 2018-12-04T22:20:04.990 回答
0

要处理低于 28 个 WindowInsetsCompat 的 API,可以使用。科特林示例:

WindowInsetsCompat.toWindowInsetsCompat(window.decorView.rootWindowInsets).displayCutout

于 2020-08-18T19:54:59.743 回答