編輯:關於 Android 開發
幾乎每個 Android 專案都會用到「取得螢幕尺寸」這類基礎操作——無論是計算控件的動態位置,還是做自適應排版。與其在各處重複寫這些瑣碎程式碼,不如把它們統一封裝到一個 ScreenUtils 工具類裡。下面這份工具類涵蓋了螢幕寬高、狀態欄高度以及螢幕截圖(含/不含狀態欄)等常用功能,隨取隨用。
//獲得屏幕相關的輔助類
public class ScreenUtils {
private ScreenUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 獲得屏幕寬度
*/
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;
}
/**
* 獲得屏幕高度
*/
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;
}
/**
* 獲得狀態欄的高度
*/
public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen.xml");
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;
}
/**
* 獲取當前屏幕截圖,包含狀態欄
*/
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 = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* 獲取當前屏幕截圖,不包含狀態欄
*/
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 = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return bp;
}
}
使用上有幾個小提醒:構造方法被私有化並拋出異常,是為了禁止它被實例化,純工具類保持全靜態調用即可;取得螢幕寬高透過 WindowManager 加 DisplayMetrics 就能拿到;狀態欄高度則是透過反射讀取系統內建的 status_bar_height 資源,並做好異常兜底;至於截屏,核心思路是開啟 DrawingCache 再從中裁切出對應區域,不含狀態欄時只需從狀態欄高度處開始截取即可。把這些常用能力沉澱下來,專案裡各處直接調用,能省下不少重複勞動。
Android ListView 圖片載入優化:避免 OOM 與圖片錯位
Android ListView 圖片載入優化:避免 OOM 與圖片錯位 在 ListView 中載入網路圖片,常見問題是記憶體不足與圖片錯位。若每個項目都直接下載並建立
Android 自動完成輸入框 AutoCompleteTextView 使用教學
輸入前幾個字就自動補全 AutoCompleteTextView 能在使用者輸入單詞或句子的前幾個字時,自動彈出匹配的候選內容,大幅減少輸入。效果如下: 一、MainA
用隨身碟安裝 Ubuntu 系統:dd 與 Win32DiskImager 製作開機碟實戰
用隨身碟安裝 Ubuntu 系統:dd 與 Win32DiskImager 製作開機碟實戰 想用隨身碟安裝 Ubuntu,第一步當然是製作一片 Ubuntu 的隨身碟安裝
Android Studio 項目提交與更新到 GitHub 的完整步驟
Android Studio 項目提交與更新到 GitHub 的完整步驟 使用 Android Studio 開發時,若想把專案同步到 GitHub,不必總是依賴命令列。