編輯:關於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 MaplruCache = 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); } }
/** * 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 Listqueue = 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 MapvalueSizes = 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); } }
上一篇我們學習了自定義ViewGroup的基本步驟,並做了一個CustomGridLayout的實例,這篇我們繼續來說說自定義ViewGroup。 Android中當有大
1、產生背景: 為了調試方便,常常希望通過知道apk的路徑,直接去啟動它,所以就寫了這個命令 2、使用方法: adb push launch /data/ adb pus
Android應用開發中,采用ListView組件來展示數據是很常用的功能,當一個應用要展現很多的數據時,一般情況下都不會把所有的數據一次就展示出來,而是通過分頁的形式來
oppo R9/plus用什麼電話卡?有買到oppo R9的用戶,如果不知道自己的手機sim卡是否適合,可以看看本文。oppo R9作為oppo手機的一款旗