1. Implementing the Code
Private intMwidth; Private intMheight; @TargetApi (build.version_codes. JELLY_BEAN_MR1) Public voidGetdisplaymetrics () {WindowManager wm=(WindowManager) Mcontext.getsystemservice (Context.window_service); Display Display=Wm.getdefaultdisplay (); intApi_level =Android.os.Build.VERSION.SDK_INT; //Method 1Displaymetrics Displaymetrics =NewDisplaymetrics (); if(Api_level >= 17) {display.getrealmetrics (displaymetrics); } Else{display.getmetrics (displaymetrics); } mwidth=Displaymetrics.widthpixels; Mheight=Displaymetrics.heightpixels; //Method 2Point outsize =NewPoint (); if(Api_level >= 17) {display.getrealsize (outsize); } Else{display.getsize (outsize); } mwidth=outsize.x; Mheight=outsize.y; //Method 3Mwidth =mcontext.getresources (). Getdisplaymetrics (). Widthpixels; Mheight=mcontext.getresources (). Getdisplaymetrics (). Heightpixels; }
2. Description
Display
Provides information about the size and density of a logical display.
The display area was described in and different ways.
- The application display area specifies the part of the display, the contain an application window, excluding the system decorations. The application display area is smaller than the real display area because the system su Btracts 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 contains content including the system decorations. E Ven so, the real display area could be smaller than the physical size of the display if the Windows Manager is emulating a SM Aller display using (adb shell am display-size). Use the following methods to query the real display area:
getRealSize(Point) , getRealMetrics(DisplayMetrics) .
3. Note
Public void getrealmetrics (displaymetrics outmetrics) Added in API level
Gets display metrics based on the real size of this display.
The size is adjusted based on the current rotation of the display.
The real size may be smaller than the physical size of the "when" window manager is emulating a smaller display ( Using adb shell am display-size).
Public void getrealsize (point outsize) Added in API level
Gets the real size of the display without subtracting any window decor or applying any compatibility scale factor S.
The size is adjusted based on the current rotation of the display.
The real size may be smaller than the physical size of the "when" window manager is emulating a smaller display ( Using adb shell am display-size).
How Android Gets a wide screen height