返回首页 Fresco 中文版

配置和使用

DRAWEE 指南

IMAGE PIPELINE 指南

第三方类库

自定义网络加载

Image pipeline 默认使用HttpURLConnection。应用可以根据自己需求使用不同的网络库。

OkHttp

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);