content: package com.jinlin.zxing.utils;
  
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.util.TypedValue;
  
  
  public class DensityUtils {
  
      private DensityUtils() {
          /* cannot be instantiated */
          throw new UnsupportedOperationException("cannot be instantiated");
      }
  
      /**
       * dp转px
       *
       * @param context
       * @return
       */
      public static int dp2px(Context context, float dpVal) {
          return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                  dpVal, context.getResources().getDisplayMetrics());
      }
  
      /**
       * sp转px
       *
       * @param context
       * @return
       */
      public static int sp2px(Context context, float spVal) {
          return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                  spVal, context.getResources().getDisplayMetrics());
      }
  
      /**
       * px转dp
       *
       * @param context
       * @param pxVal
       * @return
       */
      public static float px2dp(Context context, float pxVal) {
          final float scale = context.getResources().getDisplayMetrics().density;
          return (pxVal / scale);
      }
  
      /**
       * px转sp
       *
       * @param fontScale
       * @param pxVal
       * @return
       */
      public static float px2sp(Context context, float pxVal) {
          return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
      }
  
      public static int getScreenHeight(Context context) {
          return context.getResources().getDisplayMetrics().heightPixels;
      }
  
      public static int getScreenWidth(Context context) {
          return context.getResources().getDisplayMetrics().widthPixels;
      }
  
      public static int getToolbarHeight(Context context) {
          final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
                  new int[]{android.R.attr.actionBarSize});
          int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
          styledAttributes.recycle();
  
          return toolbarHeight;
      }
  
      /**
       * 获取状态栏高度
       *
       * @param context context
       * @return 状态栏高度
       */
      public static int getStatusBarHeight(Context context) {
          // 获得状态栏高度
          int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
          return context.getResources().getDimensionPixelSize(resourceId);
      }
  }
  