Android Volley ImageLoader – BitmapLruCache参数?

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

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

BitmapLruCache显然不包含在工具箱中。 任何想法如何实现它或指向我的一些资源?

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

谢谢!

 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); } } 

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

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

这里是一个使用Volley的基于磁盘的LRUcaching的例子。 它基于使用Jake Wharton维护的AOSP DiskLruCache版本。 http://blogs.captechconsulting.com/blog/raymond-robinson/google-io-2013-volley-image-cache-tutorial

编辑:我已经更新项目,以包含内存LRUcaching作为默认实现,因为这是推荐的方法。 Volley在其自己的L2caching中隐式地处理基于磁盘的caching。 图像caching就是L1caching。 我更新了原来的post,并在这里添加了更多的细节: http : //www.thekeyconsultant.com/2013/06/update-volley-image-cache.html 。

我build议使用单点位图caching,这样这个caching将在你的应用程序的整个生命周期中可用。

 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); } } 

这是来处理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); } }