content: package yifeiyuan.practice.yutils;
  
  import android.app.Activity;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.Rect;
  import android.util.DisplayMetrics;
  import android.util.TypedValue;
  import android.view.View;
  import android.view.WindowManager;
  
  /**
   * Created by alanchen on 15/9/8.
   */
  public class ViewUtils {
  
      public static float getElevation(Context context, float degree) {
          return context.getResources().getDisplayMetrics().density * degree;
      }
  
      public static float getElevation(Context context, int degree) {
          return context.getResources().getDisplayMetrics().density * degree;
      }
  
      /**
       * 获得屏幕高度
       * @param context
       */
      public static int getScreenWidth(Context context) {
          WindowManager wm = (WindowManager) context
                  .getSystemService(Context.WINDOW_SERVICE);
          DisplayMetrics outMetrics = new DisplayMetrics();
          wm.getDefaultDisplay().getMetrics(outMetrics);
          return outMetrics.widthPixels;
      }
  
  
      /**
       * 获得屏幕宽度
       * @param context
       */
      public static int getScreenHeight(Context context) {
          WindowManager wm = (WindowManager) context
                  .getSystemService(Context.WINDOW_SERVICE);
          DisplayMetrics outMetrics = new DisplayMetrics();
          wm.getDefaultDisplay().getMetrics(outMetrics);
          return outMetrics.heightPixels;
      }
  
  
  
      /**
       * 获取屏幕密度
       * @param context
       */
      public static float getScreenDensity(Context context) {
          return context.getResources().getDisplayMetrics().density;
      }
  
      /**
       * 获取状态栏高度
       * @param context
       * @return
       */
      public static int getStatusBarHeight(Context context) {
          int result = 0;
  
          int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
  
          if (resourceId > 0) {
              result = context.getResources().getDimensionPixelSize(resourceId);
          }
  
          return result;
      }
  
      public static int getToolbarHeight(Context context) {
          TypedValue typedValue = new TypedValue();
  
          if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true)) {
              return TypedValue.complexToDimensionPixelSize(typedValue.data, context.getResources().getDisplayMetrics());
          }
  
          return 0;
      }
  
  
  
      /**
       * 获取当前屏幕截图，包含状态栏
       * @param activity
       */
      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;
  
      }
  
      /**
       * 获取当前屏幕截图，不包含状态栏
       * @param activity
       * @return
       * by Hankkin at:2015-10-07 21:16:43
       */
      public static Bitmap snapShotWithoutStatusBar(Activity activity) {
          View view = activity.getWindow().getDecorView();
          view.setDrawingCacheEnabled(true);
          view.buildDrawingCache();
          Bitmap bmp = view.getDrawingCache();
          Rect frame = new Rect();
          view.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;
  
      }
  
  }
  