zoukankan      html  css  js  c++  java
  • Android获取屏幕宽高的方法

    1. 实现代码

        
        private int mWidth;
        private int mHeight;
        
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        public void getDisplayMetrics() {
            WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            int API_LEVEL = android.os.Build.VERSION.SDK_INT;
            // 方法1
            DisplayMetrics displayMetrics = new DisplayMetrics();
            if (API_LEVEL >= 17) {
                display.getRealMetrics(displayMetrics);
            } else {
                display.getMetrics(displayMetrics);
            }
            mWidth = displayMetrics.widthPixels;
            mHeight = displayMetrics.heightPixels;
                
            // 方法2
            Point outSize = new Point();
            if (API_LEVEL >= 17) {
                display.getRealSize(outSize);
            } else {
                display.getSize(outSize);
            }
            mWidth = outSize.x;
            mHeight = outSize.y;
            
            // 方法3
            mWidth = mContext.getResources().getDisplayMetrics().widthPixels;
            mHeight = mContext.getResources().getDisplayMetrics().heightPixels;
        }

    2. 说明

    Display

    Provides information about the size and density of a logical display.

    The display area is described in two different ways.

    • The application display area specifies the part of the display that may contain an application window, excluding the system decorations. The application display area may be smaller than the real display area because the system subtracts the space needed for decor elements such as the status bar. Use the following methods to query the application display area: getSize(Point)getRectSize(Rect) and getMetrics(DisplayMetrics).
    • The real display area specifies the part of the display that contains content including the system decorations. Even so, the real display area may be smaller than the physical size of the display if the window manager is emulating a smaller display using (adb shell am display-size). Use the following methods to query the real display area: getRealSize(Point)getRealMetrics(DisplayMetrics).

    3. Note

    public void getRealMetrics (DisplayMetrics outMetrics)

    Added in API level 17

    Gets display metrics based on the real size of this display.

    The size is adjusted based on the current rotation of the display.

    The real size may be smaller than the physical size of the screen when the window manager is emulating a smaller display (using adb shell am display-size).

    public void getRealSize (Point outSize)

    Added in API level 17

    Gets the real size of the display without subtracting any window decor or applying any compatibility scale factors.

    The size is adjusted based on the current rotation of the display.

    The real size may be smaller than the physical size of the screen when the window manager is emulating a smaller display (using adb shell am display-size).

  • 相关阅读:
    ASP.NET MVC之从控制器传递数据到视图四种方式
    MVC发布到IIS,出现HTTP 错误 404.0
    超详细MySQL安装及基本使用教程
    node.js中使用node-xlsx插件生成excel数据并导出
    jquery给一组radio赋值和取值
    node.js生成excel下载各种方法分析对比--附excel-export方法
    JS中substr和substring的区别
    jq触发a标签的href跳转
    jq中跳出方法、for循环和each循环
    IIS应用程序池频繁崩溃的问题
  • 原文地址:https://www.cnblogs.com/veins/p/4208603.html
Copyright © 2011-2022 走看看