Android 获取屏幕尺寸实例代码

时间:2021-05-21

Android 获取屏幕尺寸实例代码

实现代码:

/** * <supports-screens * android:smallScreens="true" * android:normalScreens="true" * android:largeScreens="true" * android:resizeable="true" * android:anyDensity="true" /> * <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" /> * 使Android程序支持了多种分辨率 */ int mWidthScreen,mHeightScreen; Display display = getWindowManager().getDefaultDisplay(); mWidthScreen = display.getWidth(); mHeightScreen = display.getHeight(); LogUtils.e("Width:" + display.getWidth()); LogUtils.e("Height:" + display.getHeight()); WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); mWidthScreen = outMetrics.widthPixels; mHeightScreen = outMetrics.heightPixels; LogUtils.e("Width:" + outMetrics.widthPixels); LogUtils.e("Height:" + outMetrics.heightPixels); LogUtils.e("xdpi:" + outMetrics.xdpi); LogUtils.e("ydpi:" + outMetrics.ydpi); LogUtils.e("densityDpi:" + outMetrics.densityDpi); LogUtils.e("density:" + outMetrics.density); LogUtils.e("scaledDensity:" + outMetrics.scaledDensity); LogUtils.e("spWidth:" + (outMetrics.widthPixels / outMetrics.scaledDensity + 0.5f)); LogUtils.e("spHeight:" + (outMetrics.heightPixels / outMetrics.scaledDensity + 0.5f)); LogUtils.e("dpWidth:" + (outMetrics.widthPixels / outMetrics.density + 0.5f)); LogUtils.e("dpHeight:" + (outMetrics.heightPixels / outMetrics.density + 0.5f)); DisplayMetrics resMetrics = getResources().getDisplayMetrics(); mHeightScreen = resMetrics.heightPixels; mWidthScreen = resMetrics.widthPixels; LogUtils.e("Width:" + resMetrics.widthPixels); LogUtils.e("Height:" + resMetrics.heightPixels); LogUtils.e("xdpi:" + resMetrics.xdpi); LogUtils.e("ydpi:" + resMetrics.ydpi); LogUtils.e("densityDpi:" + resMetrics.densityDpi); LogUtils.e("density:" + resMetrics.density); LogUtils.e("scaledDensity:" + resMetrics.scaledDensity); LogUtils.e("spWidth:" + (int)(resMetrics.widthPixels / resMetrics.scaledDensity + 0.5f)); LogUtils.e("spHeight:" + (int)(resMetrics.heightPixels / resMetrics.scaledDensity + 0.5f)); LogUtils.e("dpWidth:" + (resMetrics.widthPixels / resMetrics.density + 0.5f)); LogUtils.e("dpHeight:" + (resMetrics.heightPixels / resMetrics.density + 0.5f));

屏幕适配比

px dpi scale dip480x850 240 1.5 320x569540x960 240 1.5 360x640640x960 320 2.0 320x480600x1024 240 1.5 400x683720x1280 240 1.5 480x853720x1280 320 2.0 360x640768x1280 320 2.0 384x640800x1280 213 1.331 601x962800x1280 320 2.0 400x6401080x1920 420 2.625 411x7311080x1920 480 3.0 360x6401440x2560 560 3.5 411x731

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章