方法一:

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 getSize(Point) instead. 

在看一下getSize(Point)方法:

public void getSize (Point outSize)

Added in API level 13

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 Point 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;

android获得屏幕高度和宽度(display中getSize(Point)方法使用)的更多相关文章

  1. android获得屏幕高度和宽度

    获取屏幕的宽度与高度有以下几种方法: .WindowManager wm = (WindowManager) getContext()                     .getSystemSe ...

  2. 【笔记】css 实现宽度自适应屏幕 高度自适应宽度

    如果说宽高自适应屏幕尺寸那么大家可能会想到 div{ width: 100%; height: 100% } 但是如果我要自适应屏幕尺寸的同时还要变成正方形呢?(高度和宽度相等而高度不能写死) 那就要 ...

  3. JavaScript浏览器对象(BOM)中有关设备、浏览器屏幕高度和宽度的API介绍

    JavaScript世界中,有很多看起来能够帮我们知道网页宽度和高度的API,但太繁多了,而且容易弄混.不容易区分它们.下面我就来介绍一下,这些API到底是什么意思,之间的区别又在哪里. 一.设备的分 ...

  4. css设置div高度与宽度相等的一种方法

    div.category{ width:33%; padding:33% 0 0; } 1.关键在padding:33% 0 0这句代码,通过设置padding-top与宽度相等(padding使用百 ...

  5. 网页或微信小程序中使元素占满整个屏幕高度

    在项目中经常要用到一个容器元素占满屏幕高度和宽度,然后再在这个容器元素里放置其他元素. 宽度很简单就是width:100% 但是高度呢,我们知道的是height:100%必须是在父元素的高度给定了的情 ...

  6. Android 关于屏幕适配

    android屏幕适配详解 官方地址:http://developer.android.com/guide/practices/screens_support.html 转自:http://www.c ...

  7. 获取Android状态栏的高度

    Android 开发中经常需要知道屏幕高度.宽度.状态栏,标题栏的高度等 宽度和高度 WindowManager windowManager = (WindowManager) getSystemSe ...

  8. android 获取屏幕的高度和宽度、获取控件在屏幕中的位置、获取屏幕中控件的高度和宽度

    (一)获取屏幕的高度和宽度 有两种方法: 方法1: WindowManager wm = (WindowManager) getContext().getSystemService(Context.W ...

  9. Android 修改toast的默认位置和获取当前屏幕的高度和宽度

    Toast toast; toast=Toast.makeText(this, "toast", Toast.LENGTH_LONG); toast.setGravity(grav ...

随机推荐

  1. jsoneditor显示Json data

    Git开源地址:https://github.com/josdejong/jsoneditor/blob/master/docs/api.md 1.引用JS文件 <!-- jsoneditor ...

  2. WebSocket在ASP.NET MVC4中的简单实现

    WebSocket在ASP.NET MVC4中的简单实现 2013-12-21 20:48 by 小白哥哥, 810 阅读, 3 评论, 收藏, 编辑 WebSocket 规范的目标是在浏览器中实现和 ...

  3. MD5算法-爬虫学习(五)

    在实现爬虫的时候,我们使用Hash结构去存储我们用过的URL的时候,有些URL可能长度很长,为了更加节省空间,我们就要对URL进行压缩,帮它减减肥,这个我们介绍这个MD5算法,可以对URL进行有效的压 ...

  4. (转)poj1182食物链

    这题主要是看了http://blog.csdn.net/c0de4fun/article/details/7318642这篇解题报告,所以内容基本是转的!感谢大牛这么详细的把过程写的很清楚! 这道题目 ...

  5. learn objetive-c

    Cocoa Dev Central Objective-C Objective-C is the primary language used to write Mac software. If you ...

  6. 清除在Windows下访问共享文件夹时的登录信息

    清除在Windows下访问共享文件夹时的登录信息 在实际工作中,经常需要访问局域网内其他机子上的共享文件夹,例如\\192.168.1.100\d$ , 首次访问时,需要输入用户名和密码才可以进入,即 ...

  7. jQuery判断元素是否显示与隐藏

    jQuery判断一个元素是显示还是隐藏,jQuery使用is()方法来判断一个元素是否显示,反之则为隐藏 核心代码 if($("#username").is(":hidd ...

  8. EasyUI tree扩展获取实心节点

    <script type="text/javascript"> //扩展 获得tree 的实心节点 $(function(){ $.extend($.fn.tree.m ...

  9. Microsoft 电信项目组 Net代码生成器1.1

    Microsoft 电信项目组 Net代码生成器1.1 微软电信项目组代码生成器 为什么要使用这个代码生成器: 1.这个代码生成器采用 微软 企业库5.0 版本,目前CAPO所在的项目组都在使用这个 ...

  10. HDU 4062 Partition

    Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...