Android刘海、挖孔、水滴屏幕的状态高度获取与适配

Android刘海、挖孔、水滴屏幕的状态高度获取与适配

2023年6月27日发(作者:)

Android刘海、挖孔、⽔滴屏幕的状态⾼度获取与适配⼀般需要处理状态栏的问题都是适配问题,适配就稍微有点⿇烦,⽐如:刘海屏,⽔滴屏、以及刚刚不久出的挖孔屏等这类都属于刘海屏范围内,适配的话就要在各⼤⼚商做处理了,⽐如华为:刚刚开始以为只需判断是否是刘海屏就好了,设置⼀个状态栏⾼度就好Ok了,结果不然:状态栏⾼度: private fun setStatusHeight() { val layoutParams = base_Params as Params gins(0,tusBarHeight(mContext),0,0) base_Params = layoutParams }/** * 获得通知栏⾼度 */ public static int getStatusBarHeight(Context context) { Class c = null; Object obj = null; Field field = null; int x = 0, statusBarHeight = 0; try { c = e("al.R$dimen"); obj = tance(); field = ld("status_bar_height"); x = nt((obj).toString()); statusBarHeight = ources().getDimensionPixelSize(x); } catch (Exception e1) { tackTrace(); } return statusBarHeight; }因为这⾥设置的是铺满全屏,需要重新计算状态栏⾼度,在⼩⽶、oppo、华为的刘海屏和⽔滴屏幕没有问题,但在华为的挖孔屏上出现问题,就是常见的重叠,其他品牌的⼿机因为没有,不知道有没有出现类似的问题,先说问题原因是:华为的状态栏⾼度是获取到了的值很⼩,只有26仔细看到话在挖孔的上⾯很⼩的空⽩处是有⾼度的那个就是状态栏⾼度,导致出现重叠的原因,解决⽅法就是获取挖孔的⾼度,把值设置进去就好了; /** * 获取华为刘海的⾼ * * @param context * @return */ public static int getNotchSizeAtHuawei(Context context) { int[] ret = new int[]{0, 0}; try { ClassLoader cl = ssLoader(); Class HwNotchSizeUtil = ass("hSizeUtil"); Method get = hod("getNotchSize"); ret = (int[]) (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 (_INT >= N_CODES.P) { View decorView = dow().getDecorView(); WindowInsets windowInsets = tWindowInsets(); if (windowInsets != null) { DisplayCutout displayCutout = playCutout(); if (displayCutout != null) { List rects = ndingRects(); for (Rect rect : rects) { Log.e("notch", "eInsetTop():" + eInsetTop() + ", eInsetBottom():" + eInsetBottom() + ", eInsetLeft():" + eInsetLeft() + ", eInsetRight():" + eInsetRight() + ", :" + rect ); } int screenTopMargin = eInsetTop(); int screenBottomMargin = eInsetBottom(); //通过判断是否存在rects来确定是否刘海屏⼿机 if (() > 0) { return true; } } } } // 通过其他⽅式判断是否有刘海屏 ⽬前官⽅提供有开发⽂档的就 ⼩⽶,vivo,华为(荣耀),oppo String manufacturer = CTURER; if (y(manufacturer)) { if (y(manufacturer)) { return false; } else if (IgnoreCase("HUAWEI")) { return hasNotchHw(activity); } else if (IgnoreCase("xiaomi")) { return hasNotchXiaoMi(activity); return hasNotchOPPO(activity); } else if (IgnoreCase("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 = ssLoader(); Class HwNotchSizeUtil = ass("hSizeUtil"); Method get = hod("getNotchSize"); ret = (int[]) (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]; } } else if(IgnoreCase("oppo")||IgnoreCase("realme")) {//这⾥是除oppo以外的品牌⼿机,这⾥暂时没有这个⼿机,但也 /** * 判断vivo是否有刘海屏 * /appstore/developer/uploadfile/20180328/ * * @param activity * @return */ private static boolean hasNotchVIVO(Activity activity) { try { @SuppressLint("PrivateApi") Class c = e("ure"); Method get = hod("isFeatureSupport", ); return (boolean) ((c, 0x20)); } catch (Exception e) { tackTrace(); return false; } } /** * 判断oppo是否有刘海屏 * /wiki/doc#id=10159 * * @param activity * @return */ private static boolean hasNotchOPPO(Activity activity) { return kageManager().hasSystemFeature("morphism"); } /** * 判断xiaomi是否有刘海屏 * /console/doc/detail?pId=1293 * * @param activity * @return */ private static boolean hasNotchXiaoMi(Activity activity) { try { @SuppressLint("PrivateApi") Class c = e("Properties"); Method get = hod("getInt", , ); return (int) ((c, "", 0)) == 1; } catch (Exception e) { tackTrace(); return false; } } /** * 判断华为是否有刘海屏 * /consumer/cn/devservice/doc/50114 * * @param activity * @return */ private static boolean hasNotchHw(Activity activity) { try { ClassLoader cl = ssLoader(); Class HwNotchSizeUtil = ass("hSizeUtil"); Method get = hod("hasNotchInScreen"); return (boolean) (HwNotchSizeUtil); } catch (Exception e) { return false; } }

发布者:admin,转转请注明出处:http://www.yc00.com/news/1687840117a49833.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信