前言

  获取Android设备的宽度与高度是一个在Android开发上经常需要处理的事情。我们在相机开发、全屏对话框、个别View的屏幕适配经常需要使用到。但是获取屏幕高度与宽度也有很多方法与不同的结果。下面会一一总结这些方法一结果。

获取屏幕宽度

可用显示尺寸的绝对宽度(以像素为单位),请注意,在一些平板设备上可能存在侧边导航栏,那么这个尺寸可能会排除掉这个侧边导航栏的宽度

    private void getWidth(Context context){
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
}

获取屏幕高度

可用显示尺寸的绝对高度(以像素为单位)。请注意,在一些机型上这个尺寸可能会排除掉状态栏或者底部导航栏的高度。

    private void getHeight(Context context){
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int height = displayMetrics.heightPixels;
}

用WindowManage获取DisplayMetrics

    private void getSize(Context context){
DisplayMetrics outMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
}

获取包含导航栏与状态栏的设备的实际屏幕宽高

根据此设备屏幕的实际大小获取显示指标,尺寸会根据显示器的当前旋转进行调整。

    private void getDeviceRealHeight(Context context){
DisplayMetrics outMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getRealMetrics(outMetrics);
int deviceRealHeightPixels = outMetrics.heightPixels;
}

获取密度信息

    private void getDensity(Context context){
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float density = displayMetrics.density;//屏幕密度
float densityDpi = displayMetrics.densityDpi;//每英寸包含的密度
float scaledDensity = displayMetrics.scaledDensity;//屏幕密度比例
}

End

Android 开发 获取Android设备的屏幕高宽的更多相关文章

  1. android开发 获取父控件的高宽

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(wi ...

  2. Android开发 - 获取Android设备的唯一标识码(Android 6.0或更高)

    在我们的APP开发中,通常需要获取到设备的唯一标识.在Android6.0之前,有很多方法我们可以方便获取到硬件的唯一标识,但是在Android6.0之后,Android系统大幅限制了我们获取设备的硬 ...

  3. android开发获取网络状态,wifi,wap,2g,3g.工具类(一)

    android开发获取网络状态整理: package com.gzcivil.utils; import android.content.Context; import android.net.Con ...

  4. AllJoyn+Android开发案例-android跨设备调用方法

    AllJoyn+Android开发案例-android跨设备调用方法 项目须要涉及AllJoyn开源物联网框架.前面主要了解了一些AllJoyn主要的概念.像总线,总线附件,总线对象,总线接口这种概念 ...

  5. CSharp程序员学Android开发---3.Android内部元素不填充BUG

    最近公司组织项目组成员开发一个Android项目的Demo,之前没有人有Andoid方面的开发经验,都是开发C#的. 虽说项目要求并不是很高,但是对于没有这方面经验的人来说,第一步是最困难的. 项目历 ...

  6. Android开发之获取状态栏高度、屏幕的宽和高

    转自:http://blog.csdn.net/guolin_blog/article/details/16919859 获取状态栏的高度. private static int statusBarH ...

  7. Android 开发 获取设备信息与App信息

    设备信息 设备ID(DeviceId) 获取办法 android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager ...

  8. Android开发获取多个存储空间的路径(内置SD卡以及外置SD卡)

    Android开发中经常会遇到多存储空间的问题,包括内置存储路径以及外置SD卡,而且有的时候会有多张外置SD卡,此时就需要获取不同的SD卡路径,然后根据需要来写入或者读出文件. 此处给出常用的SD卡工 ...

  9. android开发-获取wifi列表

    近期博主在学frangment框架,因此想着想着就想通过listfragment完毕对wifi列表的获取. 好! 如今就不说废话了. 一.wifi的基础知识 在Android的官方文档中定义了例如以下 ...

随机推荐

  1. mysql_索引

    .默认情况下大多使用Btree索引,该索引就是通常所见 唯一索引.聚簇索引等等,Btree用在OLTP,加快查询速度 查询表索引 show  index  from  tablename 查询表结构 ...

  2. myEclipse出现cannot paste the clipboard contents into the selected elements报错

    导入jar包报错,cannot paste the clipboard contents into the selected elements,查阅资料让重新打开工程,但依然报错. 最后在本地路径复制 ...

  3. Linux 的基本命令

    Linux 的基本命令 1. 文件操作 a) Windows 是多根的文件系统,物理上是 1 到多块硬盘,逻辑上分为 C.D.E……盘, 每个盘都是一棵树.Linux 是单根的文件系统,不分 CDE ...

  4. introduce of servlet and filter

    servlet简介: Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中 ...

  5. Visual Studio Code create the aps.net core project(Visual Studio Code 创建asp.net core项目)

    Install the C# plug-in as shown below: Perfom the dotnet new --help command as shown below: Enter a ...

  6. dbca建库--linux上使用vnc图形化安装oracle10g版本

    选择创建数据库 配不配置em,自己决定,我们选择配置 配置下面账户密码,在项目中,下面账户肯定是不相同的,我们这里输入相同的,密码为oracle10g 选择文件系统存放(asm和字符设备暂时不能存放) ...

  7. 开发系统app所遇到的问题及解决

    1. 在源码环境中编译app时(使用mmm编译需要根据app写好Android.mk文件)遇到如下问题 error: Resource at colorPrimary appears in overl ...

  8. 截止2017年,最新的全国行政区划代码数据源(xml)

    <?xml version="1.0" encoding="utf-8"?> <Root xmlns:xsd="http://www ...

  9. Assets Library开发总结

    Assets Library beta版的开发工作告一段落,本着有始有终的原则,这个项目还是需要做个总结的,恩~ 先甩一个链接:https://vimeo.com/238186671 考虑到该项目开发 ...

  10. [例子]Ubuntu虚拟机设置固定IP上网

    宿主机器     win7 linux            Ubuntu 14.04 LTS 参考: Linux系列:Ubuntu虚拟机设置固定IP上网(配置IP.网关.DNS.防止resolv.c ...