詳細講解Android圖片下載架構UniversialImageLoader之記憶體緩衝擴充(四),

來源:互聯網
上載者:User

詳細講解Android圖片下載架構UniversialImageLoader之記憶體緩衝擴充(四),

           記憶體緩衝的擴充還是蠻重要的,無論是資料結構還是具體的實現還是值得我們進行細細的品味,下面咱們就一起能品味這裡面的趣味吧。

           記憶體緩衝的擴充主要學習下面的幾個類:FIFOLimitedMemoryCache、FuzzyKeyMemoryCache、LargestLimitedMemoryCache、LimitedAgeMemoryCache、LRULimitedMemoryCache、LruMemoryCache、UsingFreqLimitedMemoryCache以及WeakMemoryCache。

           Ok,首先我們學習LimitedMemoryCache這個類。LimitedMemoryCache是LimitedMemoryCache的子類,其成員變數如下:

private final List<Bitmap> queue = Collections.synchronizedList(new LinkedList<Bitmap>());
由此可知記憶體緩衝的資料結構是安全執行緒的隊列。

分析其函數,我們知道隊列的操作在這裡也是可以得到對應的體現。只是建立軟引用的方法還是可以觀察一下。

@Overrideprotected Reference<Bitmap> createReference(Bitmap value) {return new WeakReference<Bitmap>(value);}
          接下來,需要分析的第二個類是FuzzyKeyMemoryCache,按照類的注釋,這個類是架構內部使用的,它實現了MemoryCache這個介面。當前的類的成員變數如下:

private final MemoryCache cache;private final Comparator<String> keyComparator;

由MemoryCache這個成員變數並且是在當前的建構函式中初始化可知,這個類是個封裝器,真正的功能是由cache實現的。

接下來我們要分析的類是LargestLimitedMemoryCache,這個類依舊是LimitedMemoryCache的子類,

其成員變數如下:

private final Map<Bitmap, Integer> valueSizes = Collections.synchronizedMap(new HashMap<Bitmap, Integer>());
由此可見,當前的緩衝的資料結構已經發生了變化,當前已經是HashMap,HashMa的優勢在於刪除離散的資料的效率高。

重點關注一下其刪除對象的方法:

@Overrideprotected Bitmap removeNext() {Integer maxSize = null;Bitmap largestValue = null;Set<Entry<Bitmap, Integer>> entries = valueSizes.entrySet();synchronized (valueSizes) {for (Entry<Bitmap, Integer> 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;}
由此可見,迭代擷取最大的圖片,並且進行刪除。


接下來需要分析的類是LimitedAgeMemoryCache,

繼續關注當前的成員變數:

private final MemoryCache cache;private final long maxAge;private final Map<String, Long> loadingDates = Collections.synchronizedMap(new HashMap<String, Long>());

可見當前的成員變數loadingDates,當前的是HashMap。

重點關注下面的成員變數

@Overridepublic Bitmap get(String key) {Long loadingDate = loadingDates.get(key);if (loadingDate != null && System.currentTimeMillis() - loadingDate > maxAge) {cache.remove(key);loadingDates.remove(key);}return cache.get(key);}

如果當前的檔案已經達到刪除的時間,就直接從緩衝的資料結構中刪除當前的對象。


由於比較多,這一講就到這裡停筆了,後面剩餘的幾個類將會在後面的文章中講解。





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.