編輯:關於 Android 程式設計
當 GridView 需要一次呈現大量圖片時,若直接在主執行緒同步載入,不僅畫面會卡頓,還極容易觸發記憶體溢出(OutOfMemoryError)。本文示範如何以「異步載入 + 圖片縮放」的組合,既保證流暢度,又能有效杜絕 OOM。
private int fore[];
private int back[];
fore = new int[]{R.drawable.a0, R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4,
R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9,
R.drawable.a910, R.drawable.a911, R.drawable.a912, R.drawable.a913, R.drawable.a914};
back = new int[]{R.drawable.b0, R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4,
R.drawable.b5, R.drawable.b6, R.drawable.b7, R.drawable.b8, R.drawable.b9,
R.drawable.b910, R.drawable.b911, R.drawable.b912, R.drawable.b913, R.drawable.b914};
// 用來保存載入的圖片
private List<Bitmap> bitmapResource = new ArrayList<Bitmap>();
GridView grid; // 類似九宮格的圖片顯示控制項
Handler handler; // 用來傳遞訊息
int width = (int) (getWindowManager().getDefaultDisplay().getWidth() / density);// 取得螢幕寬度
ImageResource ir = new ImageResource();
public void getPicture() {
new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... params) {
// 背景執行,耗時操作放在這裡
publishProgress(); // 更新任務進度
loadingBitmap(getResources(), width, 3); // 縮放圖片,3 為顯示列數
return null;
}
@Override
protected void onPostExecute(Object result) {
// doInBackground 完成後,此方法在 UI 執行緒被呼叫
handler.removeCallbacks(update);
message.setVisibility(View.GONE);
author_message.setVisibility(View.GONE);
grid.setVisibility(View.VISIBLE);
grid.setNumColumns(3); // 設定列數
grid.setHorizontalSpacing(20); // 欄間距
grid.setVerticalSpacing(40); // 列間距
grid.setAdapter(adapter);
grid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent intent = new Intent();
intent.setClass(MenuActivity.this, ShowActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("num", position); // 傳遞位置值
intent.putExtras(bundle);
MenuActivity.this.startActivity(intent);
}
});
adapter.notifyDataSetChanged(); // 通知 UI 依新資料重繪
super.onPostExecute(result);
}
protected void onProgressUpdate(Object... values) {
handler = new Handler();
handler.post(update);
super.onProgressUpdate(values);
}
}.execute();
}
public void loadingBitmap(Resources resources, int width, int num) {
BitmapFactory.Options opts = new BitmapFactory.Options();
// 僅回傳圖片寬高,不佔用太多記憶體,也就不會那麼頻繁發生 OOM
opts.inJustDecodeBounds = true; // true 時不分配記憶體,只取得圖片大小等解碼邊界資訊
Bitmap temp = BitmapFactory.decodeResource(resources, fore[0], opts);
// 向上取整得到縮放比例 radio
int radio = (int) Math.ceil(opts.outWidth / (width * 1.0 / num - 30));
// Math.ceil(12.2) → 13;Math.ceil(12.7) → 13;Math.ceil(12.0) → 12
opts.inSampleSize = radio; // 縮略圖大小為原圖的幾分之一
if (null != temp) {
temp.recycle(); // 回收
}
opts.inJustDecodeBounds = false; // false 時才能依縮放比例載入想要的圖片
for (int i = 0; i < fore.length; i++) {
Bitmap bitmap = BitmapFactory.decodeResource(resources, fore[i], opts);
bitmapResource.add(bitmap); // 逐一加入集合
}
}
BaseAdapter adapter = new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(MenuActivity.this);
iv.setMaxWidth(width / 3 - 30); // 設定寬度
iv.setAdjustViewBounds(true); // 保持寬高比
iv.setImageBitmap(ir.getIconBitmap(position));
return iv;
}
@Override public long getItemId(int position) { return position; }
@Override public Object getItem(int arg0) { return arg0; }
@Override public int getCount() { return ir.size(); }
};
進度更新則透過 Handler 的 postDelayed 週期性觸發,載入完成(進度 100%)後立即停止:
Runnable update = new Runnable() {
@Override
public void run() {
int progress = ir.getProgress();
if (null != message) {
message.setText("數據加載中(" + progress + "%),請稍等……\n\n");
}
if (100 == progress) {
handler.removeCallbacks(update); // 載入完畢,關閉定時器
} else {
handler.postDelayed(update, 200); // 每 0.2 秒觸發一次
}
}
};
Android 設定頁面設計
設定頁面是許多應用必備的畫面,常見元素包括帳號資訊、通知開關、隱私選項、清除快取與關於頁面。設計時可透過 LinearLayout 或 RecyclerView 組合多個
安卓 APK 瘦身實戰:把安裝包砍掉四分之一
以前打包就是 Eclipse 或 Android Studio 直接生成簽名文件,從沒關注過體積問題。直到有人問我「你有做過 APK 瘦身嗎」,才發現這塊一直是空白。折騰
Android Studio 使用 SVN 的注意事項
許多團隊仍使用 SVN 進行版本控制。在 Android Studio 中設定 SVN 並不難,但仍有幾個實務細節需要注意,包括命令列用戶端設定、忽略檔案、專案分享與衝突
Android App 線上更新的完整實作流程
線上更新是幾乎每個 App 都需要的功能。本文以一個完整的 Splash 啟動頁為例,把「檢查版本 → 提示更新 → 下載 APK → 觸發安裝」這條鏈路走一遍。 一、