目前Android各大廠商都有Iphone X風格的劉海屏了,但是,在國內的各廠商的劉海屏適配也是個蛋疼的事,各自有各自的規則,大部分還沒有升級到Android P,使開發者沒辦法統一使用Android API進行適配。
這里寫了幾個大廠的劉海屏的適配,其實也就是在各廠商的開發者平台查查都是可以找到的,現在就簡單做個記錄吧。
public final class NotchScreenUtil { /** * 華為start */ // 判斷是否是華為劉海屏 public static boolean hasNotchInScreenAtHuawei(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); Log.d("NotchScreenUtil", "this Huawei device has notch in screen?"+ret); } catch (ClassNotFoundException e) { Log.e("NotchScreenUtil", "hasNotchInScreen ClassNotFoundException", e); } catch (NoSuchMethodException e) { Log.e("NotchScreenUtil", "hasNotchInScreen NoSuchMethodException", e); } catch (Exception e) { Log.e("NotchScreenUtil", "hasNotchInScreen Exception", e); } return ret; } /** * 獲取華為劉海的高 * @param context * @return */ public static int getNotchSizeAtHuawei(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("NotchScreenUtil", "getNotchSize ClassNotFoundException"); } catch (NoSuchMethodException e) { Log.e("NotchScreenUtil", "getNotchSize NoSuchMethodException"); } catch (Exception e) { Log.e("NotchScreenUtil", "getNotchSize Exception"); } return ret[1]; } /** * 華為end */ /** * Oppo start */ public static boolean hasNotchInScreenAtOppo(Context context) { boolean hasNotch = context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism"); Log.d("NotchScreenUtil", "this OPPO device has notch in screen?"+hasNotch); return hasNotch; } public static int getNotchSizeAtOppo() { return 80; } /** * Oppo end */ /** * vivo start */ public static final int NOTCH_IN_SCREEN_VOIO = 0x00000020;// 是否有凹槽 public static final int ROUNDED_IN_SCREEN_VOIO = 0x00000008;// 是否有圓角 public static boolean hasNotchInScreenAtVivo(Context context) { boolean ret = false; try { ClassLoader cl = context.getClassLoader(); Class<?> FtFeature = cl.loadClass("com.util.FtFeature"); Method get = FtFeature.getMethod("isFeatureSupport", int.class); ret = (Boolean) get.invoke(FtFeature, NOTCH_IN_SCREEN_VOIO); Log.d("NotchScreenUtil", "this VIVO device has notch in screen?" + ret); } catch (ClassNotFoundException e) { Log.e("NotchScreenUtil", "hasNotchInScreen ClassNotFoundException", e); } catch (NoSuchMethodException e) { Log.e("NotchScreenUtil", "hasNotchInScreen NoSuchMethodException", e); } catch (Exception e) { Log.e("NotchScreenUtil", "hasNotchInScreen Exception", e); } return ret; } public static int getNotchSizeAtVivo(Context context){ return dp2px(context, 32); } /** * vivo end */ /** * dp轉px * @param context * @param dpValue * @return */ private static int dp2px(Context context, int dpValue) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue,context.getResources().getDisplayMetrics()); } /** * 獲取手機廠商 * * @return 手機廠商 */ public final static int DEVICE_BRAND_OPPO = 0x0001; public final static int DEVICE_BRAND_HUAWEI = 0x0002; public final static int DEVICE_BRAND_VIVO = 0x0003; @SuppressLint("DefaultLocale") public static int getDeviceBrand() { String brand = android.os.Build.BRAND.trim().toUpperCase(); if (brand.contains("HUAWEI")) { Log.d("device brand", "HUAWEI"); return DEVICE_BRAND_HUAWEI; }else if (brand.contains("OPPO")) { Log.d("device brand", "OPPO"); return DEVICE_BRAND_OPPO; }else if (brand.contains("VIVO")) { Log.d("device brand", "VIVO"); return DEVICE_BRAND_VIVO; } return 0; } }
以上,記錄了華為、Oppo、Vivo三家廠商的屏幕適配,也經過測試,用起來沒什么問題。
使用的時候,可以使用getDeviceBrand()根據各廠商判斷用戶手機是否是劉海屏,如果是的話,則通過各廠商的方法獲取劉海的高度。獲取到高度了,具體的適配,就可以根據各自的需求功能具體實現。
