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 ;}}