2023年6月27日发(作者:)
Android刘海屏、⽔滴屏全⾯屏适配⼩结现在,市⾯上的屏幕尺⼨和全⾯屏⽅案五花⼋门。这⾥我使⽤了⼩⽶的图来说明:上述两种屏幕都可以统称为刘海屏,不过对于右侧较⼩的刘海,业界⼀般称为⽔滴屏或美⼈尖。为便于说明,后⽂提到的「刘海屏」「刘海区」都同时指代上图两种屏幕。刘海屏、⽔滴屏全⾯屏适配细节当我们在谈屏幕适配时,我们具体谈什么呢?适应更长的屏幕防⽌内容被刘海遮挡其中第⼀点是所有应⽤都需要适配的,对应下⽂的声明最⼤长宽⽐,⽽第⼆点,如果应⽤本⾝不需要全屏显⽰或使⽤沉浸式状态栏,是不需要适配的。针对需要适配第⼆点的应⽤,需要获取刘海的位置和宽⾼,然后将显⽰内容避开即可。声明最⼤长宽⽐以前的普通屏长宽⽐为16:9,全⾯屏⼿机的屏幕长宽⽐增⼤了很多,如果不适配的话就会类似下⾯这样:适配⽅式适配⽅式有两种:1. 将targetSdkVersion版本设置到API 24及以上;这个操作将会为
| InvocationTargetException e) { Log.e("test", "hw add notch screen flag api error"); } catch (Exception e) { Log.e("test", "other Exception"); } }2.清除添加的华为刘海屏Flag,恢复应⽤不使⽤刘海区显⽰。 public static void setNotFullScreenWindowLayoutInDisplayCutout (Window window) { if (window == null) { return; } Params layoutParams = ributes(); try { Class layoutParamsExCls = e("ParamsEx"); Constructor con=structor(); Object layoutParamsExObj=tance(layoutParams); Method method=hod("clearHwFlags", ); (layoutParamsExObj, FLAG_NOTCH_SUPPORT); } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |InstantiationException
| InvocationTargetException e) { Log.e("test", "hw clear notch screen flag api error"); } catch (Exception e) { Log.e("test", "other Exception"); } }⼩⽶Android O适配判断是否是刘海屏。 private static boolean isNotch() { try { Method getInt = e("Properties").getMethod("getInt", , ); int notch = (int) (null, "", 0); return notch == 1; } catch (Throwable ignore) { } return false; }设置显⽰到刘海区域@Override public void setDisplayInNotch(Activity activity) { int flag = 0x00000100 | 0x00000200 | 0x00000400; try { Method method = hod("addExtraFlags", ); (dow(), flag); } catch (Exception ignore) { } }获取刘海宽⾼public static int getNotchHeight(Context context) { int resourceId = ources().getIdentifier("notch_height", "dimen", "android"); if (resourceId > 0) { return ources().getDimensionPixelSize(resourceId); } return 0; } public static int getNotchWidth(Context context) { int resourceId = ources().getIdentifier("notch_width", "dimen", "android"); if (resourceId > 0) { return ources().getDimensionPixelSize(resourceId); } return 0; }oppo Android O适配判断是否是刘海屏@Override public boolean hasNotch(Activity activity) { boolean ret = false; try { ret = kageManager().hasSystemFeature("morphism"); } catch (Throwable ignore) { } return ret; }获取刘海的左上⾓和右下⾓的坐标private static String getScreenValue() { String value = ""; Class> cls; try { cls = e("Properties"); Method get = hod("get", ); Object object = tance(); value = (String) (object, "morphism"); } catch (Throwable ignore) { } return value; }以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
发布者:admin,转转请注明出处:http://www.yc00.com/news/1687841830a49975.html
评论列表(0条)