zoukankan      html  css  js  c++  java
  • Android学习笔记获取屏幕大小

    方法
    做屏幕适配时,有时需要知道屏幕的宽高,我们一般采用Android提供的“DisplayMetrics”类来获取相应信息。
    先看下DisplayMetrics的简单使用方法示例:
    package com.example.isweixin.window;
    import android.util.DisplayMetrics;
    import android.widget.PopupWindow;
    import android.app.Activity;
    import android.util.Log;
    
    public class MenuWindowAdd extends PopupWindow {
    	public MenuWindowAdd(final Activity context) {
    		DisplayMetrics dm = new DisplayMetrics();
    		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
    
    		int iWidth  = dm.widthPixels;
    		int iHeight = dm.heightPixels;
    		Log.e("motadou", iWidth + "," + iHeight);
    	}
    }
    
    通过DisplayMetrics类我们可以获得显示设备的这些信息:
    另外一种获取屏幕宽高的大小:
    今天使用Display获取屏幕的宽和高时出现下面的提示: Display dp=getWindowManager().getDefaultDisplay(); int Height=dp.getHeight(); ---->The method getHeight() from the type Display is deprecated int Width=dp.getWidth(); ---->The method getWidth() from the type Display is deprecated 复制代码 官方解释: int getHeight() This method was deprecated in API level 13. Use getSize(Point) instead. int getWidth() This method was deprecated in API level 13. Use getSize(Point) instead. http://developer.android.com/reference/android/view/Display.html 复制代码 复制代码 替代的方法: DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); SCREEN_WIDTH = dm.widthPixels; SCREEN_HEIGHT = dm.heightPixels; 解析heightPixels和widthPixels: public int heightPixels The absolute height of the display in pixels. public int widthPixels The absolute width of the display in pixels.
    附录
    1.与2015.04.16重新修改;
    2.参考:http://www.cnblogs.com/lyyh-victory/p/3760014.html
  • 相关阅读:
    重大利好,Dubbo 3.0要来了。
    SaaS,PaaS,IaaS都是什么鬼?
    为什么Netty这么火?与Mina相比有什么优势?
    (21)python lambda表达式
    (19)python scrapy框架
    (18) python 爬虫实战
    (5)ASP.NET HTML服务器控件
    (17)python Beautiful Soup 4.6
    (4)ASP.NET内置对象1
    (16)网络爬虫
  • 原文地址:https://www.cnblogs.com/motadou/p/2875879.html
Copyright © 2011-2022 走看看