想要获取设备宽高分辨率,最好放到Utils跑龙套里面。


基础知识:

  1. WindowManager

  • The interface that apps use to talk to the window manager.

  • Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these.

  • Each window manager instance is bound to a particular Display. To obtain a WindowManager for a different display, use createDisplayContext(Display) to obtain a Context for that display, then use Context.getSystemService(Context.WINDOW_SERVICE) to get the WindowManager.

  • The simplest way to show a window on another display is to create a Presentation. The presentation will automatically obtain a WindowManager and Context for that display.

整个Android的窗口机制是基于一个叫做 WindowManager,这个接口可以添加view到屏幕,也可以从屏幕删除view。它面向的对象一端是屏幕,另一端就是View,直接忽略我们以前的Activity或者Dialog之类的东东。其实我们的Activity或者Diolog底层的实现也是通过WindowManager,这个 WindowManager是全局的,整个系统就是这个唯一的东东。它是显示View的最底层了。 (引自http://gundumw100.iteye.com/blog/830235)


方法:

abstractDisplay

getDefaultDisplay()

Returns theDisplayupon which thisWindowManagerinstance will create new windows.

abstract void removeViewImmediate(View view)

Special variation of removeView(View) that immediately invokes the given view hierarchy's View.onDetachedFromWindow() methods before returning.

2.Display

Provides information about the size and density of a logical display.

The display area is described in two different ways.

  • The application display area specifies the part of the display that may contain an application window, excluding the system decorations. The application display area may be smaller than the real display area because the system subtracts the space needed for decor elements such as the status bar. Use the following methods to query the application display area: getSize(Point), getRectSize(Rect) and getMetrics(DisplayMetrics).

  • The real display area specifies the part of the display that contains content including the system decorations. Even so, the real display area may be smaller than the physical size of the display if the window manager is emulating a smaller display using (adb shell am display-size). Use the following methods to query the real display area: getRealSize(Point), getRealMetrics(DisplayMetrics).


A logical display does not necessarily represent a particular physical display device such as the built-in screen or an external monitor. The contents of a logical display may be presented on one or more physical displays according to the devices that are currently attached and whether mirroring has been enabled.



下面给出代码:

importandroid.content.Context;importandroid.util.DisplayMetrics;importandroid.view.Display;importandroid.view.WindowManager;publicclassUtils{publicstaticintgetScreenWidth(Contextcontext){WindowManagermanager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);Displaydisplay=manager.getDefaultDisplay();returndisplay.getWidth();}publicstaticintgetScreenHeight(Contextcontext){WindowManagermanager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);Displaydisplay=manager.getDefaultDisplay();returndisplay.getHeight();}publicstaticfloatgetScreenDensity(Contextcontext){try{DisplayMetricsdm=newDisplayMetrics();WindowManagermanager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);manager.getDefaultDisplay().getMetrics(dm);returndm.density;}catch(Exceptionex){}return1.0f;}}










更多相关文章

  1. Android(安卓)支持多屏幕机制
  2. Android中Drawable、Bitmap、byte
  3. android 中获取屏幕大小
  4. Android监听屏幕屏幕锁屏与解锁
  5. Wakelock API详解
  6. Android单击屏幕获得坐标,屏幕多点触摸测试器
  7. Android单击屏幕获得坐标,屏幕多点触摸测试器
  8. android获取屏幕长宽的方法
  9. Android:获取屏幕分辨率方法汇总

随机推荐

  1. Android(安卓)按键事件响应
  2. [原] Android上使用native IO
  3. 踩坑之硬件加速
  4. Android(安卓)-------- BouncingJellyVie
  5. Android(安卓)ApiDemos示例解析(46):App->
  6. Android获取当前位置,location为空
  7. android 虚拟机网络问题处理
  8. Android中SharedPreferences使用方法介绍
  9. [Android]AndroidBucket增加碎片SubLayou
  10. 编程androidAndroid编程心得-JSON使用心