Android 刘海、挖孔、水滴屏幕的状态高度获取与适配
一般需要处理状态栏的问题都是适配问题,适配就稍微有点麻烦,比如:刘海屏,水滴屏、以及刚刚不久出的挖孔屏等这类都属于刘海屏范围内,适配的话就要在各大厂商做处理了,比如华为:刚刚开始以为只需判断是否是刘海屏就好了,设置一个状态栏高度就好Ok了,结果不然:
状态栏高度:
private fun setStatusHeight() {val layoutParams = base_head.layoutParams as LinearLayout.LayoutParamslayoutParams.setMargins(0,StatusBarUtils.getStatusBarHeight(mContext),0,0)base_head.layoutParams = layoutParams}
/*** 获得通知栏高度*/public static int getStatusBarHeight(Context context) {Class<?> c = null;Object obj = null;Field field = null;int x = 0, statusBarHeight = 0;try {c = Class.forName("com.android.internal.R$dimen");obj = c.newInstance();field = c.getField("status_bar_height");x = Integer.parseInt(field.get(obj).toString());statusBarHeight = context.getResources().getDimensionPixelSize(x);} catch (Exception e1) {e1.printStackTrace();}return statusBarHeight;}
因为这里设置的是铺满全屏,需要重新计算状态栏高度,在小米、oppo、华为的刘海屏和水滴屏幕没有问题,但在华为的挖孔屏上出现问题,就是常见的重叠,其他品牌的手机因为没有,不知道有没有出现类似的问题,先说问题原因是:华为的状态栏高度是获取到了的值很小,只有26仔细看到话在挖孔的上面很小的空白处是有高度的那个就是状态栏高度,导致出现重叠的原因,解决方法就是获取挖孔的高度,把值设置进去就好了;
/*** 获取华为刘海的高** @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];}
这个是专门针对华为过去的高度,其他品牌获取的值为0,现在只适配了这些后期有问题会补上。
/*** 是否有刘海屏** @return*/public static boolean hasNotchInScreen(Activity activity) {// android P 以上有标准 API 来判断是否有刘海屏if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {View decorView = activity.getWindow().getDecorView();WindowInsets windowInsets = decorView.getRootWindowInsets();if (windowInsets != null) {DisplayCutout displayCutout = windowInsets.getDisplayCutout();if (displayCutout != null) {List<Rect> rects = displayCutout.getBoundingRects();for (Rect rect : rects) {Log.e("notch", "cutout.getSafeInsetTop():" + displayCutout.getSafeInsetTop()+ ", cutout.getSafeInsetBottom():" + displayCutout.getSafeInsetBottom()+ ", cutout.getSafeInsetLeft():" + displayCutout.getSafeInsetLeft()+ ", cutout.getSafeInsetRight():" + displayCutout.getSafeInsetRight()+ ", cutout.rects:" + rect);}int screenTopMargin = displayCutout.getSafeInsetTop();int screenBottomMargin = displayCutout.getSafeInsetBottom();//通过判断是否存在rects来确定是否刘海屏手机if (rects.size() > 0) {return true;}}}}// 通过其他方式判断是否有刘海屏 目前官方提供有开发文档的就 小米,vivo,华为(荣耀),oppoString manufacturer = Build.MANUFACTURER;if (StringUtils.isEmpty(manufacturer)) {return false;} else if (manufacturer.equalsIgnoreCase("HUAWEI")) {return hasNotchHw(activity);} else if (manufacturer.equalsIgnoreCase("xiaomi")) {return hasNotchXiaoMi(activity);} else if(manufacturer.equalsIgnoreCase("oppo")||manufacturer.equalsIgnoreCase("realme")) {//这里是除oppo以外的品牌手机,这里暂时没有这个手机,但也加上是评论区的伙伴验证的return hasNotchOPPO(activity);} else if (manufacturer.equalsIgnoreCase("vivo")) {return hasNotchVIVO(activity);} else {return false;}}/*** 获取华为刘海的高** @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];}/*** 判断vivo是否有刘海屏* https://swsdl.vivo.com.cn/appstore/developer/uploadfile/20180328/20180328152252602.pdf** @param activity* @return*/private static boolean hasNotchVIVO(Activity activity) {try {@SuppressLint("PrivateApi") Class<?> c = Class.forName("android.util.FtFeature");Method get = c.getMethod("isFeatureSupport", int.class);return (boolean) (get.invoke(c, 0x20));} catch (Exception e) {e.printStackTrace();return false;}}/*** 判断oppo是否有刘海屏* https://open.oppomobile.com/wiki/doc#id=10159** @param activity* @return*/private static boolean hasNotchOPPO(Activity activity) {return activity.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");}/*** 判断xiaomi是否有刘海屏* https://dev.mi.com/console/doc/detail?pId=1293** @param activity* @return*/private static boolean hasNotchXiaoMi(Activity activity) {try {@SuppressLint("PrivateApi") Class<?> c = Class.forName("android.os.SystemProperties");Method get = c.getMethod("getInt", String.class, int.class);return (int) (get.invoke(c, "ro.miui.notch", 0)) == 1;} catch (Exception e) {e.printStackTrace();return false;}}/*** 判断华为是否有刘海屏* https://devcenter-test.huawei.com/consumer/cn/devservice/doc/50114** @param activity* @return*/private static boolean hasNotchHw(Activity activity) {try {ClassLoader cl = activity.getClassLoader();Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");return (boolean) get.invoke(HwNotchSizeUtil);} catch (Exception e) {return false;}}
Android 刘海、挖孔、水滴屏幕的状态高度获取与适配相关推荐
- Android刘海屏、水滴屏全面屏适配
现在,市面上的屏幕尺寸和全面屏方案五花八门.这里我使用了小米的图来说明: 上述两种屏幕都可以统称为刘海屏,不过对于右侧较小的刘海,业界一般称为水滴屏或美人尖.为便于说明,后文提到的「刘海屏」「刘海区」 ...
- Android刘海屏、水滴屏全面屏适配详解
现在,市面上的屏幕尺寸和全面屏方案五花八门.这里我使用了小米的图来说明: 上述两种屏幕都可以统称为刘海屏,不过对于右侧较小的刘海,业界一般称为水滴屏或美人尖.为便于说明,后文提到的「刘海屏」「刘海区」 ...
- Android刘海屏、水滴屏全面屏适配。
现在,市面上的屏幕尺寸和全面屏方案五花八门.这里我使用了小米的图来说明:上述两种屏幕都可以统称为刘海屏,不过对于右侧较小的刘海,业界一般称为水滴屏或美人尖.为便于说明,后文提到的「刘海屏」「刘海区」都 ...
- Android刘海屏、水滴屏全面屏适配详解,androidui基础教程
适配方式 适配方式有两种: 将targetSdkVersion版本设置到API 24及以上: 这个操作将会为<application> 标签隐式添加一个属性,android:resizea ...
- Android刘海屏、水滴屏全面屏适配详解,997页字节跳动Android面试真题解析火爆全网
适配方式 适配方式有两种: 将targetSdkVersion版本设置到API 24及以上: 这个操作将会为<application> 标签隐式添加一个属性,android:resizea ...
- Android刘海屏、水滴屏全面屏适配详解,android开发实战湖南师范大学出版社
android:resizeableActivity=["true" | "false"] 如果该属性设置为 true,Activity 将能以分屏和自由形状模 ...
- Android刘海屏、水滴屏全面屏适配详解,android开发应用实战详解
适配方式 适配方式有两种: 将targetSdkVersion版本设置到API 24及以上: 这个操作将会为<application> 标签隐式添加一个属性,android:resizea ...
- Android刘海屏、水滴屏全面屏适配方案
原文地址:https://www.jianshu.com/p/2b8db60ba8df 我将适配方案整理后,封装成了一个库并上传至github,可参考使用 项目地址: https://github.c ...
- Android刘海屏、水滴屏全面屏适配方案,海量算法高频面试题精编解析
对Activity生效,意味着可以针对单个页面进行刘海屏适配,设置了该属性的Activity系统将不会做特殊处理: 方案二 对Application生效,意味着该应用的所有页面,系统都不会做竖屏场景的 ...
最新文章
- ecshop模板smarty foreach详解 [ECshop]
- django rest framework------得心应手
- 最实用的Office使用技巧
- Xstudio+Bugzilla
- profile之springboot
- [国家集训队] tree Ⅱ
- matlab中计算不等式的解,matlab解不等式
- cnpm能用npm install吗_指纹锁一般能用几年? 指纹锁没电了怎么办
- Tensorboard可视化具体做法
- 多表查询,初识pymysql模块
- 关于使用TestDriven.Net 2.0的一点补充
- android 设置系统屏幕亮度
- 货币代码,不允许ALE通讯
- 大数据概览以及当下实用的主流开发
- 2018谷歌大会android,谷歌召开2018中国开发者大会,带来了Android 9多项更新
- Android 10 SystemUI 如何隐藏状态栏耳机图标和定位图标
- INDEX函数与MATCH函数嵌套使用技巧
- 阿里巴巴校招一道笔试题
- linux路由器还原,openwrt路由器恢复出厂设置的方法
- 外汇和股票有什么区别啊?
热门文章
- java基本算术训练
- python导出百万数据到excel_[宜配屋]听图阁
- sureface 屏幕残影问题官方解决方案 - 卸载显卡驱动
- Linux ❉ vimrc文件详解
- 使用python+Keras检测年龄和性别
- 单位社保缴纳明细表_职工社保缴费明细表
- 计蒜客 掎角之势 (三角形内切圆与外接圆面积)
- c#学习unity3d_通过制作数字桌面游戏学习C#和Unity
- Java设计模式——观察者模式与Spring事件监听器
- [Linux 基础] -- Linux 如何查询 OS、CPU、内存、硬盘信息?