Android Volley ImageLoader - BitmapLruCache参数?

时间:2013-05-22 02:37:12

标签: android image-loading bitmapcache android-volley

我无法使用新的Volley库实现Image缓存。在演示文稿中,代码看起来像这样

mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());

BitmapLruCache显然不包含在工具包中。知道如何实现它或指向一些资源吗?

http://www.youtube.com/watch?v=yhv8l9F44qo @ 14:38

谢谢!

5 个答案:

答案 0 :(得分:49)

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
    public static int getDefaultLruCacheSize() {
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;

        return cacheSize;
    }

    public BitmapLruCache() {
        this(getDefaultLruCacheSize());
    }

    public BitmapLruCache(int sizeInKiloBytes) {
        super(sizeInKiloBytes);
    }

    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight() / 1024;
    }

    @Override
    public Bitmap getBitmap(String url) {
        return get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        put(url, bitmap);
    }
}

答案 1 :(得分:7)

Ficus为Bitmap LRU提供了这个示例代码:

https://gist.github.com/ficusk/5614325

答案 2 :(得分:5)

以下是使用Volley的基于磁盘的LRU缓存的示例。它基于使用由Jake Wharton维护的AOSP的DiskLruCache版本。 http://blogs.captechconsulting.com/blog/raymond-robinson/google-io-2013-volley-image-cache-tutorial

编辑:我已更新项目以包含内存中的LRU缓存作为默认实现,因为这是推荐的方法。 Volley在其自己的L2缓存中隐式处理基于磁盘的缓存。图像缓存只是L1缓存。我更新了原始帖子,并在此处添加了更多详细信息:http://www.thekeyconsultant.com/2013/06/update-volley-image-cache.html

答案 3 :(得分:1)

我建议使用Singleton位图缓存,以便在应用程序的整个生命周期内都可以使用此缓存。

public class BitmapCache implements ImageCache {
    private LruCache<String, Bitmap> mMemoryCache;

    private static BitmapCache mInstance;

    private BitmapCache(Context ctx) {
        final int memClass = ((ActivityManager) ctx
                .getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
        // Use 1/16th of the available memory for this memory cache.
        final int cacheSize = 1024 * 1024 * memClass / 16;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();
            }
        };
    }

    public static BitmapCache getInstance(Context ctx) {
        if (mInstance == null) {
            mInstance = new BitmapCache(ctx);
        }
        return mInstance;
    }

    @Override
    public Bitmap getBitmap(String url) {
        return mMemoryCache.get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        mMemoryCache.put(url, bitmap);
    }
}

答案 4 :(得分:0)

这是用于处理OOM的新API

public class BitmapMemCache extends LruCache<string, Bitmap> implements ImageCache {

    public BitmapMemCache() {
        this((int) (Runtime.getRuntime().maxMemory() / 1024) / 8);
    }

    public BitmapMemCache(int sizeInKiloBytes) {
        super(sizeInKiloBytes);
    }

    @Override
    protected int sizeOf(String key, Bitmap bitmap) {
        int size = bitmap.getByteCount() / 1024;
        return size;
    }

    public boolean contains(String key) {
        return get(key) != null;
    }

    public Bitmap getBitmap(String key) {
        Bitmap bitmap = get(key);
        return bitmap;
    }

    public void putBitmap(String url, Bitmap bitmap) {
        put(url, bitmap);
    }
}