码迷,mamicode.com
首页 > 移动开发 > 详细

Android开源框架Universal-Image-Loader学习无——WeakMemoryCache 和 FuzzyKeyMemoryCache

时间:2015-05-03 12:05:32      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

WeakMemoryCache 源码:

BaseMemoryCache参见:http://blog.csdn.net/woliuyunyicai/article/details/45330867

/**
 * Memory cache with {@linkplain WeakReference weak references} to {@linkplain android.graphics.Bitmap bitmaps}<br />
 * <br />
 * <b>NOTE:</b> This cache uses only weak references for stored Bitmaps.
 *
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 * @since 1.5.3
 */
public class WeakMemoryCache extends BaseMemoryCache {
    @Override
    protected Reference<Bitmap> createReference(Bitmap value) {
        return new WeakReference<Bitmap>(value);
    }
}
FuzzyKeyMemoryCache源码:
/**
 * MemoryCache的装饰者模式。为cache提供一个特殊功能:(使用Comparator)使得一些不同的keys被当做是等价的。当使用key put一些值到cache中
 * 具有“相同”意义的keys将会先被移除(一般不会用到该class)
 * NOTE: Used for internal needs. Normally you don't need to use this class.
 */
public class FuzzyKeyMemoryCache implements MemoryCache {
 
    private final MemoryCache cache;
    private final Comparator<String> keyComparator;
 
    public FuzzyKeyMemoryCache(MemoryCache cache, Comparator<String> keyComparator) {
        this.cache = cache;
        this.keyComparator = keyComparator;
    }
 
    @Override
    public boolean put(String key, Bitmap value) {
        // Search equal key and remove this entry
        synchronized (cache) {
            String keyToRemove = null;
            for (String cacheKey : cache.keys()) {
                if (keyComparator.compare(key, cacheKey) == 0) {
                    keyToRemove = cacheKey;
                    break;
                }
            }
            if (keyToRemove != null) {
                cache.remove(keyToRemove);
            }
        }
        return cache.put(key, value);
    }
 
    @Override
    public Bitmap get(String key) {
        return cache.get(key);
    }
 
    @Override
    public Bitmap remove(String key) {
        return cache.remove(key);
    }
 
    @Override
    public void clear() {
        cache.clear();
    }
 
    @Override
    public Collection<String> keys() {
        return cache.keys();
    }
}



Android开源框架Universal-Image-Loader学习无——WeakMemoryCache 和 FuzzyKeyMemoryCache

标签:

原文地址:http://blog.csdn.net/woliuyunyicai/article/details/45457127

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!