Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開源框架Universal-Image-Loader學習無——WeakMemoryCache 和 FuzzyKeyMemoryCache

Android開源框架Universal-Image-Loader學習無——WeakMemoryCache 和 FuzzyKeyMemoryCache

編輯:關於Android編程

 

 

 

/**
 * Memory cache with {@linkplain WeakReference weak references} to {@linkplain android.graphics.Bitmap bitmaps}

 * 

 * NOTE: 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 createReference(Bitmap value) {
        return new WeakReference(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 keyComparator;
 
    public FuzzyKeyMemoryCache(MemoryCache cache, Comparator 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 keys() {
        return cache.keys();
    }
}


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved