欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android 華為凹口屏適配小結(jié)

 更新時間:2019年02月20日 08:26:02   作者:阿策神奇  
這篇文章主要介紹了Android 華為凹口屏適配小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

Android 9.0 以下適配方案

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

/**
 * 華為凹口屏判斷方法 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 9.0 及以上適配

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

步驟如下: 升級 build.gradlecompileSdkVersiontargetSdkVersion28 ; 在 ApplicationActivity 中設(shè)置 meta-data 屬性,小菜測試不設(shè)置亦可;

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

根據(jù)如下方法獲取相應(yīng)參數(shù);

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);
  //設(shè)置頁面全屏顯示
  WindowManager.LayoutParams lp = getWindow().getAttributes();
  lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
  //設(shè)置頁面延伸到凹口區(qū)顯示
  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());//注意:凹口的數(shù)量可以是多個
            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;
      }
    });
}

 

 

 

 

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

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

如下設(shè)置全屏使用凹口屏?xí)r要注意 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN ,否則參數(shù)很有可能獲取不到;

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);

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

NoActionBar 主題

AppTheme 主題

對于凹口屏適配還有很多機型要單獨處理,以上僅對華為設(shè)備進(jìn)行參考;如果有不對的地方還希望多多指出。也希望大家多多支持腳本之家。

相關(guān)文章

  • Android制作漂亮自適布局鍵盤的方法

    Android制作漂亮自適布局鍵盤的方法

    最近做了個自定義鍵盤,但面對不同分辨率的機型其中數(shù)字鍵盤不能根據(jù)界面大小自已鋪滿,但又不能每種機型都做一套吧,所以要做成自適應(yīng),那這里主講思路,感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Android 實現(xiàn)抖音小游戲潛艇大挑戰(zhàn)的思路詳解

    Android 實現(xiàn)抖音小游戲潛艇大挑戰(zhàn)的思路詳解

    《潛水艇大挑戰(zhàn)》是抖音上的一款小游戲,最近特別火爆,很多小伙伴都玩過。接下來通過本文給大家分享Android 手?jǐn)]抖音小游戲潛艇大挑戰(zhàn)的思路,需要的朋友可以參考下
    2020-04-04
  • Android 調(diào)用發(fā)送短信的方法

    Android 調(diào)用發(fā)送短信的方法

    這篇文章主要介紹了Android 調(diào)用發(fā)送短信的方法的相關(guān)資料,主要實現(xiàn)Android 調(diào)用短信的使用,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • Android資源命名規(guī)范 分享

    Android資源命名規(guī)范 分享

    Android資源命名規(guī)范 分享,需要的朋友可以參考一下
    2013-06-06
  • android?viewflipper實現(xiàn)左右滑動切換顯示圖片

    android?viewflipper實現(xiàn)左右滑動切換顯示圖片

    這篇文章主要為大家詳細(xì)介紹了android?viewflipper實現(xiàn)左右滑動切換顯示圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android 自定義view仿支付寶咻一咻功能

    Android 自定義view仿支付寶咻一咻功能

    支付寶上有一個咻一咻的功能,就是點擊圖片后四周有水波紋的這種效果,今天就通過本文給大家分享實現(xiàn)此功能的思路及實例代碼,一起看看吧
    2016-12-12
  • Android底部菜單欄實現(xiàn)的實例代碼

    Android底部菜單欄實現(xiàn)的實例代碼

    這篇文章主要介紹了Android底部菜單欄實現(xiàn)的實例代碼,本文通過使用RadioGroup來實現(xiàn)底部導(dǎo)航菜單欄?,F(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android編程之微信SDK分享功能過程步驟詳細(xì)分析

    Android編程之微信SDK分享功能過程步驟詳細(xì)分析

    這篇文章主要介紹了Android編程之微信SDK分享功能過程步驟詳細(xì)分析,較為詳細(xì)的分析了Android微信SDK分享功能的原理、步驟與相關(guān)注意事項,需要的朋友可以參考下
    2015-10-10
  • Android自定義Dialog框樣式

    Android自定義Dialog框樣式

    這篇文章主要為大家詳細(xì)介紹了Android自定義Dialog框樣式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Android系統(tǒng)進(jìn)程間通信(IPC)機制Binder中的Server啟動過程源代碼分析

    Android系統(tǒng)進(jìn)程間通信(IPC)機制Binder中的Server啟動過程源代碼分析

    本文主要介紹Android IPC機制Binder中的Server啟動過程源代碼,這里對Binder 中Server 啟動過程中的源碼做了詳細(xì)的介紹,有研究Android源碼 Binder 通信的小伙伴可以參考下
    2016-08-08

最新評論