本教程內(nèi)容來源于:http://fresco-cn.org
采用 知識(shí)共享 署名 4.0 國際 許可協(xié)議 進(jìn)行許可
Image pipeline 默認(rèn)使用HttpURLConnection。應(yīng)用可以根據(jù)自己需求使用不同的網(wǎng)絡(luò)庫。
OkHttp 是一個(gè)流行的開源網(wǎng)絡(luò)請(qǐng)求庫。Image pipeline有一個(gè)使用OkHttp替換掉了Android默認(rèn)的網(wǎng)絡(luò)請(qǐng)求的補(bǔ)充。
如果需要使用OkHttp, 不要使用這個(gè)下載頁面的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+"
}
配置Image
pipeline這時(shí)也有一些不同,不再使用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, 這個(gè)類包含了網(wǎng)絡(luò)通信。
你也可以選擇性地繼承NfpRequestState, 這個(gè)類是請(qǐng)求時(shí)的數(shù)據(jù)結(jié)構(gòu)描述。
默認(rèn)的 HttpURLConnection
可以作為一個(gè)參考. 源碼在這 its source code.
在配置Image pipeline時(shí),把producer傳遞給Image pipeline。
ImagePipelineConfig config = ImagePipelineConfig.newBuilder()
.setNetworkFetchProducer(myNetworkFetchProducer);
. // other setters
.build();
Fresco.initialize(context, config);