码迷,mamicode.com
首页 > 移动开发 > 详细

Android 开发工具类 07_ScreenUtils

时间:2015-05-28 13:58:01      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

获得屏幕相关的辅助类:

1、获得屏幕高度;

2、获得屏幕宽度;

3、获得状态栏的高度;

4、获取当前屏幕截图,包含状态栏;

5、获取当前屏幕截图,不包含状态栏。

  1 import android.app.Activity;
  2 import android.content.Context;
  3 import android.graphics.Bitmap;
  4 import android.graphics.Rect;
  5 import android.util.DisplayMetrics;
  6 import android.view.View;
  7 import android.view.WindowManager;
  8 
  9 // 获得屏幕相关的辅助类
 10 public class ScreenUtils
 11 {
 12     private ScreenUtils()
 13     {
 14         /* cannot be instantiated */
 15         throw new UnsupportedOperationException("cannot be instantiated");
 16     }
 17 
 18     /**
 19      * 获得屏幕高度
 20      * 
 21      * @param context
 22      * @return
 23      */
 24     public static int getScreenWidth(Context context)
 25     {
 26         WindowManager wm = (WindowManager) context
 27                 .getSystemService(Context.WINDOW_SERVICE);
 28         DisplayMetrics outMetrics = new DisplayMetrics();
 29         wm.getDefaultDisplay().getMetrics(outMetrics);
 30         return outMetrics.widthPixels;
 31     }
 32 
 33     /**
 34      * 获得屏幕宽度
 35      * 
 36      * @param context
 37      * @return
 38      */
 39     public static int getScreenHeight(Context context)
 40     {
 41         WindowManager wm = (WindowManager) context
 42                 .getSystemService(Context.WINDOW_SERVICE);
 43         DisplayMetrics outMetrics = new DisplayMetrics();
 44         wm.getDefaultDisplay().getMetrics(outMetrics);
 45         return outMetrics.heightPixels;
 46     }
 47 
 48     /**
 49      * 获得状态栏的高度
 50      * 
 51      * @param context
 52      * @return
 53      */
 54     public static int getStatusHeight(Context context)
 55     {
 56 
 57         int statusHeight = -1;
 58         try
 59         {
 60             Class<?> clazz = Class.forName("com.android.internal.R$dimen");
 61             Object object = clazz.newInstance();
 62             int height = Integer.parseInt(clazz.getField("status_bar_height")
 63                     .get(object).toString());
 64             statusHeight = context.getResources().getDimensionPixelSize(height);
 65         } catch (Exception e)
 66         {
 67             e.printStackTrace();
 68         }
 69         return statusHeight;
 70     }
 71 
 72     /**
 73      * 获取当前屏幕截图,包含状态栏
 74      * 
 75      * @param activity
 76      * @return
 77      */
 78     public static Bitmap snapShotWithStatusBar(Activity activity)
 79     {
 80         View view = activity.getWindow().getDecorView();
 81         view.setDrawingCacheEnabled(true);
 82         view.buildDrawingCache();
 83         Bitmap bmp = view.getDrawingCache();
 84         int width = getScreenWidth(activity);
 85         int height = getScreenHeight(activity);
 86         Bitmap bp = null;
 87         bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
 88         view.destroyDrawingCache();
 89         return bp;
 90 
 91     }
 92 
 93     /**
 94      * 获取当前屏幕截图,不包含状态栏
 95      * 
 96      * @param activity
 97      * @return
 98      */
 99     public static Bitmap snapShotWithoutStatusBar(Activity activity)
100     {
101         View view = activity.getWindow().getDecorView();
102         view.setDrawingCacheEnabled(true);
103         view.buildDrawingCache();
104         Bitmap bmp = view.getDrawingCache();
105         Rect frame = new Rect();
106         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
107         int statusBarHeight = frame.top;
108 
109         int width = getScreenWidth(activity);
110         int height = getScreenHeight(activity);
111         Bitmap bp = null;
112         bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
113                 - statusBarHeight);
114         view.destroyDrawingCache();
115         return bp;
116 
117     }
118 }

 

Android 开发工具类 07_ScreenUtils

标签:

原文地址:http://www.cnblogs.com/renzimu/p/4535665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!