缓存Bitmap

来源:互联网 发布:网上发布软件 编辑:程序博客网 时间:2024/05/17 01:38

加载一个Bitmap(位图)到你的UI界面是非常简单的,但是如果你要一次加载一大批,事情就变得复杂多了。在大多数的情况下(如ListView、GridView或者ViewPager这样的组件),屏幕上的图片以及马上要在滚动到屏幕上显示的图片的总量,在本质上是不受限制的。

        像这样的组件在子视图移出屏幕后会进行视图回收,内存使用仍被保留。但假设你不保留任何长期存活的引用,垃圾回收器也会释放你所加载的Bitmap。这自然再好不过了,但是为了保持流畅且快速加载的UI,你要避免继续在图片回到屏幕上的时候重新处理。使用内存和硬盘缓存通常能解决这个问题,使用缓存允许组件快速加载并处理图片。

        这节课将带你使用内存和硬盘缓存Bitmap,以在加载多个Bitmap的时候提升UI的响应性和流畅性。

使用内存缓存

        以牺牲宝贵的应用内存为代价,内存缓存提供了快速的Bitmap访问方式。LruCache类(可以在Support Library中获取并支持到API  Level 4以上,即1.6版本以上)是非常适合用作缓存Bitmap任务的,它将最近被引用到的对象存储在一个强引用的LinkedHashMap中,并且在缓存超过了指定大小之后将最近不常使用的对象释放掉。

        注意:以前有一个非常流行的内存缓存实现是SoftReference(软引用)或者WeakReference(弱引用)的Bitmap缓存方案,然而现在已经不推荐使用了。自Android2.3版本(API Level 9)开始,垃圾回收器更着重于对软/弱引用的回收,这使得上述的方案相当无效。此外,Android 3.0(API Level 11)之前的版本中,Bitmap的备份数据直接存储在本地内存中并以一种不可预测的方式从内存中释放,很可能短暂性的引起程序超出内存限制而崩溃。

        为了给LruCache选择一个合适的大小,要考虑到很多原因,例如:

  • 其他的Activity(活动)和(或)程序都是很耗费内存的吗?
  • 屏幕上一次会显示多少图片?有多少图片将在屏幕上显示?
  • 设备的屏幕大小和密度是多少?一个超高清屏幕(xhdpi)的设备如Galaxy Nexus,相比Nexus S(hdpi)来说,缓存同样数量的图片需要更大的缓存空间。
  • Bitmap的尺寸、配置以及每张图片需要占用多少内存?
  • 图片的访问是否频繁?有些会比其他的更加被频繁的访问到吗?如果是这样,也许你需要将某些图片一直保留在内存中,甚至需要多个LruCache对象分配给不同组的Bitmap。
  • 你能平衡图片的质量和数量么?有的时候存储大量低质量的图片更加有用,然后可以在后台任务中加载另一个高质量版本的图片。

        对于设置缓存大小,并没有适用于所有应用的规范,它取决于你在内存使用分析后给出的合适的解决方案。缓存空间太小并无益处,反而会引起额外的开销,而太大了又可能再次引起java.lang.OutOfMemory异常或只留下很小的空间给应用的其他程序运行。   

        这里有一个设置Bitmap的LruCache示例:

01private LruCache<String, Bitmap> mMemoryCache;
02 
03@Override
04protected void onCreate(Bundle savedInstanceState) {
05    ...
06    // Get memory class of this device, exceeding this amount will throw an
07    // OutOfMemory exception.
08    final int memClass = ((ActivityManager) context.getSystemService(
09            Context.ACTIVITY_SERVICE)).getMemoryClass();
10 
11    // Use 1/8th of the available memory for this memory cache.
12    final int cacheSize = 1024 * 1024 * memClass / 8;
13 
14    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
15        @Override
16        protected int sizeOf(String key, Bitmap bitmap) {
17            // The cache size will be measured in bytes rather than number of items.
18            return bitmap.getByteCount();
19        }
20    };
21    ...
22}
23 
24public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
25    if (getBitmapFromMemCache(key) == null) {
26        mMemoryCache.put(key, bitmap);
27    }
28}
29 
30public Bitmap getBitmapFromMemCache(String key) {
31    return mMemoryCache.get(key);
32}

        注意:在这个例子中,1/8的应用内存被分配给缓存。在一个普通的/hdpi设备上最低也在4M左右(32/8)。一个分辨率为800*480的设备上,全屏的填满图片的GridView占用的内存约1.5M(800*480*4字节),因此这个大小的内存可以缓存2.5页左右的图片。

        当加载一个Bitmap到ImageView中,先要检查LruCache。如果有相应的数据,则立即用来更新ImageView,否则将启动后台线程来处理这个图片。

01public void loadBitmap(int resId, ImageView imageView) {
02    final String imageKey = String.valueOf(resId);
03 
04    final Bitmap bitmap = getBitmapFromMemCache(imageKey);
05    if (bitmap != null) {
06        mImageView.setImageBitmap(bitmap);
07    } else {
08        mImageView.setImageResource(R.drawable.image_placeholder);
09        BitmapWorkerTask task = new BitmapWorkerTask(mImageView);
10        task.execute(resId);
11    }
12}

        BitmapWorkerTask也需要更新内存中的数据:

01class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
02    ...
03    // Decode image in background.
04    @Override
05    protected Bitmap doInBackground(Integer... params) {
06        final Bitmap bitmap = decodeSampledBitmapFromResource(
07                getResources(), params[0], 100, 100));
08        addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
09        return bitmap;
10    }
11    ...
12}

使用硬盘缓存

        一个内存缓存对加速访问最近浏览过的Bitmap非常有帮助,但是你不能局限于内存中的可用图片。GridView这样有着更大的数据集的组件可以很轻易消耗掉内存缓存。你的应用有可能在执行其他任务(如打电话)的时候被打断,并且在后台的任务有可能被杀死或者缓存被释放。一旦用户重新聚焦(resume)到你的应用,你得再次处理每一张图片。

        在这种情况下,硬盘缓存可以用来存储Bitmap并在图片被内存缓存释放后减小图片加载的时间(次数)。当然,从硬盘加载图片比内存要慢,并且应该在后台线程进行,因为硬盘读取的时间是不可预知的。

        注意:如果访问图片的次数非常频繁,那么ContentProvider可能更适合用来存储缓存图片,例如Image Gallery这样的应用程序。

        这个类中的示例代码使用DiskLruCache(来自Android源码)实现。在示例代码中,除了已有的内存缓存,还添加了硬盘缓存。

01private DiskLruCache mDiskLruCache;
02private final Object mDiskCacheLock = new Object();
03private boolean mDiskCacheStarting = true;
04private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
05private static final String DISK_CACHE_SUBDIR = "thumbnails";
06 
07@Override
08protected void onCreate(Bundle savedInstanceState) {
09    ...
10    // Initialize memory cache
11    ...
12    // Initialize disk cache on background thread
13    File cacheDir = getDiskCacheDir(this, DISK_CACHE_SUBDIR);
14    new InitDiskCacheTask().execute(cacheDir);
15    ...
16}
17 
18class InitDiskCacheTask extends AsyncTask<File, Void, Void> {
19    @Override
20    protected Void doInBackground(File... params) {
21        synchronized (mDiskCacheLock) {
22            File cacheDir = params[0];
23            mDiskLruCache = DiskLruCache.open(cacheDir, DISK_CACHE_SIZE);
24            mDiskCacheStarting = false; // Finished initialization
25            mDiskCacheLock.notifyAll(); // Wake any waiting threads
26        }
27        return null;
28    }
29}
30 
31class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
32    ...
33    // Decode image in background.
34    @Override
35    protected Bitmap doInBackground(Integer... params) {
36        final String imageKey = String.valueOf(params[0]);
37 
38        // Check disk cache in background thread
39        Bitmap bitmap = getBitmapFromDiskCache(imageKey);
40 
41        if (bitmap == null) { // Not found in disk cache
42            // Process as normal
43            final Bitmap bitmap = decodeSampledBitmapFromResource(
44                    getResources(), params[0], 100, 100));
45        }
46 
47        // Add final bitmap to caches
48        addBitmapToCache(imageKey, bitmap);
49 
50        return bitmap;
51    }
52    ...
53}
54 
55public void addBitmapToCache(String key, Bitmap bitmap) {
56    // Add to memory cache as before
57    if (getBitmapFromMemCache(key) == null) {
58        mMemoryCache.put(key, bitmap);
59    }
60 
61    // Also add to disk cache
62    synchronized (mDiskCacheLock) {
63        if (mDiskLruCache != null && mDiskLruCache.get(key) == null) {
64            mDiskLruCache.put(key, bitmap);
65        }
66    }
67}
68 
69public Bitmap getBitmapFromDiskCache(String key) {
70    synchronized (mDiskCacheLock) {
71        // Wait while disk cache is started from background thread
72        while (mDiskCacheStarting) {
73            try {
74                mDiskCacheLock.wait();
75            } catch (InterruptedException e) {}
76        }
77        if (mDiskLruCache != null) {
78            return mDiskLruCache.get(key);
79        }
80    }
81    return null;
82}
83 
84// Creates a unique subdirectory of the designated app cache directory. Tries to use external
85// but if not mounted, falls back on internal storage.
86public static File getDiskCacheDir(Context context, String uniqueName) {
87    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
88    // otherwise use internal cache dir
89    final String cachePath =
90            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
91                    !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
92                            context.getCacheDir().getPath();
93 
94    return new File(cachePath + File.separator + uniqueName);
95}

        注意:即便是硬盘缓存初始化也需要硬盘操作,因此不应该在主线程执行。但是,这意味着硬盘缓存在初始化前就能被访问到。为了解决这个问题,在上面的实现中添加了一个锁对象(lock object),以确保在缓存被初始化之前应用无法访问硬盘缓存。

        在UI线程中检查内存缓存,相应的硬盘缓存检查应在后台线程中进行。硬盘操作永远不要在UI线程中发生。当图片处理完成后,最终的Bitmap要被添加到内存缓存和硬盘缓存中,以便后续的使用。

 处理配置更改

        运行时的配置会发生变化,例如屏幕方向的改变,会导致Android销毁并以新的配置重新启动Activity(关于此问题的更多信息,请参阅Handling Runtime Changes)。为了让用户有着流畅而快速的体验,你需要在配置发生改变的时候避免再次处理所有的图片。

        幸运的是,你在“使用内存缓存”一节中为Bitmap构造了很好的内存缓存。这些内存可以通过使用Fragment传递到信的Activity(活动)实例,这个Fragment可以调用setRetainInstance(true)方法保留下来。在Activity(活动)被重新创建后,你可以在上面的Fragment中访问到已经存在的缓存对象,使得图片能快加载并重新填充到ImageView对象中。

        下面是一个使用FragmentLruCache对象保留在配置更改中的示例:

view source
print?
01private LruCache<String, Bitmap> mMemoryCache;
02 
03@Override
04protected void onCreate(Bundle savedInstanceState) {
05    ...
06    RetainFragment mRetainFragment =
07            RetainFragment.findOrCreateRetainFragment(getFragmentManager());
08    mMemoryCache = RetainFragment.mRetainedCache;
09    if (mMemoryCache == null) {
10        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
11            ... // Initialize cache here as usual
12        }
13        mRetainFragment.mRetainedCache = mMemoryCache;
14    }
15    ...
16}
17 
18class RetainFragment extends Fragment {
19    private static final String TAG = "RetainFragment";
20    public LruCache<String, Bitmap> mRetainedCache;
21 
22    public RetainFragment() {}
23 
24    public static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
25        RetainFragment fragment = (RetainFragment) fm.findFragmentByTag(TAG);
26        if (fragment == null) {
27            fragment = new RetainFragment();
28        }
29        return fragment;
30    }
31 
32    @Override
33    public void onCreate(Bundle savedInstanceState) {
34        super.onCreate(savedInstanceState);
35        setRetainInstance(true);
36    }
37}

        为了测试这个,可以在不适用Fragment的情况下旋转设备屏幕。在保留缓存的情况下,你应该能发现填充图片到Activity中几乎是瞬间从内存中取出而没有任何延迟的感觉。任何图片优先从内存缓存获取,没有的话再到硬盘缓存中找,如果都没有,那就以普通方式加载图片。

转至:http://my.oschina.net/ryanhoo/blog/88443


原创粉丝点击