国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現屏幕各尺寸的獲取的示例

2019-10-22 18:28:43
字體:
來源:轉載
供稿:網友

在開發中我們會遇到各種需要獲得屏幕參數的場景,當中也有不少坑,所以現在就記錄一下這些參數的獲取方式。以免再入坑。

物理屏幕寬高

一、底部沒有虛擬按鍵

這里獲取到的寬高,就是你眼睛能看到的,屏幕亮著的地方的寬高。

  /**   * 獲取屏幕的寬   *   * @param context   * @return   */  public static int getScreenWidth(Context context) {    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);    DisplayMetrics dm = new DisplayMetrics();    wm.getDefaultDisplay().getMetrics(dm);    return dm.widthPixels;  }  /**   * 獲取屏幕的高度   *   * @param context   * @return   */  public static int getScreenHeight(Context context) {    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);    DisplayMetrics dm = new DisplayMetrics();    wm.getDefaultDisplay().getMetrics(dm);    return dm.heightPixels;  }

二、底部有虛擬按鍵

華為手機底部都會有一個黑色的虛擬按鍵(NavigationBar),通過上面這個方式得到的屏幕高度是屏幕真是高度-虛擬按鍵的高度。所以有虛擬按鍵的情況獲取屏幕的高度就是另一種方法了。

  public static int getRealHeight(Context context) {    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);    Display display = wm.getDefaultDisplay();    int screenHeight = 0;    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {      DisplayMetrics dm = new DisplayMetrics();      display.getRealMetrics(dm);      screenHeight = dm.heightPixels;      //或者也可以使用getRealSize方法//      Point size = new Point();//      display.getRealSize(size);//      screenHeight = size.y;    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {      try {        screenHeight = (Integer) Display.class.getMethod("getRawHeight").invoke(display);      } catch (Exception e) {        DisplayMetrics dm = new DisplayMetrics();        display.getMetrics(dm);        screenHeight = dm.heightPixels;      }    }    return screenHeight;  }

虛擬按鍵高度

虛擬按鍵(NavigationBar)高度可以通過讀取定義在Android系統尺寸資源中的 navigation_bar_height 獲得。

所以不管虛擬按鍵是顯示還是隱藏,得到的結果都是一樣的。

  public static int getNavigationBarHeight(Context context) {    int navigationBarHeight = -1;    Resources resources = context.getResources();    int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android/57731.html">android/191932.html">android");    if (resourceId > 0) {      navigationBarHeight = resources.getDimensionPixelSize(resourceId);    }    return navigationBarHeight;  }

狀態欄高度

狀態欄就是屏幕頂部顯示時間,電池,wifi 等信息的欄目。

方法一:系統提供了一個Resource類,通過這個類可以獲取資源文件,借此可以獲取 到status_bar_height 。

  public int getStatusBarHeight() {    int result = 0;    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");    if (resourceId > 0) {      result = getResources().getDimensionPixelSize(resourceId);    }    return result;  }

方法2: 通過放射

Android的所有資源都會有惟一標識在R類中作為引用。我們也可以通過反射獲取R類的實例域,然后找 status_bar_height。

  public void getStatusBarHeightByReflect() {    int statusBarHeight2 = -1;    try {      Class<?> clazz = Class.forName("com.android.internal.R$dimen");      Object object = clazz.newInstance();      int height = Integer.parseInt(clazz.getField("status_bar_height")          .get(object).toString());      statusBarHeight2 = getResources().getDimensionPixelSize(height);    } catch (Exception e) {      e.printStackTrace();    }    Log.e(TAG, "狀態欄高度-反射方式:" + statusBarHeight2);  }

借助應用區 top 屬性。

狀態欄位于屏幕的最頂端,坐標從 (0,0) 開始,所以應用區的頂部的位置就是狀態欄的高度。

  /**   * 應用區的頂端位置即狀態欄的高度   * *注意*該方法不能在初始化的時候用   * */  public void getStatusBarHeightByTop() {        Rect rectangle = new Rect();    getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);    Log.e(TAG, "狀態欄高度-應用區頂部:" + rectangle.top);  }

應用區域高度

除去狀態欄剩下的都時應用區。由此可知屏幕的高度 - 狀態欄高度 = 應用區的高度。

/**   * 不能在 onCreate 方法中使用。   * 因為這種方法依賴于WMS(窗口管理服務的回調)。正是因為窗口回調機制,所以在Activity初始化時執行此方法得到的高度是0。   * 這個方法推薦在回調方法onWindowFocusChanged()中執行,才能得到預期結果。   */  public void getAppViewHeight(){    //屏幕    DisplayMetrics dm = new DisplayMetrics();    getWindowManager().getDefaultDisplay().getMetrics(dm);    //應用區域    Rect outRect1 = new Rect();    getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);    int statusBar = dm.heightPixels - outRect1.height(); //狀態欄高度=屏幕高度-應用區域高度    Log.e(TAG, "應用區高度:" + statusBar);  }

setContentView 高度,view 顯示的高度

需要在見面創建后才能獲取到。

public static int getContentViewHeight(Activity activity) {    Rect rectangle= new Rect();    activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rectangle);    return rectangle.height();  }

標題欄高度

標題欄高度 = 應用區高度 - view 顯示高度

  public static void getTitleBarHeight(Activity activity) {    Rect outRect1 = new Rect();    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);    int viewTop = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();  //要用這種方法    int titleBarH = viewTop - outRect1.top;    Log.e(TAG, "標題欄高度-計算:" + titleBarH);  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鹤壁市| 怀仁县| 苍山县| 荥经县| 临汾市| 安新县| 宁城县| 德惠市| 西和县| 定襄县| 永定县| 恩平市| 奉化市| 开远市| 云龙县| 南投市| 古蔺县| 堆龙德庆县| 灵寿县| 酉阳| 连州市| 赣榆县| 都昌县| 西青区| 沂水县| 获嘉县| 泽库县| 广宗县| 凉城县| 禹城市| 荆州市| 昭平县| 南澳县| 鲁甸县| 郯城县| 聂荣县| 日照市| 出国| 湘潭市| 中西区| 肃北|