DisplayMetrics dm = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; density = dm.density; densityDpi = dm.densityDpi; scaledDensity = dm.scaledDensity; DisplayMetrics metrics = dm; Log.i("haier_home", "metrics::density=" + metrics.density +", densityDpi="+metrics.densityDpi +", heightPixels="+metrics.heightPixels +", widthPixels="+metrics.widthPixels +", scaledDensity="+metrics.scaledDensity +", xdpi="+metrics.xdpi +", ydpi="+metrics.ydpi);
运行程序打印出log日志
10-20 21:42:36.735: INFO/haier_home(7610): metrics::density=1.5, densityDpi=240, heightPixels=854, widthPixels=480, scaledDensity=1.5, xdpi=239.05882, ydpi=243.72585
metrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(metrics); density = metrics.density; Log.i("smart_home", "metrics::density=" + metrics.density +", densityDpi="+metrics.densityDpi +", heightPixels="+metrics.heightPixels +", widthPixels="+metrics.widthPixels +", scaledDensity="+metrics.scaledDensity +", xdpi="+metrics.xdpi +", ydpi="+metrics.ydpi);
运行程序打印出log日志
10-20 21:50:48.555: INFO/smart_home(8110): metrics::density=1.5, densityDpi=240, heightPixels=569, widthPixels=320, scaledDensity=1.5, xdpi=239.05882, ydpi=243.72585
在AndroidManifest.xml文件中添加元素support-screens
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" />
运行程序打印出log日志
10-20 22:08:50.015: INFO/SmartHomeActivity(8948): metrics::density=1.5, densityDpi=240, heightPixels=854, widthPixels=480, scaledDensity=1.5, xdpi=239.05882, ydpi=243.72585
将近3天的时间没有搞清楚相同的手机为什么DisplayMetrics对象中的heightPixels和widthPixels不同。