Android Volley ImageLoader - BitmapLruCache参数?

urS*_*Sus 35 android image-loading bitmapcache android-volley

我在使用新的Volley库实现Image缓存时遇到了麻烦.在演示文稿中,代码看起来像这样

mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢!

Vla*_*nov 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @ njzk2防止`int`溢出.`Runtime.maxMemory()`返回`long`并且转换为`int`可以导致溢出.该部门只是为了尽量减少可能性. (2认同)

Kei*_*ith 7

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

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


小智 5

以下是使用基于磁盘的LRU缓存和Volley的示例.它基于使用由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.