a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

詳解Android 華為凹口屏適配小結

2019-10-21 21:25:13
字體:
來源:轉載
供稿:網友

Android8.0以后【凹口屏】得到迅速發展,目前已有了挖孔屏/水滴屏/劉海屏等各式各樣的屏幕,究其根本依舊是【凹口屏】,單華為一個品牌就涵蓋了基本所有類型,而對于屏幕適配也是不可逃避的問題。小菜單獨對華為各型號屏幕進行適配嘗試,部分方法可通用到其他品牌設備,為 Android 標準 SDK 方法。

其實凹口屏已經出現很久了,對于獲取凹口寬高的方式也有很多種,但是以前主流的凹口屏中凹口位置一般是位于屏幕正上方,但隨著發展,也出現了在左上角的挖孔屏樣式。相應的, Android 9.0 即 SDK28 也發布了獲取凹口屏的方法。

Android 9.0 以下適配方案

對華為設備凹口屏適配情況來說,若僅需獲取凹口位置的寬高,如下方法即可,在 Android 各版本中均可( Android 9.0 及以上亦可)。此時獲取屏幕水平方向安全位置時,可根據屏幕寬度-凹口寬度再左右均分即可。

/** * 華為凹口屏判斷方法 Android 各版本均可 * @param context * @return */public static boolean hasNotchInScreen(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) {    Log.e(TAG, "hasNotchInScreen ClassNotFoundException");  } catch (NoSuchMethodException e) {    Log.e(TAG, "hasNotchInScreen NoSuchMethodException");  } catch (Exception e) {    Log.e(TAG, "hasNotchInScreen Exception");  } finally {    return ret;  }}/** * 華為凹口屏寬高獲取方式 int[]{width, height} * @param context * @return */public static int[] getNotchSize(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(TAG, "getNotchSize ClassNotFoundException");  } catch (NoSuchMethodException e) {    Log.e(TAG, "getNotchSize NoSuchMethodException");  } catch (Exception e) {    Log.e(TAG, "getNotchSize Exception");  } finally {    notchWidth = ret[0];    notchHeight = ret[1];    return ret;  }}

Android,凹口屏,適配

Android,凹口屏,適配

Android 9.0 及以上適配

對于華為新出的挖孔屏設備基本均為 Android 9.0 及以上, Android 9.0 提供了對凹口屏相關的 SDK ,谷歌認為凹口位置可以不固定位置也不固定個數,但是對于設備一條邊只能有一個;如下方法對于 Android 9.0 及以上設備判斷均可。 SDK 不僅可以判斷是否為凹口屏,同時可以獲取各個凹口大小及所在位置。

步驟如下: 升級 build.gradle 中 compileSdkVersion 或 targetSdkVersion 為 28 ; 在 Application 或 Activity 中設置 meta-data 屬性,小菜測試不設置亦可;

<meta-data android:name="android.notch_support" android:value="true"/>

根據如下方法獲取相應參數;

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {  getSupportActionBar().hide();  getWindow().getDecorView()    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);  //設置頁面全屏顯示  WindowManager.LayoutParams lp = getWindow().getAttributes();  lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;  //設置頁面延伸到凹口區顯示  getWindow().setAttributes(lp);  getWindow().getDecorView()    .findViewById(android.R.id.content)    .getRootView()    .setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {      @Override      public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {        DisplayCutout cutout = windowInsets.getDisplayCutout();        if (cutout == null) {          Log.e(TAG, "cutout==null, is not notch screen");//通過cutout是否為null判斷是否凹口手機          isNotchScreen = false;        } else {          List<Rect> rects = cutout.getBoundingRects();          if (rects == null || rects.size() == 0) {            Log.e(TAG, "rects==null || rects.size()==0, is not notch screen");            isNotchScreen = true;          } else {            Log.e(TAG, "rect size:" + rects.size());//注意:凹口的數量可以是多個            isNotchScreen = true;            for (Rect rect : rects) {              notchRight = rect.right;              notchLeft = rect.left;              notchTop = rect.top;              notchBottom = rect.bottom;              notchWidth = notchRight - notchLeft;              notchHeight = notchBottom - notchLeft;              safeLeft = cutout.getSafeInsetLeft();              safeRight = cutout.getSafeInsetRight();              safeTop = cutout.getSafeInsetTop();              safeBottom = cutout.getSafeInsetBottom();            }          }        }        return windowInsets;      }    });}

Android,凹口屏,適配

Android,凹口屏,適配

Android,凹口屏,適配

Android,凹口屏,適配

注意事項: 小菜在設置 Application 或 Activity 的主題為 NoActionBar 樣式,此時要去掉 getSupportActionBar().hide(); 否則會報空指針異常;

<style name="NoBarTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item></style>

如下設置全屏使用凹口屏時要注意 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN ,否則參數很有可能獲取不到;

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);WindowManager.LayoutParams lp = getWindow().getAttributes();lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;getWindow().setAttributes(lp);

設置主題 NoActionBar 或代碼中動態設置 getSupportActionBar().hide(); 展示效果在 Android 9.0 以下有部分差異,如下:

Android,凹口屏,適配

NoActionBar 主題

Android,凹口屏,適配

AppTheme 主題

對于凹口屏適配還有很多機型要單獨處理,以上僅對華為設備進行參考;如果有不對的地方還希望多多指出。也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美日韩久久精品 | 精品一区二区三区国产 | 涩涩涩涩 | 国产小视频免费观看 | 精品黑人一区二区三区久久 | 久久女人网 | 国产中文视频 | 91av在线免费看 | 欧美成人手机在线 | 亚洲精品入口a级 | 欧洲在线视频 | 免费成人高清 | 国产精品中文字幕一区二区 | 一区二区视频 | 一区二区三区国产 | 精品亚洲永久免费精品 | 国产在线小视频 | 娇小12-13╳yⅹ╳毛片 | 99久久国产 | 免费观看一区二区三区毛片 | 久久久久综合网 | 欧洲美女7788成人免费视频 | 欧美一区二区精品久久 | 久久伊人成人 | 日韩亚洲视频 | 欧美精品一区在线 | 中文字幕一页二页 | 国产精品一区二区免费视频 | 欧美一级片在线 | 亚洲精品不卡 | 韩日视频在线观看 | 国产精品久久嫩一区二区 免费 | 亚洲成人1区 | 国产精品久久久久9999鸭 | 日韩久久一区 | 国产欧美日韩综合精品一区二区 | 亚洲国产成人久久综合一区,久久久国产99 | 亚洲电影免费 | 一区二区三区四区久久 | 国产激情在线观看视频 | 成人一区二区三区 |