获取屏幕的宽高 android,Android获取屏幕宽高的方法

1. 实现代码

private intmWidth;private intmHeight;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)public voidgetDisplayMetrics() {

WindowManager wm=(WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

Display display=wm.getDefaultDisplay();int API_LEVEL =android.os.Build.VERSION.SDK_INT;//方法1

DisplayMetrics displayMetrics = newDisplayMetrics();if (API_LEVEL >= 17) {

display.getRealMetrics(displayMetrics);

}else{

display.getMetrics(displayMetrics);

}

mWidth=displayMetrics.widthPixels;

mHeight=displayMetrics.heightPixels;//方法2

Point outSize = newPoint();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:

The real display area specifies the part of the display that contains contentincluding 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:

3. Note

public void getRealMetrics

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)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值