引言
首先先翻译一下 notch
即 凹痕 的意思,那我们就认为是 “刘海” 就可以了。
目前,市场手机的潮流就是推出 全面屏、齐刘海 的手机,比如现在的华为、oppo 、vivo、小米等等手机厂商陆续推出了几款 齐刘海的手机。
但是,现在有个问题,就是 google 的 Android 系统中在官方发布齐刘海的屏幕方案之前,这些手机厂商已经进行了刘海屏的开发了,所以我们从android官网是找不到解决方案了。
所以,适配齐刘海的方案肯定还得从各大手机厂商中去找答案。
下面依次把 各大手机厂商的「异形屏适配指南」贴出来,方便大家参考
小米刘海屏 Android O 适配
小米刘海屏 Android P 适配
解决方案
解决方案总结一下,其实大体是这么几个步骤:
-
判断是否是对应机型的刘海屏
-
判断是否竖屏显示
-
是否需要展示状态栏
-
相应的品牌机型处理
提供一个判断刘海屏的工具类
public class NotchUtil {
/**
* OPPO
*
* @param context Context
* @return hasNotch
*/
public static boolean hasNotchInOppo(Context context) {
return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}
/**
* VIVO
* <p>
* android.util.FtFeature
* public static boolean isFeatureSupport(int mask);
* <p>
* 参数:
* 0x00000020表示是否有凹槽;
* 0x00000008表示是否有圆角。
*
* @param context Context
* @return hasNotch
*/
private static boolean hasNotchInVivo(Context context) {
boolean hasNotch = false;
try {
ClassLoader cl = context.getClassLoader();
Class ftFeature = cl.loadClass("android.util.FtFeature");
Method[] methods = ftFeature.getDeclaredMethods();
if (methods != null) {
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method.getName().equalsIgnoreCase("isFeatureSupport")) {
hasNotch = (boolean) method.invoke(ftFeature, 0x00000020);
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
hasNotch = false;
}
return hasNotch;
}
/**
* HUAWEI
* com.huawei.android.util.HwNotchSizeUtil
* public static boolean hasNotchInScreen()
*
* @param context Context
* @return hasNotch
*/
public static boolean hasNotchInHuawei(Context context) {
boolean hasNotch = false;
try {
ClassLoader cl = context.getClassLoader();
Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
hasNotch = (boolean) get.invoke(HwNotchSizeUtil);
} catch (Exception e) {
e.printStackTrace();
}
return hasNotch;
}
/**
* Mi
* @param context
* @return SystemProperties 如果无法导入,请配置gradle
* 进行android.os.SystemProperties隐藏类的导入
*/
public static boolean hasNotchInMi(Context context) {
if (SystemProperties.getInt("ro.miui.notch", 0) == 1) {
return true;
} else {
return false;
}
}
}
下方的 gradle 配置,你可能用的到,目的是导入 android.os.SystemProperties 隐藏类
android {
...
//以下是为了找到android.os.SystemProperties这个隐藏的类
String SDK_DIR = System.getenv("ANDROID_SDK_HOME")
//("TAG", "SDK_DIR = " + SDK_DIR );
if(SDK_DIR == null) {
Properties props = new Properties()
props.load(new FileInputStream(project.rootProject.file("local.properties")))
SDK_DIR = props.get('sdk.dir');
}
dependencies {
provided files("${SDK_DIR}/platforms/android-21/data/layoutlib.jar")
}
//以上是为了找到android.os.SystemProperties这个隐藏的类
...
}
剩下的配置,就得根据我们项目的具体需求,去做不一样的配置即可,当然了,肯定会用到上方我说到的那几个官网的适配方案。
感兴趣的同学,可以对上方的工具类进行丰富哦!
网友评论