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

來源:互聯網
上載者:User

標籤:

  前面的兩篇文章著重介紹的是磁碟緩衝,這篇文章主要是講解一下記憶體緩衝。對於記憶體緩衝,也打算分兩篇文章來進行講解。在這一篇文章中,我們主要是關注三個類,

MemoryCache、BaseMemoryCache以及LimitedMemoryCache。

    首先我們先看一下記憶體緩衝的介面MemoryCache。

[java] view plaincopyprint?
  1. put(String key, Bitmap value);  
  2. Bitmap get(String key);  
  3. Bitmap remove(String key);  
  4. Collection<String> keys();  
  5. void clear();  


從上面可以看出,整體的介面的設計分為5個方法,1、存入真正的Bitmap  2、通過鍵擷取Bitmap  3、通過鍵刪除Bitmap  4、迭代擷取所有的鍵的集合  5、清空記憶體的緩衝。


 接下來我們看實現記憶體緩衝的介面的抽象類別BaseMemoryCache。

與前面的文章一樣,還是先從變數入手。

[java] view plaincopyprint?
  1. /** Stores not strong references to objects */  
  2.     private final Map<String, Reference<Bitmap>> softMap = Collections.synchronizedMap(new HashMap<String, Reference<Bitmap>>());  


正如所說的那樣,這個變數是儲存非強引用的對象。

稍微的關注一下下面的方法

[java] view plaincopyprint?
  1. @Override  
  2.     public Bitmap get(String key) {  
  3.         Bitmap result = null;  
  4.         Reference<Bitmap> reference = softMap.get(key);  
  5.         if (reference != null) {  
  6.             result = reference.get();  
  7.         }  
  8.         return result;  
  9.     }  


通過鍵擷取軟引用中的數值Bitmap。


最後我們來看一下有限記憶體緩衝空間的緩衝類LimitedMemoryCache,從繼承關係上來看,它是對BaseMemoryCache的進一步擴充。

從變數上來看:

[java] view plaincopyprint?
  1. private static final int MAX_NORMAL_CACHE_SIZE_IN_MB = 16;  
  2. private static final int MAX_NORMAL_CACHE_SIZE = MAX_NORMAL_CACHE_SIZE_IN_MB * 1024 * 1024;  
  3. private final int sizeLimit;  
  4. private final AtomicInteger cacheSize;  
  5. private final List<Bitmap> hardCache = Collections.synchronizedList(new LinkedList<Bitmap>());  


從變數的定義來看,包括最大的緩衝的限制,當前的緩衝的尺寸以及強引用對象的集合。

拿其中的對象的儲存的方法來分析一下:

[java] view plaincopyprint?
  1. public boolean put(String key, Bitmap value) {  
  2.         boolean putSuccessfully = false;  
  3.         // Try to add value to hard cache  
  4.         int valueSize = getSize(value);  
  5.         int sizeLimit = getSizeLimit();  
  6.         int curCacheSize = cacheSize.get();  
  7.         if (valueSize < sizeLimit) {  
  8.             while (curCacheSize + valueSize > sizeLimit) {  
  9.                 Bitmap removedValue = removeNext();  
  10.                 if (hardCache.remove(removedValue)) {  
  11.                     curCacheSize = cacheSize.addAndGet(-getSize(removedValue));  
  12.                 }  
  13.             }  
  14.             hardCache.add(value);  
  15.             cacheSize.addAndGet(valueSize);  
  16.   
  17.             putSuccessfully = true;  
  18.         }  
  19.         // Add value to soft cache  
  20.         super.put(key, value);  
  21.         return putSuccessfully;  
  22.     }  


很明顯,在緩衝圖片的時候,先需要判斷一下當前的圖片的加入有沒有超過整體的緩衝的記憶體的尺寸的限制。如果超過,先根據不同的策略,刪除優先需要刪除的圖片,如果合適,當前的圖片插入,如果不合適,繼續迭代。


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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.