Android 網絡通訊框架Volley(二)

Volley提供2個靜態方法:緩存

public static RequestQueue newRequestQueue(Context context) {}

public static RequestQueue newRequestQueue(Context context, HttpStack stack) {} 
第一個直接調用第二個的newRequestQueue(context, null);方法,返回都是一個RequestQueue 對象
以ImageLoader爲例 構造方法
 
public ImageLoader(RequestQueue queue, ImageCache imageCache) {
        mRequestQueue = queue;
        mCache = imageCache;
}
 
須要一個RequestQueue 對象 和一個ImageCache對象 ,RequestQueue 能夠經過Volley類中的靜態方法newRequestQueue(Context context) 得到,而ImageCache 則是一個interface
 public interface ImageCache {
        public Bitmap getBitmap(String url);
        public void putBitmap(String url, Bitmap bitmap);
    }
實現此接口,即上一文章中Cache包下的BitmapCache,賦值給mCache對象
mCache中緩存key value,key規則爲String key = "#W"+maxWidth+"#H"+ maxHeight +url;

再來看下面方法
參數:defaultImageResId:未開始下載顯示的圖片Id,若是傳遞0則不顯示任何東西
           errorImageResId:下載顯示的圖片Id,若是傳遞0則不顯示任何東西
返回值:ImageListener
 
public static ImageListener getImageListener(final ImageView view,
            final int defaultImageResId, final int errorImageResId) {
        ...
        ...//直接new一個ImageListener
@Override public void onResponse(ImageContainer response, boolean isImmediate)
{ if (response.getBitmap() != null)
{ //response有值設置ImageView顯示圖片
view.setImageBitmap(response.getBitmap()); } else if (defaultImageResId != 0) { view.setImageResource(defaultImageResId); } } }
response.getBitmap()中的bitmap是經過BatchedImageRequest中的batchResponse(),
經過一個ImageContainer類中的Interface:ImageListerner.onResponse回調回來的
 
private void batchResponse(String cacheKey, BatchedImageRequest request,
            final VolleyError error) {
        mBatchedResponses.put(cacheKey, request);
        // If we don't already have a batch delivery runnable in flight, make a new one.
        // Note that this will be used to deliver responses to all callers in mBatchedResponses.
        if (mRunnable == null) {
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    for (BatchedImageRequest bir : mBatchedResponses.values()) {
                        for (ImageContainer container : bir.mContainers) {
                            if (container.mListener == null) {
                                continue;
                            }
                            if (error == null) {
                                //取得數據
                                container.mBitmap = bir.mResponseBitmap;
                                //經過ImageContainer中的mListener,即ImageListener中的onResponse()方法回調
                                container.mListener.onResponse(container, false);
                            } else {
                                container.mListener.onErrorResponse(error);
                            }
                        }
                    }
                    mBatchedResponses.clear();
                    mRunnable = null;
                }

            };
            // Post the runnable.
            mHandler.postDelayed(mRunnable, mBatchResponseDelayMs);
        }
    }
相關文章
相關標籤/搜索