Android 開發工具類 07_ScreenUtils

來源:互聯網
上載者:User

標籤:

獲得螢幕相關的輔助類:

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, height113                 - statusBarHeight);114         view.destroyDrawingCache();115         return bp;116 117     }118 }

 

Android 開發工具類 07_ScreenUtils

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.