Android 获得手机屏幕真实的宽高
2015-06-12 17:07
471 查看
http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels
EDIT: slightly improved version (avoid firing exceptions on non-supported OS version):
WindowManager w = activity.getWindowManager(); Display d = w.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); d.getMetrics(metrics); // since SDK_INT = 1; widthPixels = metrics.widthPixels; heightPixels = metrics.heightPixels; try { // used when 17 > SDK_INT >= 14; includes window decorations (statusbar bar/menu bar) widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d); heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d); } catch (Exception ignored) { } try { // used when SDK_INT >= 17; includes window decorations (statusbar bar/menu bar) Point realSize = new Point(); Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize); widthPixels = realSize.x; heightPixels = realSize.y; } catch (Exception ignored) { }
EDIT: slightly improved version (avoid firing exceptions on non-supported OS version):
WindowManager w = activity.getWindowManager(); Display d = w.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); d.getMetrics(metrics); // since SDK_INT = 1; widthPixels = metrics.widthPixels; heightPixels = metrics.heightPixels; // includes window decorations (statusbar bar/menu bar) if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) try { widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d); heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d); } catch (Exception ignored) { } // includes window decorations (statusbar bar/menu bar) if (Build.VERSION.SDK_INT >= 17) try { Point realSize = new Point(); Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize); widthPixels = realSize.x; heightPixels = realSize.y; } catch (Exception ignored) { }
相关文章推荐
- android XMl 解析神奇xstream 三: 把复杂对象转换成 xml
- android自定义View之(二) Custom Drawing
- Android控件使用—Viewpager做引导界面
- android,侧滑栏SlidingLayout、ViewPager实现画廊、简单用viewpager实现画廊、圆形图片、简单的跑马灯动画效果、可拖拽的弹簧式水泡动画集合
- Android中的颜色设置
- Android实用方法—activity多个fragment切换
- Android控件使用—TabHost底部导航
- android listview 解决数据重复
- Android自定义View—自定义dialog的使用
- Android自动化测试之MonkeyRunner录制和回放脚本
- android 照片拍照问题
- Android学习笔记 :Android动画 tween,frame,property
- Android中callback(接口回调)机制
- IOS和Android的区别
- Android IOS WebRTC 音视频开发总结(二九)-- 安卓噪声消除交流
- android实现点赞动画
- android 手势密码
- Android 有关文件路径的常用方法
- android 系统Style,Theme,以及自定义模板(declare-style)
- Android app进入前的闪屏界面代码