To get a screen-related helper class:
1, get the screen height;
2, get the screen width;
3, get the height of the status bar;
4, get the current screen, including the status bar;
5. Get the current screen without the status bar.
1 Importandroid.app.Activity;2 ImportAndroid.content.Context;3 ImportAndroid.graphics.Bitmap;4 ImportAndroid.graphics.Rect;5 ImportAndroid.util.DisplayMetrics;6 ImportAndroid.view.View;7 ImportAndroid.view.WindowManager;8 9 //get a screen-related helper classTen Public classscreenutils One { A Privatescreenutils () - { - /*cannot be instantiated*/ the Throw NewUnsupportedoperationexception ("cannot be instantiated"); - } - - /** + * Get screen height - * + * @paramContext A * @return at */ - Public Static intGetscreenwidth (Context context) - { -WindowManager WM =(windowmanager) Context - . Getsystemservice (context.window_service); -Displaymetrics Outmetrics =Newdisplaymetrics (); in Wm.getdefaultdisplay (). Getmetrics (outmetrics); - returnOutmetrics.widthpixels; to } + - /** the * Get screen width * * $ * @paramContextPanax Notoginseng * @return - */ the Public Static intGetscreenheight (Context context) + { AWindowManager WM =(windowmanager) Context the . Getsystemservice (context.window_service); +Displaymetrics Outmetrics =Newdisplaymetrics (); - Wm.getdefaultdisplay (). Getmetrics (outmetrics); $ returnOutmetrics.heightpixels; $ } - - /** the * Get the height of the status bar - * Wuyi * @paramContext the * @return - */ Wu Public Static intGetstatusheight (Context context) - { About $ intStatusheight =-1; - Try - { -Class<?> clazz = Class.forName ("Com.android.internal.r$dimen"); AObject object =clazz.newinstance (); + intHeight = integer.parseint (Clazz.getfield ("Status_bar_height") the . Get (object). toString ()); -Statusheight =context.getresources (). getdimensionpixelsize (height); $}Catch(Exception e) the { the e.printstacktrace (); the } the returnStatusheight; - } in the /** the * Get current screen, including status bar About * the * @paramActivity the * @return the */ + Public StaticBitmap Snapshotwithstatusbar (activity activity) - { theView view =Activity.getwindow (). Getdecorview ();BayiView.setdrawingcacheenabled (true); the View.builddrawingcache (); theBitmap BMP =View.getdrawingcache (); - intwidth =getscreenwidth (activity); - intHeight =getscreenheight (activity); theBitmap BP =NULL; thebp = bitmap.createbitmap (BMP, 0, 0, width, height); the View.destroydrawingcache (); the returnBP; - the } the the /**94 * Get the current screen, not including the status bar the * the * @paramActivity the * @return98 */ About Public StaticBitmap Snapshotwithoutstatusbar (activity activity) - {101View view =Activity.getwindow (). Getdecorview ();102View.setdrawingcacheenabled (true);103 View.builddrawingcache ();104Bitmap BMP =View.getdrawingcache (); theRect frame =NewRect ();106 Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);107 intStatusbarheight =Frame.top;108 109 intwidth =getscreenwidth (activity); the intHeight =getscreenheight (activity);111Bitmap BP =NULL; thebp = bitmap.createbitmap (BMP, 0, statusbarheight, width, height113-statusbarheight); the View.destroydrawingcache (); the returnBP; the 117 }118}
Android development tool Class 07_screenutils