本文內(nèi)容包括:
Image pipeline 默認(rèn)使用 HttpURLConnection 。應(yīng)用可以根據(jù)自己需求使用不同的網(wǎng)絡(luò)庫。
OkHttp 是一個流行的開源網(wǎng)絡(luò)請求庫。Imagepipeline有一個使用OkHttp替換掉了Android默認(rèn)的網(wǎng)絡(luò)請求的補充。
如果需要使用OkHttp,不要使用這個下載頁面的gradle依賴配置,應(yīng)該使用下面的依賴配置
dependencies {
// your project's other dependencies
compile: "com.facebook.fresco:drawee:0.1.0+"
compile: "com.facebook.fresco:imagepipeline-okhttp:0.1.0+"
}
配置Imagepipeline這時也有一些不同,不再使用ImagePipelineConfig.newBuilder
,而是使用OkHttpImagePipelineConfigFactory
:
Context context;
OkHttpClient okHttpClient; // build on your own
ImagePipelineConfig config = OkHttpImagePipelineConfigFactory
.newBuilder(context, okHttpClient)
. // other setters
. // setNetworkFetchProducer is already called for you
.build();
Fresco.initialize(context, config);
For complete control on how the networking layer should behave, you can provide one for your app. You must subclass 為了完全控制網(wǎng)絡(luò)層的行為,你可以自定義網(wǎng)絡(luò)層。繼承 NetworkFetchProducer , 這個類包含了網(wǎng)絡(luò)通信。
你也可以選擇性地繼承 NfpRequestState , 這個類是請求時的數(shù)據(jù)結(jié)構(gòu)描述。
默認(rèn)的 HttpURLConnection
可以作為一個參考.
在配置 Image pipeline 時,把producer傳遞給Image pipeline。
ImagePipelineConfig config = ImagePipelineConfig.newBuilder()
.setNetworkFetchProducer(myNetworkFetchProducer);
. // other setters
.build();
Fresco.initialize(context, config);
Drawee 并不是吊死在特定的一種圖片加載機制上,它同樣適用于其他 image loader。
不過有一些特性,只有Fresco image pipeline才有。前面的提到的需要使用 ImageRequest 和配置 imagepipeline的特性,使用其他image loader時都有可能不起作用。
我們有一個Drawee使用Volley的 ImageLoader 的補充實現(xiàn)。
我們僅僅對那些已經(jīng)深度使用Volley ImageLoader的應(yīng)用推薦這個組合。
同樣地,如要使用,使用下面的依賴,而不是下載頁面給出的依賴:
dependencies {
// your project's other dependencies
compile: "com.facebook.fresco:drawee-volley:0.1.0+"
}
這時,不需要再調(diào)用Fresco.initialize
了,需要的是初始化Volley。
Context context;
ImageLoader imageLoader; // build yourself
VolleyDraweeControllerBuilderSupplier mControllerBuilderSupplier
= new VolleyDraweeControllerBuilderSupplier(context, imageLoader);
SimpleDraweeView.initialize(mControllerBuilderSupplier);
不要讓 VolleyDraweeControllerBuilderSupplier
離開作用域,你需要它來創(chuàng)建DraweeController,除非你只使用SimpleDraweeView.setImageURI
。
不是調(diào)用Fresco.newControllerBuilder
, 而是:
VolleyController controller = mControllerBuilderSupplier
.newControllerBuilder()
. // setters
.build();
mSimpleDraweeView.setController(controller);
依照源碼 作為例子,其他Image Loader也是可以和Drawee配合使用的,但是沒有我們還沒有Drawee和其他Image loader的配合使用的補充實現(xiàn)。
更多建議: