content: package com.aishang5wpj.juhenews.utils;
  
  import android.content.Context;
  import android.graphics.drawable.Drawable;
  import android.os.Build;
  import android.util.DisplayMetrics;
  import android.view.WindowManager;
  
  import java.io.UnsupportedEncodingException;
  import java.net.URLEncoder;
  
  /**
   * Created by wpj on 16/5/17下午2:38.
   */
  public class Utils {
  
      public static String urlEncoderStr(String string) {
          try {
              return URLEncoder.encode(string, "utf-8");
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          }
          return string;
      }
  
      public static Drawable getDrawable(Context context, int resId) {
          if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  
              return context.getDrawable(resId);
          }
          return context.getResources().getDrawable(resId);
      }
  
      public static int getWindowWidth(Context context) {
          WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          DisplayMetrics metric = new DisplayMetrics();
          wm.getDefaultDisplay().getMetrics(metric);
          return metric.widthPixels;
      }
  
      public static int getWindowHeight(Context context) {
          WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          DisplayMetrics metric = new DisplayMetrics();
          wm.getDefaultDisplay().getMetrics(metric);
          return metric.heightPixels;
      }
  
      /**
       * 获得状态栏的高度
       *
       * @param context
       * @return
       */
      public static int getStatusHeight(Context context) {
  
          int statusHeight = -1;
          try {
              Class<?> clazz = Class.forName("com.android.internal.R$dimen");
              Object object = clazz.newInstance();
              int height = Integer.parseInt(clazz.getField("status_bar_height")
                      .get(object).toString());
              statusHeight = context.getResources().getDimensionPixelSize(height);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return statusHeight;
      }
  }
  