Android获取手机屏幕、状态栏、Activity显示区域的宽高的获取
来源:互联网 发布:app拍照软件排行 编辑:程序博客网 时间:2024/05/16 19:18
1、手机屏幕宽高的获取
outRect.top即是状态栏高度
1.1DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); int width = metric.widthPixels; // 屏幕宽度(像素) int height = metric.heightPixels; // 屏幕高度(像素)1.2DisplayMetrics metrics = getResources().getDisplayMetrics(); int width=metrics.widthPixels; int height=metrics.heightPixels;1.3Display defaultDisplay = getWindowManager().getDefaultDisplay(); defaultDisplay.getWidth(); defaultDisplay.getHeight();
2、状态栏宽高的获取
Rect outRect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
outRect.top即是状态栏高度
3、应用区域(Activity)的宽高
Rect outRect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);w=outRect.rirht-outRect.left
h=outRect.bottom-outRect.top
ps:如果像妹子手机有smartBar、那么smartBar的高度=屏幕的高度-outRect.bottom(?guess)
注:在onCreate生命周期中有些尺寸或是无效的是无效的最好是在onWindowFocusChanged中获取
@SuppressLint("NewApi")private void showMetres() {int screenW,screenH;int statusBarH;int contentH;int smartBarH; //获取屏幕的宽高 DisplayMetrics outMetrics=new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(outMetrics);screenW=outMetrics.widthPixels;screenH=outMetrics.heightPixels;//获取状态栏的高Rect outRect=new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);statusBarH=outRect.top;//内容显示区的高contentH=outRect.bottom-outRect.top;//smartBarH=screenH-outRect.bottom;Rect rect=new Rect();getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);int i=rect.bottom-rect.top;smartBarH=screenH-rect.bottom;//这个可能不正确……!!!!StringBuilder sb=new StringBuilder("屏幕的宽为:"+screenW+"\n").append("屏幕的高为:"+screenH+"\n").append("状态栏的高为:"+statusBarH+"\n").append("标题栏的高为:"+getActionBar().getHeight()+"\n")//标题栏的高.append("内容显示区的高为(包含SmartBar):"+contentH+"\n").append("内容显示区的高为(不包含SmartBar):"+i+"\n").append("smartBar的高为:"+smartBarH);TextView tv = (TextView) findViewById(R.id.tv);tv.setText(sb.toString());}
0 0
- Android获取手机屏幕、状态栏、Activity显示区域的宽高的获取
- Android获取手机屏幕宽高/屏幕状态栏的高度
- Android在Activity中获取手机屏幕的宽高
- Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法
- Android 获取手机屏幕的宽高
- android获取手机屏幕宽高的总结
- Android获取手机屏幕的宽高
- Android获取手机屏幕的宽高
- 获取手机屏幕的宽高
- 获取手机屏幕的宽高
- 获取手机屏幕的宽高
- android获取手机屏幕的宽和高
- Android:获取手机屏幕宽高
- Android——获取手机屏幕的宽高
- 获取手机屏幕宽高
- 获取手机屏幕宽高
- Android之获取控件的宽高和手机屏幕的宽高
- Android之---获取手机屏幕、应用的宽和高
- 第8章 私服nexus
- 【Android】问题记录2015-11-11 13:50:16
- MyEclipse安装插件
- 第9章 maven的插件和生命周期
- 使用MySql自带的库进行数据库连接时可能产生的一些问题
- Android获取手机屏幕、状态栏、Activity显示区域的宽高的获取
- 第10章 多环境下构建项目
- TR111 and TR069 function development note (1)
- maven使用指南
- 【SDCC讲师专访】腾讯潘安群:腾讯云金融级数据库TDSQL分析
- 基于opencv的相机之鬼怪贴图功能实现(十一)
- JAVA中File类遍历目录解析
- 使用 getprop 命令查看 android 系统允许进程分配的内存大小
- C# Socket简单例子(服务器与客户端通信)