Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開源框架Universal-Image-Loader學習四——LimitedMemoryCache的一些子集

Android開源框架Universal-Image-Loader學習四——LimitedMemoryCache的一些子集

編輯:關於Android編程

LRULimitedMemoryCache源碼:

 

/**
 * (cache size limited)Size of all stored bitmaps will not to exceed size limit.
 * (刪除策略LRU)When cache reaches limit size then the least recently used bitmap is deleted from cache.
 * NOTE: This cache uses strong and weak references for stored Bitmaps. 
 * 限定值內的使用強引用
 */
public class LRULimitedMemoryCache extends LimitedMemoryCache {
 
    private static final int INITIAL_CAPACITY = 10;
    private static final float LOAD_FACTOR = 1.1f;
 
    /** LRU */
    private final Map lruCache = Collections.synchronizedMap(new LinkedHashMap(INITIAL_CAPACITY, LOAD_FACTOR, true));
 
    /**構造函數 */
    public LRULimitedMemoryCache(int maxSize) {
        super(maxSize);
    }
 
    @Override
    public boolean put(String key, Bitmap value) {
        if (super.put(key, value)) {
            lruCache.put(key, value);
            return true;
        } else {
            return false;
        }
    }
 
    @Override
    public Bitmap get(String key) {
        lruCache.get(key); // call "get" for LRU logic
        return super.get(key);
    }
 
    @Override
    public Bitmap remove(String key) {
        lruCache.remove(key);
        return super.remove(key);
    }
 
    @Override
    public void clear() {
        lruCache.clear();
        super.clear();
    }
 
    @Override
    protected int getSize(Bitmap value) {
        return value.getRowBytes() * value.getHeight();
    }
 
    @Override
    protected Bitmap removeNext() {
        Bitmap mostLongUsedValue = null;
        synchronized (lruCache) {
            Iterator> it = lruCache.entrySet().iterator();
            if (it.hasNext()) {
                Entry entry = it.next();
                mostLongUsedValue = entry.getValue();
                it.remove();
            }
        }
        return mostLongUsedValue;
    }
 
    @Override
    protected Reference createReference(Bitmap value) {
        return new WeakReference(value);
    }
}

FIFOLimitedMemoryCache源碼:

 

 

/**
 * Limited {@link Bitmap bitmap} cache. Provides {@link Bitmap bitmaps} storing. Size of all stored bitmaps will not to
 * exceed size limit. 
 * 刪除策略:FIFO
 * NOTE:This cache uses strong and weak references for stored Bitmaps. Strong references - for limited count of
 * Bitmaps (depends on cache size), weak references - for all other cached Bitmaps.
 */
public class FIFOLimitedMemoryCache extends LimitedMemoryCache {
 
    private final List queue = Collections.synchronizedList(new LinkedList());
 
    public FIFOLimitedMemoryCache(int sizeLimit) {
        super(sizeLimit);
    }
 
    @Override
    public boolean put(String key, Bitmap value) {
        if (super.put(key, value)) {
            queue.add(value);
            return true;
        } else {
            return false;
        }
    }
 
    @Override
    public Bitmap remove(String key) {
        Bitmap value = super.get(key);
        if (value != null) {
            queue.remove(value);
        }
        return super.remove(key);
    }
 
    @Override
    public void clear() {
        queue.clear();
        super.clear();
    }
 
    @Override
    protected int getSize(Bitmap value) {
        return value.getRowBytes() * value.getHeight();
    }
 
    @Override
    /*刪除策略:刪除頭結點,即FIFO*/
    protected Bitmap removeNext() {
        return queue.remove(0);
    }
 
    @Override
    protected Reference createReference(Bitmap value) {
        return new WeakReference(value);
    }
}

 

 

LargestLimitedMemoryCache源碼:
/**
 * Limited {@link Bitmap bitmap} cache. Provides {@link Bitmap bitmaps} storing. Size of all stored bitmaps will not to
 * exceed size limit. 
 * 刪除策略: 刪除size最大的bitmap
 * NOTE:This cache uses strong and weak references for stored Bitmaps. Strong references - for limited count of
 * Bitmaps (depends on cache size), weak references - for all other cached Bitmaps
 */
public class LargestLimitedMemoryCache extends LimitedMemoryCache {
   
    private final Map valueSizes = Collections.synchronizedMap(new HashMap());
 
    public LargestLimitedMemoryCache(int sizeLimit) {
        super(sizeLimit);
    }
 
    @Override
    public boolean put(String key, Bitmap value) {
        if (super.put(key, value)) {
            valueSizes.put(value, getSize(value));
            return true;
        } else {
            return false;
        }
    }
 
    @Override
    public Bitmap remove(String key) {
        Bitmap value = super.get(key);
        if (value != null) {
            valueSizes.remove(value);
        }
        return super.remove(key);
    }
 
    @Override
    public void clear() {
        valueSizes.clear();
        super.clear();
    }
 
    @Override
    protected int getSize(Bitmap value) {
        return value.getRowBytes() * value.getHeight();
    }
 
    @Override
    /*刪除策略:遍歷map查找最大值*/
    protected Bitmap removeNext() {
        Integer maxSize = null;
        Bitmap largestValue = null;
        Set> entries = valueSizes.entrySet();
        synchronized (valueSizes) {
            for (Entry entry : entries) {
                if (largestValue == null) {
                    largestValue = entry.getKey();
                    maxSize = entry.getValue();
                } else {
                    Integer size = entry.getValue();
                    if (size > maxSize) {
                        maxSize = size;
                        largestValue = entry.getKey();
                    }
                }
            }
        }
        valueSizes.remove(largestValue);
        return largestValue;
    }
 
    @Override
    protected Reference createReference(Bitmap value) {
        return new WeakReference(value);
    }
}


 

 

 

 

 

 

 

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