content: package com.handsome.didi.Utils;
  
  import android.content.Context;
  import android.util.DisplayMetrics;
  import android.view.WindowManager;
  
  /**
   * @author 许英俊 2017/4/14
   */
  public class DensityUtils {
  
      /**
       * dp转换成px
       *
       * @param context
       * @param dpValue
       * @return
       */
      public static int dip2px(Context context, float dpValue) {
          final float scale = context.getResources().getDisplayMetrics().density;
          return (int) (dpValue * scale);
      }
  
      /**
       * px转换成dip
       *
       * @param context
       * @param pxValue
       * @return
       */
      public static int px2dip(Context context, float pxValue) {
          final float scale = context.getResources().getDisplayMetrics().density;
          return (int) (pxValue / scale);
      }
  
      /**
       * 获取DisplayMetrics
       *
       * @param context
       * @return
       */
      public static DisplayMetrics getDisplayMetrics(Context context) {
          DisplayMetrics displayMetrics = new DisplayMetrics();
          WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          if (windowManager == null) {
              return displayMetrics;
          }
          windowManager.getDefaultDisplay().getMetrics(displayMetrics);
          return displayMetrics;
      }
  
      /**
       * 获取屏幕宽
       *
       * @param context
       * @return
       */
      public static int getDisplayWidth(Context context) {
          DisplayMetrics displayMetrics = new DisplayMetrics();
          WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          if (windowManager == null) {
              return displayMetrics.widthPixels;
          }
          windowManager.getDefaultDisplay().getMetrics(displayMetrics);
          return displayMetrics.widthPixels;
      }
  
      /**
       * 获取屏幕的高
       *
       * @param context
       * @return
       */
      public static int getDisplayHeight(Context context) {
          DisplayMetrics displayMetrics = new DisplayMetrics();
          WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          if (windowManager == null) {
              return displayMetrics.heightPixels;
          }
          windowManager.getDefaultDisplay().getMetrics(displayMetrics);
          return displayMetrics.heightPixels;
      }
  
  }
  