Android-get the screen width tool class, android-tool class

Source: Internet
Author: User

Android-get the screen width tool class, android-tool class

// Obtain the screen-related auxiliary class public class ScreenUtils {private ScreenUtils () {/* cannot be instantiated */throw new UnsupportedOperationException ("cannot be instantiated ");} /*** get screen width ** @ param context * @ return */public static int getScreenWidth (Context context) {WindowManager wm = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics (); wm. getDe FaultDisplay (). getMetrics (outMetrics); return outMetrics. widthPixels;}/*** obtain screen height ** @ param context * @ return */public static int getScreenHeight (Context context) {WindowManager wm = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics (); wm. getdefadisplay display (). getMetrics (outMetrics); return outMetrics. heightPixels;}/*** get the height of the status bar Degree ** @ param context * @ return */public static int getStatusHeight (Context context) {int statusHeight =-1; try {Class <?> Clazz = Class. forName ("com. android. internal. r$ dimen. xml "); Object object = clazz. newInstance (); int height = Integer. parseInt (clazz. getField ("status_bar_height "). get (object ). toString (); statusHeight = context. getResources (). getDimensionPixelSize (height);} catch (Exception e) {e. printStackTrace ();} return statusHeight;}/*** get the current screen, including the status bar ** @ param activity * @ return */public static Bitmap snapShotWithStatusBar (Activity activity) {View view = activity. getWindow (). getDecorView (); view. setDrawingCacheEnabled (true); view. buildDrawingCache (); Bitmap bmp = view. getDrawingCache (); int width = getScreenWidth (activity); int height = getScreenHeight (activity); Bitmap bp = null; bp = Bitmap. createBitmap (bmp, 0, 0, width, height); view. destroyDrawingCache (); return bp;}/*** get the current screen, excluding the status bar ** @ param activity * @ return */public static Bitmap snapShotWithoutStatusBar (Activity activity) {View view = activity. getWindow (). getDecorView (); view. setDrawingCacheEnabled (true); view. buildDrawingCache (); Bitmap bmp = view. getDrawingCache (); Rect frame = new Rect (); activity. getWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame); int statusBarHeight = frame. top; int width = getScreenWidth (activity); int height = getScreenHeight (activity); Bitmap bp = null; bp = Bitmap. createBitmap (bmp, 0, statusBarHeight, width, height-statusBarHeight); view. destroyDrawingCache (); return bp ;}}

 

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.