标签:
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. 说明
Provides information about the size and density of a logical display.
The display area is described in two different ways.
getSize(Point)
, getRectSize(Rect)
and getMetrics(DisplayMetrics)
.getRealSize(Point)
, getRealMetrics(DisplayMetrics)
.
3. Note
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).
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).
标签:
原文地址:http://www.cnblogs.com/veins/p/4208603.html