android中获取屏幕的长于宽,参考了网上有很多代码,但结果与实际不符,如我的手机是i9000,屏幕大小是480*800px,得到的结果却为320*533
结果很不靠谱,于是自己写了几行代码,亲测一下
测试参数:
测试环境: i9000(三星)
物理屏幕:480*800px
density :1.5
测试代码:
-
-
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
-
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
-
-
Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
-
-
-
-
DisplayMetrics dm = new DisplayMetrics();
-
dm = getResources().getDisplayMetrics();
-
-
float density = dm.density;
-
int densityDPI = dm.densityDpi;
-
float xdpi = dm.xdpi;
-
float ydpi = dm.ydpi;
-
-
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
-
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
-
-
screenWidth = dm.widthPixels;
-
screenHeight = dm.heightPixels;
-
-
Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
-
-
-
-
-
dm = new DisplayMetrics();
-
getWindowManager().getDefaultDisplay().getMetrics(dm);
-
-
density = dm.density;
-
densityDPI = dm.densityDpi;
-
xdpi = dm.xdpi;
-
ydpi = dm.ydpi;
-
-
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
-
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
-
-
int screenWidthDip = dm.widthPixels;
-
int screenHeightDip = dm.heightPixels;
-
-
Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);
-
-
screenWidth = (int)(dm.widthPixels * density + 0.5f);
-
screenHeight = (int)(dm.heightPixels * density + 0.5f);
-
-
Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
结果如下:
-
E/== MyScreenActivity =================================== getDefaultDisplay( 8509): screenWidth=320; screenHeight=533
-
E/== MyScreenActivity =================================== DisplayMetrics( 8509): xdpi=156.3077; ydpi=157.51938
-
E/== MyScreenActivity =================================== DisplayMetrics( 8509): density=1.0; densityDPI=160
-
E/== MyScreenActivity =================================== DisplayMetrics(111)( 8509): screenWidth=320; screenHeight=533
-
E/== MyScreenActivity =================================== DisplayMetrics( 8509): xdpi=234.46153; ydpi=236.27907
-
E/== MyScreenActivity =================================== DisplayMetrics( 8509): density=1.5; densityDPI=240
-
E/== MyScreenActivity =================================== DisplayMetrics(222)( 8509): screenWidthDip=320; screenHeightDip=533
-
E/== MyScreenActivity =================================== DisplayMetrics(222)( 8509): screenWidth=480; screenHeight=800
分析结果:
在onDraw()方法中
方法1和2,得到的结果都一致,均为320*533,明显不是测试机i9000的屏幕大小
方法3,将方法1和2得到的结果,乘以density后,完美的480*800,perfect!
注: density 大于1的情况下,需要设置targetSdkVersion在4-9之间,例如
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="10" />
但是,这就说明方法3一定是通用的吗?
回答是否定的,因为我也在模拟器、HTC G14物理机,以及ViewSonic、Galaxy平板上测试过,方法3在density=1.5时,放大了实际屏幕值,例如:HTC G14
在HTC G14上,实际屏幕大小,直接通过dm.widthPixels、dm.heightPixels便得到了实际物理屏幕大小(540,960)
导致无法通过一种通用的方法获取真实物理屏幕大小的原因,可能就是因为Android系统开源,不同的手机生产厂商没有统一的制造标准,来规定手机屏幕。
仔细分析代码,发现问题出在代码:
getWindowManager().getDefaultDisplay().getMetrics(dm)
Initialize a DisplayMetrics object from this display‘s data.
dm = getResources().getDisplayMetrics()
Return the current display metrics that are in effect for this resource object. The returned object should be treated as read-only.
Android 获取屏幕尺寸与密度,布布扣,bubuko.com
Android 获取屏幕尺寸与密度
原文:http://blog.csdn.net/t80t90s/article/details/25491393