Android 获得手机屏幕真实的宽高
http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels
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 获得手机屏幕真实的宽高的更多相关文章
- Android之手机屏幕的获取
屏幕区域的获取: activity.getWindowManager().getDefaultDisplay(); 应用区域的获取: Rect outRect = new Rect(); activi ...
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
博客地址 : http://blog.csdn.net/shulianghan/article/details/41520569 代码下载 : -- GitHub : https://github.c ...
- js 获取屏幕或元素宽高...
窗口相对于屏幕顶部距离 window.screenTop 窗口相对于屏幕左边距离 window.screenLeft, 屏幕分辨率的高 window.screen.height, 屏幕分辨率的宽 wi ...
- js获取屏幕以及元素宽高的方法
一.window相关 网页正文部分上:window.screenTop 网页正文部分左:window.screenLeft 屏幕分辨率的高:window.screen.height 屏幕分辨率的宽:w ...
- Android获取手机屏幕宽高
//如果是获取单位是像素,可以如下: Display display = getWindowManager().getDefaultDisplay(); Point size = new Point( ...
- Android 截取手机屏幕两种实现方案解析
近期在开发的过程中,遇到了一个须要截取屏幕保存为图片的需求,详细为截取webview的视图保存图片. 方法1:首先想到的思路是利用SDK提供的View.getDrawingCache()方法: pub ...
- android获取手机屏幕分辨率
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt ...
- js里获取html真实舞台宽高
var winSize = {width:document.documentElement.clientWidth,height:document.documentElement.clientHeig ...
- android获取屏幕宽高与获取控件宽高
获取屏幕宽高 // 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素 ...
随机推荐
- 《java程序设计》结对编程-四则运算(第一周-阶段总结)
一.需求分析(描述自己对需求的理解,以及后续扩展的可能性) 实现一个命令行程序,要求: - 自动生成小学四则运算题目(加,减,乘,除) - 支持整数 - 支持多运算符(比如生成包含100个运算符的题目 ...
- SQL定义变量
- DDD模型领域WF到领域层(十五)
实现超市的结算系统: 计算相应的优惠方式的接口 public interface ICompute { double GetResultTotalMoney(double TotalMoney); } ...
- 【C++】类前置声明范例
• 在编写C++程序的时候,偶尔需要用到前置声明(Forward declaration).下面的程序中,带注释的那行就是类B的前置说明.这是必须的,因为类A中用到了类B,而类B的声明出现在类A的后面 ...
- H 模拟水题
n个人 小明编号为m 从编号为a的人开始数 起始数字为b 遇到素数duang 并反相 求小明应该说什么 Sample Input 310 2 //n m3 4 //a b3 32 64 13 8 Sa ...
- hdu 1253 3维迷宫 在规定时间内能否出迷宫 (3维BFS)
题意:有一个人要在魔王回来之前逃出城堡.1表示墙,0表示路.魔王将在T分钟后回到城堡 起点可以是墙,但是人能走出.而终点也可以是墙,那自然就走不出了,但是要判断. 剪枝:如果终点是门或者从起点到终点的 ...
- curl 命令模拟 HTTP GET/POST 请求
https://www.cnblogs.com/alfred0311/p/7988648.html
- [HEOI2016/TJOI2016]序列
题解: 很水的题目 首先容易发现每个位置实际上只有最大值是有用的 然后把条件变成dp[i]=max(dp[j]+1)(j<i,F[i]>G[j],G[i]>H[j]) 然后我研究了一 ...
- 074 hbase与mapreduce集成
一:运行给定的案例 1.获取jar包里的方法 2.运行hbase自带的mapreduce程序 lib/hbase-server-0.98.6-hadoop2.jar 3.具体运行 注意命令:mapre ...
- AndrodStudio报错: Cannot launch AVD in emulator.
Cannot launch AVD in emulator.Output:Hax is enabledHax ram_size 0x60000000HAX is working and emulato ...