Android 获取屏幕高度,宽度,状态栏高度
背景介绍:
到目前为止,android已经从1.5发展到目前的3.2,我们在写一个应用的时候,最常用到得就是获取屏幕高度,宽度,以及status bar的高度。
然而android系统变化太快了,从开始的手机操作系统到目前的3.2 平板电脑系统,在获取这些数据的时候也发生了很大的变化。
值得我们重视,否则会有很多错误发生。
问题分析及解决方案:
1. android 1.6 到 android 2.x
这是android手机操作系统,从1.6到2.x都有着统一的获取方法。
直接利用android api即可获取相关的尺寸:
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth();//屏幕宽度 int height = wm.getDefaultDisplay().getHeight();//屏幕高度 Rect rect= new Rect(); this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; //状态栏高度
2. android 3.0 平板系统
在3.0系统中,status bar在屏幕下方,因为计算方法也发生改变。
在3.0系统中获取屏幕高度和宽度的方法没有改变。
状态的获取方法如下:
this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = window.getWindowManager().getDefaultDisplay().getHeight() - rect.bottom;
即利用屏幕高度减去显示区域的最大高度即为下方status bar的高度
3. android 3.2平板系统
在android 3.2中就有了很大的改变,当我们调用getWidth()和getheight()获取宽度和高度的时候,不会返回屏幕的真实尺寸,
而是只返回屏幕的显示区域的尺寸,即减去了状态栏的高度。
运用这两个api函数读取的尺寸肯定不是我们想要的结果。
这时我们发现其提供了两个隐藏函数getRealHeight()和getRealWidth()用来获取真实的屏幕尺寸。
一因为是隐藏函数,所以我们只能通过反射来调用这两个函数,但这样带来不好的就是反射效率实在是太差了。
Display display = wm.getDefaultDisplay();
Class c = Class.forName("android.view.Display"); Method method = c.getMethod("getRealHeight");
int height = (Integer) method.invoke(display); Rect rect= new Rect(); this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); statusbarHeight = height - rect.bottom;
对上述代码进行优化,如果当我们频繁调用的时候,必将影响程序性能。
我们可以保存第一次反射的相关信息,然后在后面直接调用。
private Method method = null;// 用来保存method对象 --------------------------------------------------------------------------------- Display display = wm.getDefaultDisplay(); //判断method是否为空,如果为null,则利用反射得到method信息,否则,利用旧的method对象。
if(method == null)
{ method = display.getClass().getMethod("getRealHeight"); //这里直接用display的class信息 } int height = (Integer) method.invoke(display); Rect rect= new Rect(); this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
经试验Android4.0以上平台也依然可以用最上面的方法获取状态栏高度
Android 获取屏幕高度,宽度,状态栏高度的更多相关文章
- Android获取屏幕高度、标题高度、状态栏高度详解
Android获取屏幕高度的方法主要由view提供 通过View提供的方法获取高度方式有两种: 1, 当前显示的view中直接获取当前view高宽2,通过Activity的getWindow().fi ...
- 获取Android 手机屏幕宽度和高度以及获取Android手机序列号
1.获取Android 手机屏幕宽度 1 DisplayMetrics dm = new DisplayMetrics(); 2 this.getWindowManager().getDefaultD ...
- js获取屏幕高度宽度
获取各种屏幕的宽度和高度Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽 ...
- android 获取屏幕高度和宽度 的方法
我们需要获取Android手机或Pad的屏幕的物理尺寸,以便于界面的设计或是其他功能的实现.下面就介绍讲一讲如何获取屏幕的物理尺寸 下面的代码即可获取屏幕的尺寸. 在一个Activity的onC ...
- android: 获取屏幕高度和虚拟导航栏高度的几种方法
package com.yongdaimi.android.androidapitest; import android.app.Activity; import android.content.Co ...
- Android开发之获取状态栏高度、屏幕的宽和高
转自:http://blog.csdn.net/guolin_blog/article/details/16919859 获取状态栏的高度. private static int statusBarH ...
- android 获得屏幕宽度和高度
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- js和jQuery 获取屏幕高度、宽度
js获取屏幕高度,宽带 网页可见区域宽:document.body.clientWidth网页可见区域高:document.body.clientHeight网页可见区域宽:document.body ...
- android获取自己定义控件位置坐标,屏幕尺寸,标题栏,状态栏高度
android获取自己定义控件位置坐标,屏幕尺寸,标题栏,状态栏高度 1.获取自己定义控件height 在本Activity中获取当前Activity中控件的height: Button button ...
随机推荐
- 关于 Google Chrome 中的全屏模式和 APP 模式
前言:我一直在纠结这篇文章是否应该归类在「前段开发」的范围内,哈哈! 前段时间做了一个项目,涉及到一个要全屏模式去访问网页的需求,因为 Google Chrome 的效率不错,而且专门为 Chrome ...
- messager(消息窗口)
一.$.messager.alert()类似js中的alert('String') 方法参数:title, msg, icon, function(回调函数) 描述:title头部面板标题.msg主要 ...
- JavaScript 类型转换
在 JavaScript 中有 5 中不同的数据类型: string number boolean object function 3 种对象类型: Object Date Array 2 个不包含任 ...
- openfire插件开发之完美开发
http://redhacker.iteye.com/blog/1919329 一.说在前面 在继上篇Openfire3.8.2在eclipse中Debug方式启动最简单的方式后,我研究了openfi ...
- 查看python selenium的api
打开命令行工具,doc中输入: python -m pydoc -p 然后在浏览器中访问http://localhost:4567/,此时应该可以看到python中所有的Modules 按ctrl+f ...
- 【Python】一个简单的例子
问题描述: Python基础篇 参考资料: (1)http://www.cnblogs.com/octobershiner/archive/2012/12/04 ...
- 【bzoj1009】[HNOI2008]GT考试
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3018 Solved: 1856[Submit][Statu ...
- iOS Automation Test
google resource for KIF: http://www.oschina.net/translate/ios-ui-testing-with-kif
- 您可能不知道的ASP.Net小技巧
<!-- 页码和简介 --> 1. 在提交页面之后,保持滚动条的位置 可以在page指令上加上MaintainScrollPositionOnPostback指令 <%@ Page ...
- (转载)李剑英的CSLight入门指南结合NGUI热更新
原地址:http://www.xuanyusong.com/archives/3075 李剑英的CSLight入门指南文档撰写者:GraphicQQ: 1065147807 一. CSLIGHT 作者 ...