Android 获取屏幕高宽度,密度,通知栏高度,截图等常用方法

来源:互联网 发布:网络热销产品排行榜 编辑:程序博客网 时间:2024/06/06 00:42

分享一下Android中常用的一些辅助方法:

获取屏幕高度:

[java] view plaincopyprint?
  1. /** 
  2.     * 获得屏幕高度 
  3.     * @param context 
  4.     * @return 
  5.     * by Hankkin at:2015-10-07 21:15:59 
  6.     */  
  7.    public static int getScreenWidth(Context context) {  
  8.        WindowManager wm = (WindowManager) context  
  9.                .getSystemService(Context.WINDOW_SERVICE);  
  10.        DisplayMetrics outMetrics = new DisplayMetrics();  
  11.        wm.getDefaultDisplay().getMetrics(outMetrics);  
  12.        return outMetrics.widthPixels;  
  13.    }  

获取屏幕宽度:

[java] view plaincopyprint?
  1. /** 
  2.      * 获得屏幕宽度 
  3.      * @param context 
  4.      * @return 
  5.      * by Hankkin at:2015-10-07 21:16:13 
  6.      */  
  7.     public static int getScreenHeight(Context context) {  
  8.         WindowManager wm = (WindowManager) context  
  9.                 .getSystemService(Context.WINDOW_SERVICE);  
  10.         DisplayMetrics outMetrics = new DisplayMetrics();  
  11.         wm.getDefaultDisplay().getMetrics(outMetrics);  
  12.         return outMetrics.heightPixels;  
  13.     }  
获取屏幕密度:

[java] view plaincopyprint?
  1. /** 
  2.      * 获取屏幕密度 
  3.      * @param context 
  4.      * @return 
  5.      * by Hankkin at:2015-10-07 21:16:29 
  6.      */  
  7.     public static float getScreenDensity(Context context) {  
  8.         return context.getResources().getDisplayMetrics().density;  
  9.     }  

dip转px:

[java] view plaincopyprint?
  1. /** 
  2.      * dip转px像素 
  3.      * @param context 
  4.      * @param px 
  5.      * @return 
  6.      * by Hankkin at:2015-10-07 21:16:43 
  7.      */  
  8.     public static int dip2px(Context context, float px) {  
  9.         final float scale = getScreenDensity(context);  
  10.         return (int) (px * scale + 0.5);  
  11.     }  

获取状态栏高度:

[java] view plaincopyprint?
  1. /** 
  2.      * 获得状态栏的高度 
  3.      * @param context 
  4.      * @return 
  5.      * by Hankkin at:2015-10-07 21:16:43 
  6.      */  
  7.     public static int getStatusHeight(Context context) {  
  8.   
  9.         int statusHeight = -1;  
  10.         try {  
  11.             Class<?> clazz = Class.forName("com.android.internal.R$dimen");  
  12.             Object object = clazz.newInstance();  
  13.             int height = Integer.parseInt(clazz.getField("status_bar_height")  
  14.                     .get(object).toString());  
  15.             statusHeight = context.getResources().getDimensionPixelSize(height);  
  16.         } catch (Exception e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.         return statusHeight;  
  20.     }  

获取屏幕当前截图:

[java] view plaincopyprint?
  1. /** 
  2.      * 获取当前屏幕截图,包含状态栏 
  3.      * @param activity 
  4.      * @return 
  5.      * by Hankkin at:2015-10-07 21:16:43 
  6.      */  
  7.     public static Bitmap snapShotWithStatusBar(Activity activity) {  
  8.         View view = activity.getWindow().getDecorView();  
  9.         view.setDrawingCacheEnabled(true);  
  10.         view.buildDrawingCache();  
  11.         Bitmap bmp = view.getDrawingCache();  
  12.         int width = getScreenWidth(activity);  
  13.         int height = getScreenHeight(activity);  
  14.         Bitmap bp = null;  
  15.         bp = Bitmap.createBitmap(bmp, 00, width, height);  
  16.         view.destroyDrawingCache();  
  17.         return bp;  
  18.   
  19.     }  
  20.   
  21.     /** 
  22.      * 获取当前屏幕截图,不包含状态栏 
  23.      * @param activity 
  24.      * @return 
  25.      * by Hankkin at:2015-10-07 21:16:43 
  26.      */  
  27.     public static Bitmap snapShotWithoutStatusBar(Activity activity) {  
  28.         View view = activity.getWindow().getDecorView();  
  29.         view.setDrawingCacheEnabled(true);  
  30.         view.buildDrawingCache();  
  31.         Bitmap bmp = view.getDrawingCache();  
  32.         Rect frame = new Rect();  
  33.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
  34.         int statusBarHeight = frame.top;  
  35.   
  36.         int width = getScreenWidth(activity);  
  37.         int height = getScreenHeight(activity);  
  38.         Bitmap bp = null;  
  39.         bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height  
  40.                 - statusBarHeight);  
  41.         view.destroyDrawingCache();  
  42.         return bp;  
  43.   
  44.     }  
0 0
原创粉丝点击