免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
volley完全解析

一、volley是什么?

1、簡介

Volley是Goole在2013年Google I/O大會上推出了一個新的網(wǎng)絡(luò)通信框架,它是開源的。從名字由來和配圖中無數(shù)急促的火箭可以看出 Volley 的特點:特別適合數(shù)據(jù)量小,通信頻繁的網(wǎng)絡(luò)操作。(個人認為 Android 應(yīng)用中絕大多數(shù)的網(wǎng)絡(luò)操作都屬于這種類型)。

Volley加載圖片實現(xiàn)了兩級緩存(網(wǎng)絡(luò)緩存、文件緩存),沒有實現(xiàn)內(nèi)存的緩存。Volley已經(jīng)把各種異步任務(wù)、圖片采樣都封裝好了。內(nèi)存緩存使用lrucache類實現(xiàn),需要我們手動添加進去。沒有使用軟引用緩存。因為4.0之后的android系統(tǒng)已經(jīng)不推薦使用軟引用緩存了。

2、volley的總體設(shè)計

3、volley可以做什么

JSON,圖像等的異步下載;
處理get、post等網(wǎng)絡(luò)請求;
網(wǎng)絡(luò)請求的排序(scheduling);
網(wǎng)絡(luò)請求的優(yōu)先級處理;
緩存;
多級別取消請求;
和Activity和生命周期的聯(lián)動(Activity結(jié)束時同時取消所有網(wǎng)絡(luò)請求);
等等。

它的設(shè)計目標就是非常適合去進行數(shù)據(jù)量不大,但通信頻繁的網(wǎng)絡(luò)操作,而對于大數(shù)據(jù)量的網(wǎng)絡(luò)操作,比如說下載文件等,Volley的表現(xiàn)就會非常糟糕。

二、圖片的三級緩存在volley中的實現(xiàn)

其實volley可以完全取代我們手寫的三級緩存,因為google已經(jīng)對volley進行了非常好的封裝,具體說明如下:

1、volley的推薦用法-單例模式

使用volley時,我們推薦把volley的使用封裝成單例使用。在application中初始化它。具體代碼如下:

單例:

package com.ht.xiangqu.util;import android.content.Context;import android.graphics.Bitmap;import android.support.v4.util.LruCache;import com.android.volley.RequestQueue;import com.android.volley.toolbox.ImageLoader;import com.android.volley.toolbox.Volley;/** * Created by annuo on 2015/6/16. */public class RequestManager { private static RequestManager ourInstance; private RequestQueue requestQueue; private ImageLoader imageLoader; public static RequestManager createInstance(Context context) { if (context != null) { if (ourInstance == null) { ourInstance = new RequestManager(context); } else { throw new IllegalArgumentException('Context must be set'); } } return ourInstance; } public static RequestManager getInstance() { return ourInstance; } private RequestManager(Context context) { requestQueue = Volley.newRequestQueue(context); imageLoader = new ImageLoader( requestQueue, new ImageLoader.ImageCache() { private LruCache cache = new LruCache<>(20); @Override public Bitmap getBitmap(String url) { return cache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { cache.put(url, bitmap); } } ); } public RequestQueue getRequestQueue() { return requestQueue; } public ImageLoader getImageLoader() { return imageLoader; }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

application:

package com.ht.xiangqu.util;import android.app.Application;import android.util.Log;/** * Created by annuo on 2015/6/16. */public class MainApplation extends Application { @Override public void onCreate() { super.onCreate(); RequestManager.createInstance(getApplicationContext()); Log.d('nihao', 'nihao'); }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2、內(nèi)存緩存

只有加載圖片的時候才會有內(nèi)存緩存,對于字符串一般都是之后文件緩存。

google并沒有自動的幫我們實現(xiàn)內(nèi)存的緩存,需要我們自己手動加入進去。內(nèi)存緩存在單例類中已經(jīng)體現(xiàn)了(即LruCache),以后我們每次使用的時候都不必再加入內(nèi)存緩存。LruCache這個類是Android3.1版本中提供的,如果你是在更早的Android版本中開發(fā),則需要導入android-support-v4的jar包。

3、文件緩存

volley已經(jīng)默認幫我們實現(xiàn)了文件的緩存。我們通過源代碼看一下:

/** * 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; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

以上代碼是imageloader中的一段代碼,從中我們可以看到在構(gòu)造imageloader時,我們已經(jīng)默認的建立了一個L1級的緩存(文件緩存)。

那volley緩存下來的文件到底在哪呢?見下圖:

具體的位置就在如圖所示的位置。即data/data/應(yīng)用程序的包名/volley,如果沒有修改volley的緩存位置,默認名字叫volley。

4、圖片的二次采樣的問題

其實volley默認的已經(jīng)幫我們做了圖片的二次采樣,只是需要我們在進行請求的時候,多加入兩個參數(shù)。我們一般都忽略了這個問題,最后導致的是不斷的OOM。

/** * 這是訪問網(wǎng)絡(luò)圖片的核心方法 * @param requestUrl * @param imageListener * @param maxWidth * @param maxHeight * @return */ public ImageContainer get(String requestUrl, ImageListener imageListener, int maxWidth, int maxHeight) {
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
Request newRequest = new ImageRequest(requestUrl, new Listener() { @Override public void onResponse(Bitmap response) { onGetImageSuccess(cacheKey, response); } }, maxWidth, maxHeight, Config.RGB_565, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { onGetImageError(cacheKey, error); } }); mRequestQueue.add(newRequest); mInFlightRequests.put(cacheKey, new BatchedImageRequest(newRequest, imageContainer)); return imageContainer; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

以上代碼是imageloader的核心方法,其中有兩個參數(shù)是maxWidth,maxHeight,我們一般都忽略了這兩個參數(shù)。默認值是0和0,這樣二次采樣算法就不起作用了,我們得到的圖片是從服務(wù)器1:1哪來的。但是一般我們設(shè)置了這兩個參數(shù),就可以得到我們希望的縮小比例的圖片。這樣就可以完全的避免OOM。

三、volley的其他問題

1、圓角圖片的問題

public static ImageListener getImageListener(final ImageView view, final int defaultImageResId, final int errorImageResId) { return new ImageListener() { @Override public void onErrorResponse(VolleyError error) { if (errorImageResId != 0) { view.setImageResource(errorImageResId); } } @Override public void onResponse(ImageContainer response, boolean isImmediate) { if (response.getBitmap() != null) { //在這里可以設(shè)置,如果想得到圓角圖片的畫,可以對bitmap進行加工,可以給imageview加一個 //額外的參數(shù) view.setImageBitmap(response.getBitmap()); } else if (defaultImageResId != 0) { view.setImageResource(defaultImageResId); } } }; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

2、listview復用時,解決圖片錯位的問題

/** * 使用此方法能夠解決圖片錯亂問題 * @param view * @param defaultImageResId * @param errorImageResId * @param url * @return */ public static ImageListener getImageListener( final ImageView view, final int defaultImageResId, final int errorImageResId, final String url) { return new ImageListener() { @Override public void onErrorResponse(VolleyError error) { if (errorImageResId != 0) { view.setImageResource(errorImageResId); } } @Override public void onResponse(ImageContainer response, boolean isImmediate) { if (response.getBitmap() != null) { //在這里可以設(shè)置,如果想得到圓角圖片的畫,可以對bitmap進行加工,可以給imageview加一個 //額外的參數(shù) String urlTag = (String) view.getTag(); if(urlTag!=null && urlTag.trim().equals(url)){ view.setImageBitmap(response.getBitmap()); } } else if (defaultImageResId != 0) { view.setImageResource(defaultImageResId); } } }; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

四、volley的具體用法

關(guān)于這塊內(nèi)容,網(wǎng)絡(luò)上有很多的資料,總之volley使用起來非常的簡單,感興趣的可以去網(wǎng)絡(luò)上查找相關(guān)的資料進行學習。volley的加載速度絕對出乎你的意料。

——知道自己是誰,要什么,能跳多高,而且敢跳,并承受跳了可能失敗的所有結(jié)果。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android網(wǎng)絡(luò)框架Volley
Android利用Volley異步加載數(shù)據(jù)(JSON和圖片)完整示例
Android Volley
Android 各大網(wǎng)絡(luò)請求庫的比較及實戰(zhàn)
Volley圖片加載相關(guān)源碼解析
Android客戶端讀取PHP服務(wù)器的圖片等信息,并用ListView列出來,實現(xiàn)緩存功能。
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服