content: package com.agenthun.readingroutine.utils;
  
  import android.content.Context;
  import android.util.DisplayMetrics;
  import android.view.WindowManager;
  
  /**
   * Created by Agent Henry on 2015/4/23.
   */
  public class UiUtils {
      public static int getScreenWidthPixels(Context context) {
          DisplayMetrics dm = new DisplayMetrics();
          ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
          return dm.widthPixels;
      }
  
      public static int getScreenHeightPixels(Context context) {
          DisplayMetrics dm = new DisplayMetrics();
          ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
          return dm.heightPixels;
      }
  
      public static float dipToPx(Context context, int dip) {
          return dip * getScreenDensity(context) + 0.5f;
      }
  
      public static float getScreenDensity(Context context) {
          try {
              DisplayMetrics dm = new DisplayMetrics();
              ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
              return dm.density;
          } catch (Exception e) {
              return DisplayMetrics.DENSITY_DEFAULT;
          }
      }
  }
  