Android development tool Class 07_screenutils

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.