全面屏适配请阅读本人另外一篇博客:

https://blog.csdn.net/zhao8856234/article/details/117744924?spm=1001.2014.3001.5501

直接上工具类,复制使用即可:

public class NotchUtil {private static final String SP_NAME = "MY_NOTCH_SP";//保存异形屏Nameprivate static final String KEY_IS_NOTCH_SCREEN = "KEY_IS_NOTCH_SCREEN";//是否是异形屏KEY/*** 保存当前手机是否是异形屏** @param isNotchScreen 是否是异形屏(刘海屏,水滴屏,挖孔屏等)*/public static void saveScreen(Context context, boolean isNotchScreen) {SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);SharedPreferences.Editor edit = sp.edit();if (!sp.contains(KEY_IS_NOTCH_SCREEN)) {//不存在的时候保存edit.putBoolean(KEY_IS_NOTCH_SCREEN, isNotchScreen);} else if (sp.contains(KEY_IS_NOTCH_SCREEN) && !sp.getBoolean(KEY_IS_NOTCH_SCREEN, false)) {//当这个值存在并且是false的时候也保存edit.putBoolean(KEY_IS_NOTCH_SCREEN, isNotchScreen);}//当这个值存在并且为true的时候不去覆盖数据.edit.commit();}/*** 获取保存在本地的屏幕类型,是否是异形屏(刘海屏,水滴屏,挖孔屏等)*/public static boolean getScreenType(Context context) {return context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE).getBoolean(KEY_IS_NOTCH_SCREEN, false);}/*** 判断是否是8.0系统之后的异形屏(刘海屏,水滴屏,挖孔屏等)* 必须在Activity的onAttachedToWindow()方法中才会生效,其他地方windowInsets为 null* onCreate->onStart->onResume->onAttachedToWindow*/public static boolean IsNotchScreen(Context context, WindowInsets windowInsets) {if (Build.VERSION.SDK_INT >= 26) {if (isAndroidPNotchScreen(windowInsets) || hasNotchInScreenAtVivo(context) || hasNotchInScreenAtOppo(context)|| hasNotchInScreenAtHuawei(context) || hasNotchInScreenAtMI()) { //TODO 各种品牌return true;}}return false;}/*** Android P 异形屏判断** @param windowInsets 必须使用控件的setOnApplyWindowInsetsListener()接口回调方法中的才或者在Activity的*                     onAttachedToWindow()方法中调用getWindow().getDecorView().getRootWindowInsets();*                     其他地方获取会直接为空*/@SuppressLint("NewApi")public static boolean isAndroidPNotchScreen(WindowInsets windowInsets) {boolean isNotchScreen = false;if (PlatformUtils.isAndroidP() && null != windowInsets) {DisplayCutout cutout = windowInsets.getDisplayCutout();if (cutout != null) {List<Rect> rects = cutout.getBoundingRects();if (rects != null && rects.size() > 0) {isNotchScreen = true;}}}return isNotchScreen;}/*** 判断是否华为异形屏*/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);} catch (ClassNotFoundException e) {PlatformLog.ee("hasNotchInScreenAtHuawei()-> ClassNotFoundException");} catch (NoSuchMethodException e) {PlatformLog.ee("hasNotchInScreenAtHuawei()-> NoSuchMethodException");} catch (Exception e) {PlatformLog.ee("hasNotchInScreenAtHuawei()-> Exception");} finally {PlatformLog.e("hasNotchInScreenAtHuawei()-> ClassNotFoundException");return ret;}}/*** 获取华为O版本异形屏宽高*/public static int[] getNotchSizeForHuawei(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) {PlatformLog.ee("HUAWEI-getNotchSize()-> ClassNotFoundException");} catch (NoSuchMethodException e) {PlatformLog.ee("HUAWEI-getNotchSize()-> NoSuchMethodException");} catch (Exception e) {PlatformLog.ee("HUAWEI-getNotchSize()-> Exception");} finally {return ret;}}/*** 判断oppo 机型是否异形屏*/public static boolean hasNotchInScreenAtOppo(Context context) {return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");}public static String getNotchOppoProperty() {String value = "";try {Class<?> cls = Class.forName("android.os.SystemProperties");Method hideMethod = cls.getMethod("get", String.class);Object object = cls.newInstance();value = (String) hideMethod.invoke(object, "ro.oppo.screen.heteromorphism");} catch (ClassNotFoundException e) {PlatformLog.ee("getNotchOppoProperty()->get error() ", e.getMessage());} catch (NoSuchMethodException e) {PlatformLog.ee("getNotchOppoProperty()->get error() ", e.getMessage());} catch (InstantiationException e) {PlatformLog.ee("getNotchOppoProperty()->get error() ", e.getMessage());} catch (IllegalAccessException e) {PlatformLog.ee("getNotchOppoProperty()->get error() ", e.getMessage());} catch (IllegalArgumentException e) {PlatformLog.ee("getNotchOppoProperty()->get error() ", e.getMessage());} catch (InvocationTargetException e) {PlatformLog.ee("getNotchOppoProperty()->get error() ", e.getMessage());}return value;}/*** 判断vivo机型是否异形屏*/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("android.util.FtFeature");Method get = FtFeature.getMethod("isFeatureSupport", int.class);ret = (boolean) get.invoke(FtFeature, NOTCH_IN_SCREEN_VOIO);} catch (ClassNotFoundException e) {PlatformLog.ee("hasNotchInScreenAtVivo()-> ClassNotFoundException");} catch (NoSuchMethodException e) {PlatformLog.ee("hasNotchInScreenAtVivo()-> NoSuchMethodException");} catch (Exception e) {PlatformLog.ee("hasNotchInScreenAtVivo()-> Exception");} finally {return ret;}}/*** 判断小米机型是否异形屏*/private static Method getBooleanMethod = null;public static boolean hasNotchInScreenAtMI() {try {if (getBooleanMethod == null) {getBooleanMethod = Class.forName("android.os.SystemProperties").getMethod("getBoolean", String.class, boolean.class);}//Log.i(TAG,"getBoolean:"+getBooleanMethod.invoke(null, key, def));return (Boolean) getBooleanMethod.invoke(null, "ro.miui.notch", false);} catch (Exception e) {return false;}}}

调用方法:

1. Activity调用:

/*** 生命周期 onCreate->onStart->onResume->onAttachedToWindow* 判断是否是异形屏,必须在此方法*/@Overridepublic void onAttachedToWindow() {super.onAttachedToWindow();if (Build.VERSION.SDK_INT >= 23) {boolean isNotchScreen = NotchUtil.IsNotchScreen(this, getWindow().getDecorView().getRootWindowInsets());NotchUtil.saveScreen(this, isNotchScreen);}}

2. 控件调用:

 public static void setNotchScreenParentLayout(final Context context, final View parentLayout) {//异形屏 刘海屏从安卓8.0系统开始推出if (null != context && null != parentLayout && Build.VERSION.SDK_INT >= 26) {//设置异形屏的安全区域,避免遮挡内容parentLayout.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {@Overridepublic WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {boolean isNotchScreen = NotchUtil.IsNotchScreen(context, windowInsets);if (isNotchScreen && null != windowInsets) {parentLayout.setPadding(0, 0, 0, 0);if (Build.VERSION.SDK_INT >= 28 && null != windowInsets.getDisplayCutout()) {DisplayCutout cutout = windowInsets.getDisplayCutout();if (cutout != null) {List<Rect> rects = cutout.getBoundingRects();if (rects != null && rects.size() > 0) {PlatformLog.e("异形屏->安卓9.0之后系统,间距左" + cutout.getSafeInsetLeft() +",上:" + cutout.getSafeInsetTop() + ",右:" + cutout.getSafeInsetRight() + ",下:" + cutout.getSafeInsetBottom());parentLayout.setPadding(cutout.getSafeInsetLeft(), cutout.getSafeInsetTop(),cutout.getSafeInsetRight(), cutout.getSafeInsetBottom());}}}}return windowInsets;}});}}

Android 检测手机是否是异形屏,刘海屏,水滴屏,挖孔屏相关推荐

  1. 刘海、水滴、挖孔都靠边!中兴吐槽:不配全面屏

    历经两年之久的屏下摄像头手机终于要来了,全球首发的却是中兴品牌,要知道在此之前小米.vivo等厂商也展示过屏下摄像头样机,但不知道是因为什么因素迟迟不见动静,每次都是隔几个月出来冒个泡. 事实上,屏下 ...

  2. 【Android 屏幕适配】异形屏适配 ① ( 异形屏类型:刘海屏、水滴屏、挖孔屏 | 沉浸式布局刘海屏适配 | 华为手机异形屏适配注意点 )

    文章目录 一.异形屏类型:刘海屏.水滴屏.挖孔屏 二.沉浸式布局刘海屏适配 三.华为手机异形屏适配注意点 屏幕适配参考文档 : 设备兼容性概览 屏幕兼容性概览 支持不同的像素密度 声明受限屏幕支持 异 ...

  3. Android 全面屏处理(适配挖孔屏、刘海屏) kotlin

    测试机: Android 11 的 Xiaomi MI MAX 3 Android 12 的 Xiaomi K40 Pro 测试方法: 1. 该方法在api30后提示已经过时 在onCreat()方法 ...

  4. 苹果计划在明年推出的高端iPhone机型上应用挖孔屏

    近日,据国外媒体报道,苹果公司计划在明年推出的高端iPhone机型上应用挖孔屏. 此前,海外博主带来了最新的iPhone 14 Pro系列的概念渲染图,提前展示了该机的外观设计方案.图片显示,iPho ...

  5. iPhone 14将采用挖孔屏?不全是,只有两款采用挖孔屏

    每一代的iPhone在发布前,都会引起网友的热烈讨论,在iPhone13发布之前,就有爆料人士透露,iPhone14会在设计上做出改变,不再采用刘海屏,而是改成挖孔屏. 最新的报道显示,iPhone1 ...

  6. Android 挖孔屏适配

    刘海屏刚出来那会儿,怎么看怎么丑,后面又是水滴屏,稍微有点顺眼,再就是挖孔屏,都是啥玩意. 现在再看,刘海似乎也没那么丑了,居然还觉得挖孔的有点惊艳! 需求 前段时间在做挖孔屏适配,中间就不要有刘海, ...

  7. 2022 年苹果新品预测:iPhone 14 采用“挖孔屏”、Mac 全面升级?

    从收入与市值来看,2021 年对苹果而言无疑是成功的一年: 据去年 10 月 28 日苹果财报显示,截至 9 月 30 日其营业收入已达 3658.17 亿美元,较同期上涨 33.26%: 同时,20 ...

  8. 苹果前置摄像头拍出来是反的怎么调_不用等了!屏下摄像头难攻克,明年还是挖孔屏...

    看过目前市面上所有的手机外观,网友们表示更期待的还是屏下摄像头技术.打孔也好,刘海也好,水滴美人尖也好,升降机械结构也好,传统外观也好,折叠也好,都没能真正的解决用户们呼唤已久的"手机正面全 ...

  9. 怎么在长方体上挖孔_华为P50将至,颜值升级!用上和三星旗舰一样的中置挖孔屏?...

    华为P系列近些年采用的屏幕设计风格并不一样,例如华为P20系列采用的是"刘海屏",华为P30系列采用的是"水滴屏",华为P40系列采用的是左侧"挖孔屏 ...

  10. 华为p40还用麒麟990鸿蒙,华为P40渲染图曝光,2K挖孔屏+麒麟990+鸿蒙OS

    (文章来源:百家号) 在目前的众多手机品牌中,大部分国外的手机品牌在中国市场都是寸步难行,之所以这样,最主要的原因就是国产手机崛起,目前很多国内消费者都比较喜欢国产手机,比如华为手机.小米手机.viv ...

最新文章

  1. 2022-2028年中国喷涂速凝橡胶行业市场调研分析及未来前景分析报告
  2. pycharm和python区别-一些 PyCharm 的使用和设置建议
  3. 【维纳滤波】通过MATLAB自带的维纳滤波函数进行滤波
  4. 数据蒋堂 | 怎样看待存储过程的移植困难
  5. Unix原理与应用学习笔记----第五章 普通文件3
  6. 递归基础之N皇后问题
  7. ubuntu python3.7修改默认pip版本_Ubuntu16.04安装Python3.7及其pip3并切换为默认版本
  8. cocos creator基础-(十三)cc.Loader使用
  9. 灯塔浮标行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  10. 国内外著名PT网站大全
  11. Unity的C#编程教程_17_Variables 挑战 3 折扣计算器
  12. 从大数据+AI 谈谈概念与场景
  13. VMware虚拟机安装windows系统
  14. 日本海淘转运运输方式对比及如何选择转运渠道
  15. 渣土车空车未盖盖识别系统 OpenCv
  16. hadoop学习-1
  17. 第四章——确定性推理
  18. ipref网络性能评估工具
  19. mysql基础篇-archive引擎
  20. 如何使用 Lightly 分享项目?

热门文章

  1. DMSP/OLS夜间灯光遥感影像数据概述
  2. 波浪数,51nod1788,根号分治+Meet in the Middle
  3. KK 的99 条额外的建议[翻译]#yyds干货盘点#
  4. python mongdb 和 mysql简单使用
  5. oracle用户授权只读,只读权限oracle用户
  6. Chrome浏览器常用快捷键总结
  7. Android - scheme 一个app跳转另一个app、模块开发
  8. 关于电脑显示器分辨率只有两个选项1024×768和800×600的解决办法,本人电脑亲测可用
  9. 微信小程序调试过程中页面加载不出来
  10. 雷神simplest_ffmpeg_player解析(二)