ImportJava.lang.reflect.Field;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.graphics.Point;ImportAndroid.util.DisplayMetrics;ImportAndroid.view.Display;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;/*** Get screen width height tool class * Created by Administrator on 2015/10/21 0021.*/ Public classScreenutils {/*** Used to get the height of the status bar. * * @returnreturns the pixel value of the status bar height. */ Public Static intgetstatusbarheight (activity activity) {Try{Class<?> C = class.forname ("Com.android.internal.r$dimen"); Object obj=c.newinstance (); Field field= C.getfield ("Status_bar_height"); intx =integer.parseint (Field.get (obj). toString ()); returnactivity.getresources (). getdimensionpixelsize (x); } Catch(Exception e) {//LOGHELPER.E ("Exception", "*****exception*****\n", e); } return0; } /*** Used to get the height of the status bar. Get (recommend this way) with the resource object * *@returnreturns the pixel value of the status bar height. */ Public Static intGetstatusbarheight (Context context) {intresult = 0; intResourceId = Context.getresources (). Getidentifier ("Status_bar_height", "Dimen", "Android"); if(ResourceId > 0) {result=context.getresources (). Getdimensionpixelsize (ResourceId); } returnresult; } /*** Get title bar Height * *@paramactivity *@return */ Public Static intgettitlebarheight (activity activity) {intContenttop =Activity.getwindow (). Findviewbyid (window.id_android_content). GetTop (); returnContenttop-getstatusbarheight (activity); } /*** Get the height and width of the screen in activity * *@paramactivity in the real machine, sometimes found that the size is not very accurate, you need to add the following configuration in androidmanifest: * <supports-screens android:smallscreens= "True" * android:normalscreens= "true" android:largescreens= "true" * android:resizeable= "true "android:anydensity=" true "/>*/ Public Static int[] getscreensize (activity activity) {display display=Activity.getwindowmanager (). Getdefaultdisplay (); Point Point=NewPoint (); Display.getsize (point); return New int[] {point.x, point.y}; } /*** In non-activity, usually at Custom view * <p/> * Currently not recommended (3.2 and below) * *@paramcontext in the real machine, sometimes you will find that the size is not very accurate, you need to add the following configuration in androidmanifest: * <supports-screens android:smallscreens= " True "* android:normalscreens=" true "android:largescreens=" true "* android:resizeable=" true " Android:anydensity= "true"/>*/ Public Static int[] getscreensize (context context) {WindowManager WM=(WindowManager) Context.getsystemservice (Context.window_service); Displaymetrics DM=NewDisplaymetrics (); Wm.getdefaultdisplay (). Getmetrics (DM); return New int[] {dm.widthpixels, dm.heightpixels}; }}
Android Get screen width height tool class