詳細解讀Volley(三)—— ImageLoader & NetworkImageView

來源:互聯網
上載者:User

標籤:

ImageLoader是一個載入網狀圖片的封裝類,其內部還是由ImageRequest來實現的。但因為源碼中沒有提供磁碟緩衝的設定,所以咱們還需要去源碼中進行修改,讓我們可以更加自如的設定是否進行磁碟緩衝。

 

一、添加對磁碟緩衝的控制

我們默默的開啟源碼,添加如下代碼:

    private boolean mShouldCache = true;    /**     * Set whether or not responses to this request should be cached(Disk Cache).     *     * @return This Request object to allow for chaining.     */    public void setShouldCache(boolean shouldCache) {        mShouldCache = shouldCache;    }        /**     * Returns true if responses to this request should be cached.     */    public final boolean shouldCache() {        return mShouldCache;    }

定位到get方法

public ImageContainer get(String requestUrl, ImageListener imageListener,int maxWidth, int maxHeight) 

找到初始化Request<Bitmap>的地方。

    public ImageContainer get(String requestUrl, ImageListener imageListener,            int maxWidth, int maxHeight) {        // only fulfill requests that were initiated from the main thread.        throwIfNotOnMainThread();        final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);        // Try to look up the request in the cache of remote images.        Bitmap cachedBitmap = mCache.getBitmap(cacheKey);        if (cachedBitmap != null) {            // Return the cached bitmap.            ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);            imageListener.onResponse(container, true);            return container;        }        // The bitmap did not exist in the cache, fetch it!        ImageContainer imageContainer =                new ImageContainer(null, requestUrl, cacheKey, imageListener);        // Update the caller to let them know that they should use the default bitmap.        imageListener.onResponse(imageContainer, true);        // Check to see if a request is already in-flight.        BatchedImageRequest request = mInFlightRequests.get(cacheKey);        if (request != null) {            // If it is, add this request to the list of listeners.            request.addContainer(imageContainer);            return imageContainer;        }        // The request is not already in flight. Send the new request to the network and        // track it.        Request<Bitmap> newRequest = makeImageRequest(requestUrl, maxWidth, maxHeight, cacheKey);        mRequestQueue.add(newRequest);
mInFlightRequests.put(cacheKey,
new BatchedImageRequest(newRequest, imageContainer)); return imageContainer; }

把紅色代碼中間添加:newRequest.setShouldCache(mShouldCache);最終效果如下:

     // The request is not already in flight. Send the new request to the network and        // track it.        Request<Bitmap> newRequest = makeImageRequest(requestUrl, maxWidth, maxHeight, cacheKey);        newRequest.setShouldCache(mShouldCache);        mRequestQueue.add(newRequest);

 

二、ImageLoader

    /**     * Constructs a new ImageLoader.     * @param queue The RequestQueue to use for making image requests.     * @param imageCache The cache to use as an L1 cache.     */    public ImageLoader(RequestQueue queue, ImageCache imageCache) {        mRequestQueue = queue;        mCache = imageCache;    }

初始化要傳入兩個參數:①RequestQueue對象;②ImageCache對象(不能傳null!!!!)

RequestQueue這個我就不多說了,之前的文章已經講解過了,下面來說說ImageCache這個對象。

 

2.1 建立ImageCache對象來實現記憶體緩衝

ImageCache是一個圖片的記憶體緩衝對象,源碼中叫做L1緩衝,其實緩衝分為L1、L2兩種,L1就是所謂的記憶體緩衝,將展示過的圖片放入記憶體中進行緩衝,L2就是磁碟緩衝,如果這個圖片下載完成,它可以被存放到磁碟中,在沒有網路的時候就可以調出來使用了。

為了簡單我先空實現ImageCache介面,產生一個MyImageCache對象

    class MyImageCache implements ImageCache {        @Override        public Bitmap getBitmap(String url) {            return null;        }        @Override        public void putBitmap(String url, Bitmap bitmap) {        }    }

這個介面提供的方法簡單明了,今後我們可以用自己的記憶體緩衝來完善這個類,當前getBitmap返回的是null,說明這個記憶體緩衝沒啥用處,和沒緩衝一樣。

 

2.2 實現載入網狀圖片

        ImageLoader imageLoader = new ImageLoader(mQueue, new MyImageCache());        ImageListener listener = ImageLoader.getImageListener(iv, R.drawable.default_photo, R.drawable.error_photo);        imageLoader.setShouldCache(true);        imageLoader.get("http://img5.duitang.com/uploads/item/201409/14/20140914162144_MBEmX.jpeg", listener);

代碼的思路是產生ImageLoader後,再初始化一個監聽器,監聽器中傳入imageview對象,還有預設的圖片,出錯時展示的圖片,這個很好理解。最後在imageLoader的get方法中傳入URL,還有監聽器對象即可。

值得注意的是,get方法還有一種變體:

imageLoader.get("http://img5.duitang.com/uploads/item/201409/14/20140914162144_MBEmX.jpeg", listener, 0 ,0);

這裡最後傳入的數值是得到圖片的最大寬、高,其意義和ImageRequest中的寬、高完全一致,可以參考之前的文章。其實,如果你去源碼中找找的話,你會發現這兩個參數最終都是傳給ImageRequest的,所以在此就不做過多講解了。

 

2.3 設定緩衝

因為我們一上來就修改了源碼,所以當我們在執行get()方法前可以通過setShouldCache(false)來取消磁碟緩衝,如果你不進行設定的話預設是執行磁碟緩衝的。那麼如何配置L1緩衝呢?剛剛我們的MyImageCache僅僅是一個空實現,現在就開始來完善它。

我的想法是通過LruCache進行圖片緩衝,分配的緩衝空間是5m。如果對LruCache不是很瞭解,可以看看我之前的文章:詳細解讀LruCache類

class MyImageCache implements ImageCache {        private LruCache<String, Bitmap> mCache;                  public MyImageCache() {            int maxSize = 5 * 1024 * 1024;              mCache = new LruCache<String, Bitmap>(maxSize) {                  @Override                  protected int sizeOf(String key, Bitmap bitmap) {                      return bitmap.getRowBytes() * bitmap.getHeight();                  }              };        }                @Override        public Bitmap getBitmap(String url) {            return mCache.get(url);                      }        @Override        public void putBitmap(String url, Bitmap bitmap) {            mCache.put(url, bitmap);         }    }

每次執行get方法時,Volley會到MyImageCache中調用getBitmap(),看看有沒有記憶體緩衝,如果你返回了null,那麼Volley就會從網路上下載,如果不為null,Volley會直接把取得的bitmap展示到imageview中。當圖片展示到螢幕上後(無論是這個圖片是從記憶體中讀的,還是從磁碟中讀的,或者是從網路上下載的),Volley都會自動調用putBitmap,把圖片放入記憶體中緩衝起來。

說明:緩衝的size是:bitmap.getRowBytes() * bitmap.getHeight(),這裡getRowBytes()是返回圖片每行的位元組數,圖片的size應該乘以高度。

 

注意:imageLoader.setShouldCache(false);僅僅是設定了不實用磁碟緩衝,和記憶體緩衝沒有任何關係。如果你想要不實用記憶體緩衝,請在自訂的ImageCache中進行處理。

 

2.4 其他方法

public final boolean shouldCache()

查看是否已經做了磁碟緩衝。

 

void setShouldCache(boolean shouldCache)

設定是否運行磁碟緩衝,此方法需要在get方法前使用

 

public boolean isCached(String requestUrl, int maxWidth, int maxHeight)

判斷對象是否已經被緩衝,傳入url,還有圖片的最大寬高

 

public void setBatchedResponseDelay(int newBatchedResponseDelayMs)

Sets the amount of time to wait after the first response arrives before delivering all responses. Batching can be disabled entirely by passing in 0.

設定第一次響應到達後到分發所有響應之前的整體時間,單位ms,如果你設定的時間是0,那麼Batching將不可用。

 

三、NetworkImageView

NetworkImageView繼承自ImageView,你可以認為它是一個可以實現載入網狀圖片的imageview,十分簡單好用。

3.1 XML

    <com.android.volley.toolbox.NetworkImageView        android:id="@+id/network_image_view"        android:layout_width="200dp"        android:layout_height="200dp"        android:layout_gravity="center_horizontal" />

 

3.2 JAVA

     NetworkImageView networkImageView = (NetworkImageView) findViewById(R.id.network_image_view);        networkImageView.setDefaultImageResId(R.drawable.default_photo);        networkImageView.setErrorImageResId(R.drawable.error_photo);        networkImageView.setImageUrl("http://img5.duitang.com/uploads/item/201409/14/20140914162144_MBEmX.jpeg", imageLoader);

 

3.3 設定圖片的寬高

NetworkImageView沒有提供任何設定圖片寬高的方法,這是由於它是一個控制項,在載入圖片的時候它會自動擷取自身的寬高,然後對比網狀圖片的寬度,再決定是否需要對圖片進行壓縮。也就是說,壓縮過程是在內部完全自動化的,並不需要我們關心。NetworkImageView最終會始終呈現給我們一張大小比控制項尺寸略大的網狀圖片,因為它會根據控制項寬高來等比縮放原始圖片,這點需要注意,如果你想要瞭解詳細原理,請看我之前的ImageRequest介紹。

如果你不想對圖片進行壓縮的話,只需要在布局檔案中把NetworkImageView的layout_width和layout_height都設定成wrap_content就可以了,這樣它就會將該圖片的原始大小展示出來,不會進行任何壓縮。

 

參考自:

http://blog.csdn.net/guolin_blog/article/details/17482165

詳細解讀Volley(三)—— ImageLoader & NetworkImageView

聯繫我們

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