1. 程式人生 > >Android 手機適配劉海(華為、vivo)

Android 手機適配劉海(華為、vivo)

直接開始。

先看下鴻陽公眾號的上面文章:可以關注他,這裡只是方便自己記錄一下。

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