亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)屏幕各尺寸的獲取的示例

 更新時(shí)間:2017年09月01日 14:09:20   作者:酸甜小黃瓜  
本篇文章主要介紹了Android實(shí)現(xiàn)屏幕各尺寸的獲取的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

在開發(fā)中我們會(huì)遇到各種需要獲得屏幕參數(shù)的場景,當(dāng)中也有不少坑,所以現(xiàn)在就記錄一下這些參數(shù)的獲取方式。以免再入坑。

物理屏幕寬高

一、底部沒有虛擬按鍵

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

  /**
   * 獲取屏幕的寬
   *
   * @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;
  }

二、底部有虛擬按鍵

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

  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系統(tǒng)尺寸資源中的 navigation_bar_height 獲得。

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

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

狀態(tài)欄高度

狀態(tài)欄就是屏幕頂部顯示時(shí)間,電池,wifi 等信息的欄目。

方法一:系統(tǒng)提供了一個(gè)Resource類,通過這個(gè)類可以獲取資源文件,借此可以獲取 到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的所有資源都會(huì)有惟一標(biāo)識(shí)在R類中作為引用。我們也可以通過反射獲取R類的實(shí)例域,然后找 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, "狀態(tài)欄高度-反射方式:" + statusBarHeight2);
  }

借助應(yīng)用區(qū) top 屬性。

狀態(tài)欄位于屏幕的最頂端,坐標(biāo)從 (0,0) 開始,所以應(yīng)用區(qū)的頂部的位置就是狀態(tài)欄的高度。

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

應(yīng)用區(qū)域高度

除去狀態(tài)欄剩下的都時(shí)應(yīng)用區(qū)。由此可知屏幕的高度 - 狀態(tài)欄高度 = 應(yīng)用區(qū)的高度。

/**
   * 不能在 onCreate 方法中使用。
   * 因?yàn)檫@種方法依賴于WMS(窗口管理服務(wù)的回調(diào))。正是因?yàn)榇翱诨卣{(diào)機(jī)制,所以在Activity初始化時(shí)執(zhí)行此方法得到的高度是0。
   * 這個(gè)方法推薦在回調(diào)方法onWindowFocusChanged()中執(zhí)行,才能得到預(yù)期結(jié)果。
   */
  public void getAppViewHeight(){
    //屏幕
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    //應(yīng)用區(qū)域
    Rect outRect1 = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
    int statusBar = dm.heightPixels - outRect1.height(); //狀態(tài)欄高度=屏幕高度-應(yīng)用區(qū)域高度
    Log.e(TAG, "應(yīng)用區(qū)高度:" + statusBar);
  }

setContentView 高度,view 顯示的高度

需要在見面創(chuàng)建后才能獲取到。

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

標(biāo)題欄高度

標(biāo)題欄高度 = 應(yīng)用區(qū)高度 - 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, "標(biāo)題欄高度-計(jì)算:" + titleBarH);
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論