本教程內容來源于:http://fresco-cn.org
采用 知識共享 署名 4.0 國際 許可協議 進行許可
Image pipeline 默認使用HttpURLConnection。應用可以根據自己需求使用不同的網絡庫。
OkHttp 是一個流行的開源網絡請求庫。Image pipeline有一個使用OkHttp替換掉了Android默認的網絡請求的補充。
如果需要使用OkHttp, 不要使用這個下載頁面的gradle依賴配置,應該使用下面的依賴配置
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這時也有一些不同,不再使用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 為了完全控制網絡層的行為,你可以自定義網絡層。繼承NetworkFetchProducer, 這個類包含了網絡通信。
你也可以選擇性地繼承NfpRequestState, 這個類是請求時的數據結構描述。
默認的 HttpURLConnection 可以作為一個參考. 源碼在這 its source code.
在配置Image pipeline時,把producer傳遞給Image pipeline。
ImagePipelineConfig config = ImagePipelineConfig.newBuilder()
.setNetworkFetchProducer(myNetworkFetchProducer);
. // other setters
.build();
Fresco.initialize(context, config);