屏幕相关辅助类 ScreenUtils

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值