Android 台灣中文網

標題: 管理圖片 [打印本頁]

作者: tommybbee    時間: 2013-9-2 12:28
標題: 管理圖片
手機可用的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;
        }
}
作者: mtcr1    時間: 2013-10-25 19:23
請指教,請問要如何使用?




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1