博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android获得屏幕高度和宽度(display中getSize(Point)方法使用)
阅读量:5303 次
发布时间:2019-06-14

本文共 2087 字,大约阅读时间需要 6 分钟。

方法一:

public static int SCREEN_WIDTH;

public static int SCREEN_HEIGHT;

//获取屏幕

WindowManager wm = (WindowManager) getBaseContext().getSystemService(
Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
SCREEN_WIDTH= size.x;
SCREEN_HEIGHT= size.y;

 

display中getSize(Point)方法使用:

Display display = getWindowManager().getDefaultDisplay();

int width = display.getWidth(); // deprecated

int height = display.getHeight(); // deprecated          

查看API 发现:

int getWidth()、int getHeight()方法:(我用的是level 19)

This method was deprecated in API level 13. Use  instead. 

在看一下getSize(Point)方法:

         public void getSize ( outSize)

 

Added in 

Gets the size of the display, in pixels.

Note that this value should not be used for computing layouts, since a device will typically have screen decoration (such as a status bar) along the edges of the display that reduce the amount of application space available from the size returned here. Layouts should instead use the window size.

The size is adjusted based on the current rotation of the display.

The size returned by this method does not necessarily represent the actual raw size (native resolution) of the display. The returned size may be adjusted to exclude certain system decoration elements that are always visible. It may also be scaled to provide compatibility with older applications that were originally designed for smaller displays

Parameters

outSize  object to receive the size information. 
那么到底怎么使用呢:
方案1
:If you want the the display dimensions in pixels you can usegetSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
 
方案2: if you're not in an Activity you can get the default Display viaWINDOW_SERVICE:
display = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE)).getDefaultDisplay();
diswidth = getContext().getResources().getDisplayMetrics().widthPixels;
disheight = getContext().getResources().getDisplayMetrics().heightPixels;

 

转载于:https://www.cnblogs.com/akiha/p/5889274.html

你可能感兴趣的文章
文件系统测试工具整理
查看>>
好用的性能检测工具 - Glances
查看>>
tcp滑动窗口和读写缓冲区
查看>>
GO 使用静态链接库编译 生成可执行文件 使用第三方 .a 文件,无源码构造
查看>>
ssh 使用指定网卡 连接特定网络
查看>>
鸿蒙操作系统发布会 分析 记录
查看>>
浅谈python 中正则的一些函数
查看>>
app生命周期之即将关闭
查看>>
MPU6050
查看>>
Asp.Net 加载不同项目程序集
查看>>
Jenkins插件--通知Notification
查看>>
思1-基本三观
查看>>
angularJS--apply() 和digest()方法
查看>>
Alpha 冲刺 (5/10)
查看>>
PHP函数之$_SERVER
查看>>
利用安装光盘创建本地yum源补装 RPM 软件包-通过命令行模式
查看>>
XML通過XSD產生CLASS
查看>>
跨线程调用窗体控件
查看>>
linq to sql 扩展方法
查看>>
241. Different Ways to Add Parentheses
查看>>