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

时间: 2024-10-08 04:49:06

Android获取屏幕宽高的方法的相关文章

android 获取屏幕宽高最新方法

DisplayMetrics dm;  dm = new DisplayMetrics();  WindowManager wm = getContext().getWindowManager();  wm.getDefaultDisplay().getMetrics(dm);  int w = dm.widthPixels;  int h = dm.heightPixels;

android获取屏幕宽高与获取控件宽高

获取屏幕宽高 // 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px) int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p) Log.e(TAG + " getDefaultDisplay", "screen

android获取屏幕宽高工具类

import java.lang.reflect.Field; import android.app.Activity; import android.content.Context; import android.graphics.Point; import android.util.DisplayMetrics; import android.view.Display; import android.view.Window; import android.view.WindowManager

android 获取屏幕宽高 和 获取控件坐标

一.获取屏幕宽高: (1). WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); (2). WindowManager wm = this.getWindowManager(); int width = wm

Android 获取屏幕宽高

WindowManager m = getActivity().getWindowManager(); DisplayMetrics metric = new DisplayMetrics(); m.getDefaultDisplay().getMetrics(metric); int width = metric.widthPixels; // 屏幕宽度(像素) int height = metric.heightPixels; // 屏幕高度(像素) float density = metr

Android 获取屏幕宽高值

时间:2016年3月7日16:14:52 note:单位为像素. 三个方法中都是根据Display来进行测量. //方法一: WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); System.out

android获取view宽高的几种方法

在onCreate方法中我们通过mView.getWidth()和mView.getHeight()获取到的view的宽高都是0,那么下面几种方法就可以在onCreate方法中获取到view的宽高. 1. int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED

JS快速获取图片宽高的方法

快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器.一.简陋的获取图片方式 // 图片地址 后面加时间戳是为了避免缓存 var img_url = 'http://www.qttc.net/static/upload/2013/13643608813441.jpg?'+Date.parse(new Date()); // 创建对象 var img = new I

转载:JS快速获取图片宽高的方法

快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器. 我们一步一步进入这个过程. 一.简陋的获取图片方式 1 2 3 4 5 6 7 8 9 10 11 // 图片地址 后面加时间戳是为了避免缓存 var img_url = 'http://www.qttc.net/static/upload/2013/13643608813441.jpg?'+Date.par