Android学习之四:获取屏幕分辨率

 
  DisplayMetrics dm=new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    String str="您的手机分辨率为:"+dm.widthPixels+" X "+dm.heightPixels;

 

《Android SDK开发范例大全》对上面代码的解释:
构造函数DisplayMetrics 不需要传递任何参数;调用getWindowManager() 之后,会取得现有Activity 的Handle ,
此时,getDefaultDisplay() 方法将取得的宽高维度存放于DisplayMetrics 对象中,
而取得的宽高维度是以像素为单位(Pixel) ,“像素”所指的是“绝对像素”而非“相对像素”。

拓展:

上面涉及到的几种类:

1.DisplayMetrics类
一种数据结构用以描述display的信息,包括它的大小,分辨率和字体大小
A structure describing general information about a display, such as its size, density, and font scaling.
2. this.getWindowManager()得到的对象是WindowManager
   WindowManager类:
 The interface that apps use to talk to the window manager.
 Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these.

3. getDefaultDisplay()得到的是Display这个类
涉及到方法
void  getMetrics(DisplayMetrics outMetrics)
 Initialize a DisplayMetrics object from this display's data.
 将 display的数据初始化一个DisplayMetrics 对象
这个类的其他方法:
int  getDisplayId()
 Returns the index of this display.
int  getHeight()
 Returns the raw height of the display, in pixels.
int  getOrientation()
 This method is deprecated. use getRotation()
int  getPixelFormat()
 Return the native pixel format of the display.
float  getRefreshRate()
 Return the refresh rate of this display in frames per second.
int  getRotation()
 Returns the rotation of the screen from its "natural" orientation.
int  getWidth()
 Returns the raw width of the display, in pixels.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值