Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖)
我們需要獲取Android手機或Pad的屏幕的物理尺寸,以便于界面的設(shè)計或是其他功能的實現(xiàn)。下面就分享一下Android中常用的一些輔助方法:
獲取屏幕高度:
/** * 獲得屏幕高度 * @param context * @return * by Hankkin at:2015-10-07 21:15:59 */ public static int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; }
獲取屏幕寬度:
/** * 獲得屏幕寬度 * @param context * @return * by Hankkin at:2015-10-07 21:16:13 */ public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; }
獲取屏幕密度:
/** * 獲取屏幕密度 * @param context * @return * by Hankkin at:2015-10-07 21:16:29 */ public static float getScreenDensity(Context context) { return context.getResources().getDisplayMetrics().density; }
dip轉(zhuǎn)px:
/** * dip轉(zhuǎn)px像素 * @param context * @param px * @return * by Hankkin at:2015-10-07 21:16:43 */ public static int dip2px(Context context, float px) { final float scale = getScreenDensity(context); return (int) (px * scale + 0.5); }
獲取狀態(tài)欄高度:
/** * 獲得狀態(tài)欄的高度 * @param context * @return * by Hankkin at:2015-10-07 21:16:43 */ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; }
獲取屏幕當前截圖:
/** * 獲取當前屏幕截圖,包含狀態(tài)欄 * @param activity * @return * by Hankkin at:2015-10-07 21:16:43 */ public static Bitmap snapShotWithStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp; } /** * 獲取當前屏幕截圖,不包含狀態(tài)欄 * @param activity * @return * by Hankkin at:2015-10-07 21:16:43 */ public static Bitmap snapShotWithoutStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bp; }
以上所述是本文給大家介紹的Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖),希望對大家也是幫助,更多信息登錄腳本之家網(wǎng)站了解更多信息。
相關(guān)文章
Android jni調(diào)試打印char陣列的實例詳解
這篇文章主要介紹了Android jni調(diào)試打印char陣列的實例詳解的相關(guān)資料,通過此文希望能幫助到大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08Android實現(xiàn)ImageView圖片縮放和拖動
這篇文章主要為大家詳細介紹了Android實現(xiàn)ImageView圖片縮放和拖動的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Andriod 讀取網(wǎng)絡(luò)圖片實例代碼解析
Android手機上,我們經(jīng)常用imageview顯示圖片,通過本文學習獲取網(wǎng)絡(luò)圖片并顯示在imageview中,對android讀取網(wǎng)絡(luò)圖片相關(guān)知識感興趣的朋友一起學習吧2016-02-02Android中修改TabLayout底部導航條Indicator長短的方法
Tablayout在我們?nèi)粘i_發(fā)中經(jīng)常會遇到,下面這篇文章主要給大家介紹了在Android中修改TabLayout底部導航條Indicator長短的方法,文中給出了詳細的示例代碼供大家參考學習,需要的朋友們下面來一起看看吧。2017-06-06Android編程實現(xiàn)禁止StatusBar下拉的方法
這篇文章主要介紹了Android編程實現(xiàn)禁止StatusBar下拉的方法,涉及Android StatusBarManager相關(guān)屬性控制操作技巧,需要的朋友可以參考下2017-08-08Android自定義View 使用PathMeasure簡單模仿系統(tǒng)ProgressBar(四)
這篇文章主要為大家詳細介紹了Android自定義View,使用PathMeasure簡單模仿系統(tǒng)ProgressBar,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android中SurfaceTexture TextureView SurfaceView GLSurfaceView
這篇文章主要介紹了Android中SurfaceTexture TextureView SurfaceView GLSurfaceView的區(qū)別,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Android使用ViewPager實現(xiàn)頂部tabbar切換界面
這篇文章主要為大家詳細介紹了使用ViewPager實現(xiàn)頂部tabbar切換界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08