content: package com.zhongou.utils;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.res.Resources;
  import android.util.DisplayMetrics;
  import android.view.View;
  import android.view.WindowManager;
  
  /**
   * 屏幕操作工具
   * Created by sjy on 2016/11/29.
   */
  
  public class ScreenUtils {
  
  
      /**
       * 获取屏幕px
       *
       * @param context
       * @return 分辨率
       */
      static public int getScreenWidthPixels(Context context) {
          DisplayMetrics dm = new DisplayMetrics();
          ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                  .getMetrics(dm);
          return dm.widthPixels;
      }
  
      /**
       * 获取屏幕的宽度
       *
       * @param context
       * @return
       */
      public static int getScreenWidthDp(Context context) {
          WindowManager wm = (WindowManager)
                  context.getSystemService(Context.WINDOW_SERVICE);
  
          int width = wm.getDefaultDisplay().getWidth();
          return width;
      }
  
      /**
       * dip 转px
       *
       * @param context
       * @param dip
       * @return
       */
      static public int dipToPx(Context context, int dip) {
          return (int) (dip * getScreenDensity(context) + 0.5f);
      }
  
      /**
       * sp转px.
       *
       * @param value   the value
       * @param context the context
       * @return the int
       */
      public static int sp2px(float value, Context context) {
          Resources r;
          if (context == null) {
              r = Resources.getSystem();
          } else {
              r = context.getResources();
          }
          float spvalue = value * r.getDisplayMetrics().scaledDensity;
          return (int) (spvalue + 0.5f);
      }
  
      /**
       * px转sp.
       *
       * @param value   the value
       * @param context the context
       * @return the int
       */
      public static int px2sp(float value, Context context) {
          final float scale = context.getResources().getDisplayMetrics().scaledDensity;
          return (int) (value / scale + 0.5f);
      }
  
      /**
       * 获取屏幕密度
       *
       * @param context
       * @return
       */
  
      static public 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;
          }
      }
  
      /**
       * 获取屏幕宽高乘积
       *
       * @param activity
       * @return
       */
  
      public static int getScreenSize(Activity activity) {
          int widthPixels = 0;
          int heightPixels = 0;
          DisplayMetrics dm = new DisplayMetrics();
          if (dm != null) {
              activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
              widthPixels = dm.widthPixels;
              heightPixels = dm.heightPixels;
          }
          return widthPixels * heightPixels;
      }
  
  
      /**
       * 获取屏幕的高度
       *
       * @param context
       * @return
       */
      public static int getScreenHeight(Context context) {
          WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          DisplayMetrics outMetrics = new DisplayMetrics();// 创建了一张白纸
          windowManager.getDefaultDisplay().getMetrics(outMetrics);// 给白纸设置宽高
          return outMetrics.heightPixels;
      }
  
      /**
       * 获取屏幕中控件顶部位置的高度--即控件顶部的Y点
       *
       * @return
       */
      public static int getScreenViewTopHeight(View view) {
          return view.getTop();
      }
  
      /**
       * 获取屏幕中控件底部位置的高度--即控件底部的Y点
       *
       * @return
       */
      public static int getScreenViewBottomHeight(View view) {
          return view.getBottom();
      }
  
      /**
       * 获取屏幕中控件左侧的位置--即控件左侧的X点
       *
       * @return
       */
      public static int getScreenViewLeftHeight(View view) {
          return view.getLeft();
      }
  
      /**
       * 获取屏幕中控件右侧的位置--即控件右侧的X点
       *
       * @return
       */
      public static int getScreenViewRightHeight(View view) {
          return view.getRight();
      }
      /**
       * sp转为px
       */
      public static int sp2px(Context context, float spValue) {
          float fontScale = context.getResources().getDisplayMetrics().density;
          return (int) (spValue * fontScale + 0.5f);
      }
  
      /**
       * px转为sp
       */
      public static int px2sp(Context context, float pxValue) {
          final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
          return (int) (pxValue / fontScale + 0.5f);
      }
  
      /**
       * dip转为PX
       */
      public static int dip2px(Context context, float dipValue) {
          float fontScale = context.getResources().getDisplayMetrics().density;
          return (int) (dipValue * fontScale + 0.5f);
      }
  
      /**
       * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
       */
      public static int px2dip(Context context, float pxValue) {
          final float scale = context.getResources().getDisplayMetrics().density;
          return (int) (pxValue / scale + 0.5f);
      }
  
      /**
       * 获取屏幕的宽度px
       *
       * @param context 上下文
       * @return 屏幕宽px
       */
      public static int getScreenWidth(Context context) {
          WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          DisplayMetrics outMetrics = new DisplayMetrics();// 创建了一张白纸
          windowManager.getDefaultDisplay().getMetrics(outMetrics);// 给白纸设置宽高
          return outMetrics.widthPixels;
      }
  
  
  }
  
  