content: package indi.liyi.example.utils;
  
  import android.content.Context;
  import android.graphics.Point;
  import android.os.Build;
  import android.support.annotation.NonNull;
  import android.view.WindowManager;
  
  public class Utils {
  
      public static int dp2px(Context context, float dpVal) {
          final float scale = context.getResources().getDisplayMetrics().density;
          return (int) (dpVal * scale + 0.5f);
      }
  
      public static Point getScreenSize(@NonNull Context context) {
          Point screenSize = new Point();
          WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          if (wm == null) {
              screenSize.set(context.getResources().getDisplayMetrics().widthPixels,
                      context.getResources().getDisplayMetrics().heightPixels);
              return screenSize;
          }
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              wm.getDefaultDisplay().getRealSize(screenSize);
          } else {
              wm.getDefaultDisplay().getSize(screenSize);
          }
          return screenSize;
      }
  
      /**
       * 获取状态栏的高度
       *
       * @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;
      }
  }
  