馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
手機可用的Memory空間真的很小, 基本上載入數張圖片就會不幸滿溢
所以要好好管理, 而當中最主要的當然是圖片, 動輒用MB計的。
所以分享一下這段簡單的程式碼,
讓大家避免再因為用盡16MB允許空間而陣亡。
package com.tommiAndroid.WhatSaid;
import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;
import android.graphics.Bitmap;
public class BitmapManagement {
//board
private static List<WeakReference<Bitmap>> weakReferences = new LinkedList<WeakReference<Bitmap>>();
//method
public static void add(Bitmap bitmap) {
if (bitmap.isRecycled() == false) {
WeakReference<Bitmap> weakReference = get(bitmap);
if (weakReference == null)
weakReferences.add(new WeakReference<Bitmap>(bitmap));
}
}
private static WeakReference<Bitmap> get(Bitmap bitmap) {
for (WeakReference<Bitmap> weakReference : weakReferences)
if (weakReference.get() == bitmap)
return weakReference;
return null;
}
public static void recycle(Bitmap bitmap) {
if (bitmap.isRecycled() == false)
bitmap.recycle();
WeakReference<Bitmap> weakReference = get(bitmap);
if (weakReference != null)
weakReferences.remove(weakReference);
}
public static int byteCount() {
int byteCount = 0;
for (WeakReference<Bitmap> weakReference : weakReferences) {
Bitmap bitmap = weakReference.get();
if (bitmap != null
&& bitmap.isRecycled() == false)
byteCount += bitmap.getByteCount();
}
return byteCount;
}
} |
|