Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 大量圖片加載與 SoftReference 快取的實測分析

編輯:關於 Android 程式設計

這是改進後的版本,我實測可以加載到五萬張圖片,判斷五萬以上應該也沒問題——不過現實中大概一千張就很夠用了,我的專案其實到一百張就差不多。以下用不同載入方式做對比,順便示範如何用軟引用做快取。

package com.lanlong.test;

import java.io.File;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class AndrodTestActivity extends Activity {
    int number = 50000;
    Drawable[] array;
    Bitmap[] array2;
    private Map<String, SoftReference<Drawable>> imageCache =
            new HashMap<String, SoftReference<Drawable>>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String iconPath = Environment.getExternalStorageDirectory()
                + "/vpn/Bafangtong/image/weixin.png";

        array = new Drawable[number]; // BitmapDrawable 一樣
        array2 = new Bitmap[number];

        File mfile = new File(iconPath);
        Log.i("", "mfile.exists() = " + mfile.exists());

        Drawable draw = null;
        long timeBegin = System.currentTimeMillis();

        for (int i = 0; i < number; i++) {
            Log.e("", "測試第" + (i + 1) + "張圖片");
            // 各種載入方式的耗時(供對照):
            // getResources().getDrawable:1000 ok
            // BitmapFactory.decodeFile:max 222
            // Drawable.createFromPath:max 221
            // BitmapDrawable.createFromPath:max 221
            // Drawable.createFromStream:max 111
            // BitmapFactory.decodeStream:max 221
            addDrawableToCache("" + i, iconPath);
            draw = getDrawableByPath("" + i, iconPath);
            Log.i("", "draw = " + draw);
        }

        long timeEnd = System.currentTimeMillis();
        Log.v("", "time = " + (timeEnd - timeBegin) / 1000 + " second");
    }

    public void addDrawableToCache(String id, String path) {
        // 強引用的 Drawable 物件
        Drawable drawable = Drawable.createFromPath(path);
        // 用軟引用包裝
        SoftReference<Drawable> softDrawable = new SoftReference<Drawable>(drawable);
        // 加入 Map 中作為快取
        imageCache.put(id, softDrawable);
        Log.w("", "add, imageCache.size() = " + imageCache.size());
    }

    public Drawable getDrawableByPath(String id, String path) {
        // 從快取中取出軟引用的 Drawable
        SoftReference<Drawable> softDrawable = imageCache.get(id);
        if (softDrawable == null) {
            return null;
        }
        // 取出 Drawable;若因記憶體不足被回收,將取得 null
        Drawable drable = softDrawable.get();
        return drable;
    }
}

為什麼要用 SoftReference?

圖片是記憶體大戶,大量載入時極易觸發 OutOfMemoryError。SoftReference(軟引用)的特性是:當記憶體充足時,它持有的物件會被保留;一旦系統記憶體吃緊,垃圾回收器就會主動回收這些軟引用物件,而不會等到 OOM 才崩潰。因此把它用作圖片快取,能在「快取命中」與「記憶體安全」之間取得良好平衡,讓應用即使載入成千上萬張圖片也能穩定運行。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved