Android 获取屏幕宽高的正确姿势
前言
在开发时,我们经常需要根据屏幕的宽高来进行对view的适配,无论是自定义view还是andorid自带的一些控件,比如说需要占当前屏幕高度的30%,就需要获取到屏幕的宽高,但在获取宽高时我遇到了一些坑,总结如下
获取高度
下面两种方法都是安卓自带方法可以获取到屏幕宽高的
但是!
这两种方法获取的高度都是省略了手机最上方系统状态栏的高度
系统方法
int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;//屏幕高度
int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;//屏幕宽度
WindowManager
/*** 屏幕高度* @return the height of screen, in pixel*/
public static int getScreenHeight(Context context) {WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Point point = new Point();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {//noinspection ConstantConditionswm.getDefaultDisplay().getRealSize(point);} else {//noinspection ConstantConditionswm.getDefaultDisplay().getSize(point);}return point.y;
}/*** 屏幕宽度* @return the width of screen, in pixel*/
public static int getScreenWidth(Context context) {WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Point point = new Point();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {//noinspection ConstantConditionswm.getDefaultDisplay().getRealSize(point);} else {//noinspection ConstantConditionswm.getDefaultDisplay().getSize(point);}return point.x;
}
根据屏幕判断,获取完整的屏幕宽高
这种方法可以获取到完整的屏幕高度
/**** 获取屏幕的高度,全面屏和非全面屏* @param context* @return*/
public static int getFullActivityHeight(@Nullable Context context) {if (!isAllScreenDevice()) {return getScreenHeight(context);}return getScreenRealHeight(context);
}private static final int PORTRAIT = 0;
private static final int LANDSCAPE = 1;
private volatile static boolean mHasCheckAllScreen;
private volatile static boolean mIsAllScreenDevice;@NonNull
private volatile static Point[] mRealSizes = new Point[2];public static int getScreenRealHeight(@Nullable Context context) {if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {return getScreenHeight(context);}int orientation = context != null? context.getResources().getConfiguration().orientation: getApplicationContext().getResources().getConfiguration().orientation;orientation = orientation == Configuration.ORIENTATION_PORTRAIT ? PORTRAIT : LANDSCAPE;if (mRealSizes[orientation] == null) {WindowManager windowManager = context != null? (WindowManager) context.getSystemService(Context.WINDOW_SERVICE): (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);if (windowManager == null) {return getScreenHeight(context);}Display display = windowManager.getDefaultDisplay();Point point = new Point();display.getRealSize(point);mRealSizes[orientation] = point;}return mRealSizes[orientation].y;
}public static int getScreenHeight(@Nullable Context context) {if (context != null) {return context.getResources().getDisplayMetrics().heightPixels;}return 0;
}/**** 获取当前手机是否是全面屏* @return*/public static boolean isAllScreenDevice() {if (mHasCheckAllScreen) {return mIsAllScreenDevice;}mHasCheckAllScreen = true;mIsAllScreenDevice = false;// API小于21时,没有全面屏if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {return false;}WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);if (windowManager != null) {Display display = windowManager.getDefaultDisplay();Point point = new Point();display.getRealSize(point);float width, height;if (point.x < point.y) {width = point.x;height = point.y;} else {width = point.y;height = point.x;}if (height / width >= 1.97f) {mIsAllScreenDevice = true;}}return mIsAllScreenDevice;
}
参考博客:Android怎样正确的获取屏幕的高度 - 简书
Android 获取屏幕宽高的正确姿势相关推荐
- Android获取屏幕宽高,状态栏宽高,actionbar宽高,layout宽高,导航栏高度的方法汇总
看这个博客你可以知道 获取屏幕宽高,状态栏宽高,actionbar宽高,layout宽高,导航栏(虚拟按键栏)高度的方法 目录顺序为 代码测试的机型 状态栏高度 actionbar高度 屏幕高度 导航 ...
- android获取屏幕宽高与获取控件宽高
1.获取屏幕宽高 方法1: int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px ...
- android获取该控件在屏幕,android获取屏幕宽高与获取控件宽高(三种方法)
1.获取屏幕宽高 方法1: int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px ...
- 关于Android获取屏幕宽高、dp、sp、px之间的转化
开发过程中,动态创建布局,或者自定义view,少不了需要获取屏幕宽高,这里的宽高指手机屏幕的分辨率,单位是px,而我们在布局文件中用到的空间宽高单位是dp,字体用的是sp. 这几个计量单位之间,是有关 ...
- 获取屏幕的宽高 android,Android获取屏幕宽高的方法
1. 实现代码 private intmWidth;private intmHeight; @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)public v ...
- android利用反射调用截屏api,Android利用反射机制调用截屏方法和获取屏幕宽高的方法...
想要在应用中进行截屏,可以直接调用 View 的 getDrawingCache 方法,但是这个方法截图的话是没有状态栏的,想要整屏截图就要自己来实现了. 还有一个方法可以调用系统隐藏的 screen ...
- Flutter使用ScreenUtil获取屏幕宽高初始化报错
报错如下 原因 我们在布局中使用ScreenUtil().screenWidth获取屏幕宽度是,在初始化未装载视图时,第一次获取时拿不到,会出现如上的崩溃. 解决方案 import 'package: ...
- Android获取View宽高的常见方式
背景 有时我们会有基于这样的需求,当Activity创建时,调用View.getWidth.View.getHeight().View.getMeasuredWidth() .View.getgetM ...
- uniapp微信小程序获取屏幕宽高
uniapp开发微信小程序的时候,有时候去调整样式 你需要适配各种手机屏幕,使用,你的样式宽高就不能使用rpx 有的朋友觉得可以使用vw vh % 是的,当然可以 但是要让你的元素,宽高,比如 ...
最新文章
- layui关闭表格编辑_告别复制粘贴,表格再多也能快速合并!
- 使用 Debian 从 0 开始搭建 hexo 博客
- [Python图像处理] 八.图像腐蚀与图像膨胀
- CodeForces - 1285E Delete a Segmen(线段树+区间合并+离散化)
- 北大计算机最好的班叫什么,中国大学计算机最好的班,再次迎来“图灵奖”导师,赶超“姚班”...
- visa虚拟卡生成器_英国虚拟卡 获取多张VISA和Mastercard
- Navicat Premium 15安装需要注意的几个细节
- python制作模型排放清单_四川省人为源大气污染物排放清单及特征
- 【ML】使用支持向量回归器进行时间序列预测
- 神器-数学公式识别工具-mathpix
- 寒冬不怕!印度互联网市场潜力巨大
- 微位科技李子阳:哈耶克—未来的价值单位
- 三种常用的数字数据编码方式
- 计算机科学与技术专业大学排名及录取分数,计算机科学与技术专业分数线各大学排名(湖南)...
- Bootstrap DataTable自定义表格 设置某列不排序
- 走近秦岭深山小镇--四亩地镇
- 软件测试技术同学如何面对裁员浪潮?
- 跨境电商注册义乌个体户结汇怎么开户
- android亮屏、暗屏、解锁、关闭系统对话的监听事件
- 优思学院|PDCA与PDSA到底有何分别?
热门文章
- 调用接口返回一句情话
- exit函数和析构函数的关系
- 关于开发定位导航软件中间件的思考
- python scatter legend_Python可视化|matplotlib10-绘制散点图scatter
- css中单行文本分散对齐
- 我应该选一所什么学校--所有选择,都应基于你的需要
- cesium 文本标注被遮挡_Cesium中Primitive讲解
- 用一张图说一说 ChatGPT 内部技术工作流程
- 曾经的经典玄幻小说(二)
- Word控件Spire.Doc 【Table】教程(7): 如何在C#中用表格替换word文档中的文本