屏幕相关辅助工具类

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