Android 手机适配刘海(华为、vivo)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34900897/article/details/81671591

直接开始。

先看下鸿阳公众号的上面文章:可以关注他,这里只是方便自己记录一下。

Android 刘海屏适配方案

一、vivo的刘海适配直接官方文档

(长屏幕)

google 适配全面屏要求,必须在AndroidManifest.xml声明一下meta-data,应用下可以全屏显示:

<meta-data android:name="android.max_aspect" android:value="ratio_float"/>

或者

android:maxAspectRatio="ratio_float"(API LEVEL 26)

ratio_float为手机屏幕的高和宽的比例,如手机屏幕为2280×1080 19:9的分辨率,则ratio_float = 19/9≈2.11,设置比该值大即可全屏显示。

在Android 7.0以上Google默认支持了分屏模式,设置android:resizeableActivity="true" 同样可以让应用全屏显示

二、华为  官方文档

华为做的比较详细了,

先判断是否有刘海,直接copy官方代码

public static boolean hasNotchInScreen(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) {

        Log.e("test", "hasNotchInScreen ClassNotFoundException");

    } catch (NoSuchMethodException e) {

        Log.e("test", "hasNotchInScreen NoSuchMethodException");

    } catch (Exception e) {

        Log.e("test", "hasNotchInScreen Exception");

    } finally {

        return ret;

    }

}

二、获取刘海尺寸

public static int[] getNotchSize(Context context) {

    int[] ret = new int[]{0, 0};

    try {

        ClassLoader cl = context.getClassLoader();

        Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");

        Method get = HwNotchSizeUtil.getMethod("getNotchSize");

        ret = (int[]) get.invoke(HwNotchSizeUtil);

    } catch (ClassNotFoundException e) {

        Log.e("test", "getNotchSize ClassNotFoundException");

    } catch (NoSuchMethodException e) {

        Log.e("test", "getNotchSize NoSuchMethodException");

    } catch (Exception e) {

        Log.e("test", "getNotchSize Exception");

    } finally {

        return ret;

    }

}

其实得到这些可以自行处理,比如,,判断有的话处理一下没有的话就把尺寸获取到,做出一些相应的处理。

小米 

判断是否有

系统增加了 property ro.miui.notch,值为1时则是 Notch 屏手机。

SystemProperties.getInt("ro.miui.notch", 0) == 1;

获取刘海高度

int resourceId = context.getResources().getIdentifier("notch_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}

官方文档给出了很多的方法,,可以查看文档

再一次给出连接

华为 https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114

oppo https://open.oppomobile.com/service/message/detail?id=61876

vivo https://dev.vivo.com.cn/documentCenter/doc/135

google https://developer.android.com/preview/features

小米  https://dev.mi.com/console/doc/detail?pId=1293

猜你喜欢

转载自blog.csdn.net/qq_34900897/article/details/81671591