在View构造函数中获得屏幕的宽高

public class GameView extends View
{
public GameView(Context context)
{
Display d = ((Activity) context).getWindowManager().getDefaultDisplay();
Log.v(TAG,"W + H = "+d.getWidth()+" + "+d.getHeight());
}
} ========================================================== DisplayMetrics定义了屏幕的一些属性,可以通过getMetrics方法得到当前屏幕的DisplayMetrics属性,代码如: DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; //得到宽度 int height = dm.heightPixels; //得到高度 ========================================================== View中的onLayout方法传入的参数可以获得当前可用区域的宽高度: protected void onLayout(boolean changed, int left, int top, int right,int bottom)
{ int width = right - left; //得到宽度 int height = bottom - top; //得到高度
}

获取屏幕宽高

// 获取屏幕宽高(方法1)
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px)
int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p)
Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// 获取屏幕密度(方法2)
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
float xdpi = dm.xdpi;
float ydpi = dm.ydpi;
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
screenWidth = dm.widthPixels; // 屏幕宽(像素,如:480px)
screenHeight = dm.heightPixels; // 屏幕高(像素,如:800px)
Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// 获取屏幕密度(方法3)
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
xdpi = dm.xdpi;
ydpi = dm.ydpi;
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
int screenWidthDip = dm.widthPixels; // 屏幕宽(dip,如:320dip)
int screenHeightDip = dm.heightPixels; // 屏幕宽(dip,如:533dip)
Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);
screenWidth = (int)(dm.widthPixels * density + 0.5f); // 屏幕宽(px,如:480px)
screenHeight = (int)(dm.heightPixels * density + 0.5f); // 屏幕高(px,如:800px)
Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);

获取控件的宽高,一般来说,我们在onCreate里面得到的控件的宽高全是0.采用下面的方法,可以得到真实的宽高

        //------------------------------------------------方法一
int w = View.MeasureSpec.makeMeasureSpec(,View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(,View.MeasureSpec.UNSPECIFIED);
imageView.measure(w, h);
int height =imageView.getMeasuredHeight();
int width =imageView.getMeasuredWidth();
textView.append("\n"+height+","+width);     此方法会加载onMeasure三次 //-----------------------------------------------方法二
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int height = imageView.getMeasuredHeight();
int width = imageView.getMeasuredWidth();
textView.append("\n"+height+","+width);
return true;
}
});     此方法会加载onMeasure二次,但是回调函数会回调很多次      //-----------------------------------------------方法三
ViewTreeObserver vto2 = imageView.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());
}
});     <pre name="code" class="csharp">  
   此方法会加载onMeasure二次,但是回调函数只回调一次
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();

Android 获得屏幕的宽高度的更多相关文章

  1. android获取屏幕长宽的方法

    package com.kale.imageview02; import android.annotation.SuppressLint; import android.app.Activity; i ...

  2. Android获取屏幕长宽

    总结了下,我遇到的获取Android屏幕长宽的方式总共有三种.大同小异,重点在于如何获取系统中的WindowManager管理类对象,方可对数据的操作: 代码如下 /** * @return 屏幕的长 ...

  3. 获取Android 手机屏幕宽度和高度以及获取Android手机序列号

    1.获取Android 手机屏幕宽度 1 DisplayMetrics dm = new DisplayMetrics(); 2 this.getWindowManager().getDefaultD ...

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  5. android 获取屏幕的宽和高

    屏幕高度:context.getResources().getDisplayMetrics().heightPixels 屏幕宽度:context.getResources().getDisplayM ...

  6. 获取android手机屏幕的宽高、density

    public static String getDisplayMetrics(Context cx) { String str = ""; DisplayMetrics dm = ...

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

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

  8. Android 开发 获取Android设备的屏幕高宽

    获得屏幕的宽度和高度有很多种方法: //1.通过WindowManager获取 DisplayMetrics dm = new DisplayMetrics(); heigth = dm.height ...

  9. Android中获取屏幕长宽的方法

    package com.kale.screen; import android.annotation.SuppressLint; import android.app.Activity; import ...

随机推荐

  1. 转载---SQL Server XML基础学习之<6>--XQuery的 value() 方法、 exist() 方法 和 nodes() 方法

    /*------------------------------------------------------------------------------+ #| = : = : = : = : ...

  2. wpf打印控件 实现分页打印控件功能

    因为 要实现打印 wpf  listbox控件  数据特别多 要打印在 几张纸上    找了几天 都没有找到相关的例子 现在 解决了 在这里和大家分享一下 public void print(Fram ...

  3. oracle里面的时间转字符串to_char(),字符串转时间to_date(),以及substr和instr的使用。

    工作中编写过的一条语句 select * from Bt_Holiday where to_char(Setting_DATE,'YYYY')=Substr('2015-03-00',1,4) AND ...

  4. 解决自定义BackItem与Pop Gesture冲突的问题

    在做项目的时候遇到的这个问题, 一开始项目要求自定义导航栏返回按钮,结果发生了没法手势返回的问题,以为是需要添加拖拽手势呢,结果折腾了一下午没有实现想要的效果.接着一直百度问题,才发现跑偏了,犯了一个 ...

  5. ios--socket

    一.打开服务器 a.在终端打开,到服务器文件路径输入命令 python chatserver.py b.当显示 Iphone Chat server started 表示成功 二.建立连接 a.设置对 ...

  6. aspx文件移动到新建的文件夹中设置路径的问题

    项目中仅仅把aspx移动到想要的文件夹内是会出错的,不用想也知道是路径问题.这里我就说这个路径该如何去修改. 两个地方需要修改:1.母版路径修改方法: <link href="Styl ...

  7. OpenJudge 2738 浮点数加法

    1.链接地址: http://bailian.openjudge.cn/practice/2738 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 求2个浮点数相加的和 题目 ...

  8. json字符串转JSONObject,输出JSONObject问题

    json架包:json-lib-2.4-jdk15.jar json字符串(存在null值)转JSONObject 后return JSONObject对象的时候会报错 例如: String str= ...

  9. CentOS7 列出服务和对应端口

    列出服务和他们对应的端口: netstat -tulpn

  10. HDOJ(1005) Number Sequence

    这道题,咋一看很像Fibonacci数列,使用递归或者改进的动态规划来解决.但是仔细一看,(1 <= n <= 100,000,000),使用这些方法,要么超时,要么肯定内存不够用,因此必 ...