使用内存缓存:
内存缓存bitmaps读写快,但占用应用的运行内存。LruCache类就可以很好的优化占内存的问题。将最近使用的对象存于强引用LinkedHashMap中,当超出指定内存时,回收最近最少用到的对象。
注意:之前,有种比较流行的缓存实现方法是用软引用或弱引用来实现,但现在并不建议使用。因为从Android2.3开始,垃圾回收机制就更积极地回收软引用和弱引用对象。另外,Android3.0之前,bitmap的数据是存在本地内存上的,很难释放,所以容易引起内存溢出。
为了选择一个合适的大小给LruCache,应该考虑一下几个因素:
①我们的Activity或Application还剩多少内存可用?
②一屏一次需要显示多少图片,有多少图片正准备显示?
③屏幕尺寸多少,设备密度多少(屏幕分辨率多少)?高密度的手机需要保存更大的缓存。
④bitmap对象的规格和配置参数是什么,从而每个bitmap占多大内存?
⑤这些图片的访问频率如何?是否有些图片比其他图片访问得更频繁?如果是,那么就需要一直缓存这部分图片或者用多个LruCache对象将BitMaps分组。
⑥怎么平衡质量和数量?有时存储更多低质量的图片更有用,高质量的图片可以另起后台任务去加载。
没有一种合适的大小或者公式适应所有应用,这需要靠自己分析判断。
下面是一个示例:
private LruCache<String, Bitmap> mMemoryCache; @Override protected void onCreate(Bundle savedInstanceState) { ... // Get max available VM memory, exceeding this amount will throw an // OutOfMemory exception. Stored in kilobytes as LruCache takes an // int in its constructor. final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. return bitmap.getByteCount() / 1024; } }; ... } public void addBitmapToMemoryCache(String key, Bitmap bitmap) { if (getBitmapFromMemCache(key) == null) { mMemoryCache.put(key, bitmap); } } public Bitmap getBitmapFromMemCache(String key) { return mMemoryCache.get(key); }注:这里我们使用应用内存的八分之一作为缓存,在一般设备上(hdpi),这个值最小大约4MB(32/8)。在800×480分辨率的设备上,一个全屏的GradView,填满图片后,大约会占1.5MB(800*480*4byte),所以这个大小的缓存至少可以缓存大约两页半的图片。
为什么*4byte呢?应为一个Bitmap占用的内存=长(像素)*宽(像素)*单位像素占字节数。而单位像素占字节数由BitmapFoctory.options的inPreferredConfig决定。inPreferredConfig是Bitmap.Config类型,默认是ARGB_8888。其中ALPHA_8占一个字节;ARGB_8888占4个字节;RGB_565占2个字节。
当加载一个bitmap到ImageView中时,首先检查LruCache中是否有该对象,如果有,则立即使用LruCache中的对象加载图片;如果没有,在异步加载图片。
public void loadBitmap(int resId, ImageView imageView) { final String imageKey = String.valueOf(resId); final Bitmap bitmap = getBitmapFromMemCache(imageKey); if (bitmap != null) { mImageView.setImageBitmap(bitmap); } else { mImageView.setImageResource(R.drawable.image_placeholder); BitmapWorkerTask task = new BitmapWorkerTask(mImageView); task.execute(resId); } }BitmapWorkerTask执行完后,同样也要讲实体添加到内存缓存中。
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> { ... // Decode image in background. @Override protected Bitmap doInBackground(Integer... params) { final Bitmap bitmap = decodeSampledBitmapFromResource( getResources(), params[0], 100, 100)); addBitmapToMemoryCache(String.valueOf(params[0]), bitmap); return bitmap; } ... }
使用磁盘缓存:
内存缓存虽然速度快,但有一点不可靠,如果一个GradView缓存了一页图片,但是来了个电话,这是GradView就在后台运行了,这时候内存缓存就很容易被回收,当GradView再恢复的时候,就得重新下载缓存在内存中的图片了。
所以就用到了磁盘缓存,主要保存到磁盘时需要异步进行。
注:如果缓存的图片访问较频发,ContentProvider可能更适合存放缓存图片。比如一个画廊应用。