Android Volley ImageLoader - BitmapLruCache parame

2019-01-08 08:36发布

I am having trouble implementing Image cache using the new Volley library. In the presentation, code look like this

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

The BitmapLruCache is obviously not included in the toolkit. Any idea how to implement it or point me to some resources?

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

Thanks!

5条回答
Bombasti
2楼-- · 2019-01-08 09:10
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);
    }
}
查看更多
孤傲高冷的网名
3楼-- · 2019-01-08 09:21

Ficus provides this sample code for the Bitmap LRU:

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

查看更多
何必那么认真
4楼-- · 2019-01-08 09:24

this is comes in new API to handle the OOM

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);
    }
}
查看更多
干净又极端
5楼-- · 2019-01-08 09:29

Here is an example of using a Disk based LRU cache with Volley. It is based on using a version of AOSP's DiskLruCache maintained by Jake Wharton. http://blogs.captechconsulting.com/blog/raymond-robinson/google-io-2013-volley-image-cache-tutorial

Edit: I have updated the project to include an in-memory LRU cache as the default implementation as this is the recommended method. Volley implicitly handles disk based cache in its own L2 cache. The image cache is just the L1 cache. I updated the original post and added some more detail here: http://www.thekeyconsultant.com/2013/06/update-volley-image-cache.html.

查看更多
够拽才男人
6楼-- · 2019-01-08 09:31

What I advise is using a Singleton Bitmap Cache so this cache is going to be available during all the life of your app.

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);
    }
}
查看更多
登录 后发表回答