标签:屏幕物理尺寸 分辨率 android 屏幕密度 dip
大家帮忙喽!
博主参加2014博客之星活动,大家帮忙投票啦!猛击这里!
通过程序去了解硬件情况是一件十分有意思的事情。很早我就研究在WM6.5上获得屏幕物理尺寸,但一直没有成功。后来又想要在Android上有所突破,不过在今天之前得到的尺寸都不准确。虽然很多人认为没必要这么较真,因为貌似很多情况下用不到。不过我就当这是一件很有挑战性的事,一定要做到。对,就是这么任性。
源码中android.view包下的Display类提供了很多方法供程序员获得显示相关的信息,通过此类让我们开启了解设备屏幕之旅吧。
public void getSize(Point outSize) { synchronized (this) { updateDisplayInfoLocked(); mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments); outSize.x = mTempMetrics.widthPixels; outSize.y = mTempMetrics.heightPixels; } }参数是一个返回参数,用以返回分辨率的Point,这个Point也比较简单,我们只需要关注x和y这两个成员就可以了。
private void getDisplayInfomation() { Point point = new Point(); getWindowManager().getDefaultDisplay().getSize(point); Log.d(TAG,"the screen size is "+point.toString()); }结果如下:
D/MainActivity﹕ the screen size is Point(800, 1280)
public void getRealSize(Point outSize) { synchronized (this) { updateDisplayInfoLocked(); outSize.x = mDisplayInfo.logicalWidth; outSize.y = mDisplayInfo.logicalHeight; } }
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"结果还是相同的。
private void getDisplayInfomation() { Point point = new Point(); getWindowManager().getDefaultDisplay().getSize(point); Log.d(TAG,"the screen size is "+point.toString()); getWindowManager().getDefaultDisplay().getRealSize(point); Log.d(TAG,"the screen real size is "+point.toString()); }
D/MainActivity﹕ the screen size is Point(800, 1202) D/MainActivity﹕ the screen real size is Point(800, 1280)
DENSITY_LOW = 120 DENSITY_MEDIUM = 160 //默认值 DENSITY_TV = 213 //TV专用 DENSITY_HIGH = 240 DENSITY_XHIGH = 320 DENSITY_400 = 400 DENSITY_XXHIGH = 480 DENSITY_XXXHIGH = 640
private void getDensity() { DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); Log.d(TAG,"Density is "+displayMetrics.density+" densityDpi is "+displayMetrics.densityDpi+" height: "+displayMetrics.heightPixels+ " width: "+displayMetrics.widthPixels); }
the screen size is Point(1600, 2438) the screen real size is Point(1600, 2560) Density is 2.0 densityDpi is 320 height: 2438 width: 1600
private void getScreenSizeOfDevice() { DisplayMetrics dm = getResources().getDisplayMetrics(); int width=dm.widthPixels; int height=dm.heightPixels; double x = Math.pow(width,2); double y = Math.pow(height,2); double diagonal = Math.sqrt(x+y); int dens=dm.densityDpi; double screenInches = diagonal/(double)dens; Log.d(TAG,"The screenInches "+screenInches); }
Log如下:
01-13 16:35:03.026 16601-16601/com.linc.listviewanimation D/MainActivity﹕ the screen size is Point(1600, 2438) 01-13 16:35:03.026 16601-16601/com.linc.listviewanimation D/MainActivity﹕ the screen real size is Point(1600, 2560) 01-13 16:35:03.026 16601-16601/com.linc.listviewanimation D/MainActivity﹕ Density is 2.0 densityDpi is 320 height: 2438 width: 1600 xdpi 338.666 ydpi 338.666 01-13 16:35:03.026 16601-16601/com.linc.listviewanimation D/MainActivity﹕ The screenInches 9.112922229586951如Log所见,使用heightPixels得出的值是2483而不是正确的2560.从而使结果9.11反倒跟真实屏幕尺寸很接近。下面用正确的height再算一遍。
01-13 16:39:05.476 17249-17249/com.linc.listviewanimation D/MainActivity﹕ the screen size is Point(1600, 2560) 01-13 16:39:05.476 17249-17249/com.linc.listviewanimation D/MainActivity﹕ the screen real size is Point(1600, 2560) 01-13 16:39:05.476 17249-17249/com.linc.listviewanimation D/MainActivity﹕ Density is 2.0 densityDpi is 320 height: 2560 width: 1600 xdpi 338.666 ydpi 338.666 01-13 16:39:05.476 17249-17249/com.linc.listviewanimation D/MainActivity﹕ The screenInches 9.433981132056605结果是9.43英寸,而真实值是8.91.如果再换一个设备,那么值差的更多。说明上面的计算是错误的。
//The exact physical pixels per inch of the screen in the X/Y dimension.屏幕X/Y轴上真正的物理PPI。
private void getScreenSizeOfDevice2() { Point point = new Point(); getWindowManager().getDefaultDisplay().getRealSize(point); DisplayMetrics dm = getResources().getDisplayMetrics(); double x = Math.pow(point.x/ dm.xdpi, 2); double y = Math.pow(point.y / dm.ydpi, 2); double screenInches = Math.sqrt(x + y); Log.d(TAG, "Screen inches : " + screenInches); }Log is as follows:
01-13 16:58:50.142 17249-17249/com.linc.listviewanimation D/MainActivity﹕ Screen inches : 8.914015757534717
//pixel = dip*density; private int convertDpToPixel(int dp) { DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); return (int)(dp*displayMetrics.density); } private int convertPixelToDp(int pixel) { DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics(); return (int)(pixel/displayMetrics.density); }
参考:
http://stackoverflow.com/questions/19155559/how-to-get-android-device-screen-size
标签:屏幕物理尺寸 分辨率 android 屏幕密度 dip
原文地址:http://blog.csdn.net/lincyang/article/details/42679589