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.