Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖)
我們需要獲取Android手機(jī)或Pad的屏幕的物理尺寸,以便于界面的設(shè)計(jì)或是其他功能的實(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;
}
獲取屏幕當(dāng)前截圖:
/**
* 獲取當(dāng)前屏幕截圖,包含狀態(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;
}
/**
* 獲取當(dāng)前屏幕截圖,不包含狀態(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獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖),希望對(duì)大家也是幫助,更多信息登錄腳本之家網(wǎng)站了解更多信息。
相關(guān)文章
Android開發(fā)返回鍵明暗點(diǎn)擊效果的實(shí)例代碼
這篇文章主要介紹了Android開發(fā)返回鍵明暗點(diǎn)擊效果的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
一篇文章弄懂Android自定義viewgroup的相關(guān)難點(diǎn)
這篇文章主要給大家介紹了關(guān)于如何通過(guò)一篇文章弄懂Android中自定義viewgroup的一些相關(guān)難點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
Android RadioGroup和RadioButton控件簡(jiǎn)單用法示例
這篇文章主要介紹了Android RadioGroup和RadioButton控件簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了Android單選按鈕控件的基本定義、布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07
Android7.0以上Uri轉(zhuǎn)路徑的方法實(shí)現(xiàn)(已驗(yàn)證)
這篇文章主要介紹了Android7.0以上Uri轉(zhuǎn)路徑的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android同時(shí)安裝Release和Debug版本的方法
這篇文章主要介紹了Android同時(shí)安裝Release和Debug版本的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Android響應(yīng)事件onClick方法的五種實(shí)現(xiàn)方式小結(jié)
本篇文章主要介紹了Android響應(yīng)onClick方法的五種實(shí)現(xiàn)方式小結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
TextView實(shí)現(xiàn)跑馬燈效果 就這么簡(jiǎn)單!
TextView實(shí)現(xiàn)跑馬燈效果,就這么簡(jiǎn)單輕松實(shí)現(xiàn),這篇文章介紹了TextView制作跑馬燈效果的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

