栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现屏幕各尺寸的获取的示例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android实现屏幕各尺寸的获取的示例

在开发中我们会遇到各种需要获得屏幕参数的场景,当中也有不少坑,所以现在就记录一下这些参数的获取方式。以免再入坑。

物理屏幕宽高

一、底部没有虚拟按键

这里获取到的宽高,就是你眼睛能看到的,屏幕亮着的地方的宽高。

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

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

应用区域高度

除去状态栏剩下的都时应用区。由此可知屏幕的高度 - 状态栏高度 = 应用区的高度。


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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157922.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号