第 4 章 第三方類庫

2018-02-24 15:57 更新

本文內(nèi)容包括:

自定義網(wǎng)絡(luò)加載

Image pipeline 默認(rèn)使用 HttpURLConnection 。應(yīng)用可以根據(jù)自己需求使用不同的網(wǎng)絡(luò)庫。

OkHttp

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

使用自定的網(wǎng)絡(luò)層

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

使用其他的Image Loader

Drawee 并不是吊死在特定的一種圖片加載機制上,它同樣適用于其他 image loader。

不過有一些特性,只有Fresco image pipeline才有。前面的提到的需要使用 ImageRequest 和配置 imagepipeline的特性,使用其他image loader時都有可能不起作用。

Drawee 和 Volley ImageLoader配合使用

我們有一個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+"
}

初始化Volley ImageLoader

這時,不需要再調(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。

DraweeControllers 和 Volley ImageLoader 配合使用

不是調(diào)用Fresco.newControllerBuilder, 而是:

VolleyController controller = mControllerBuilderSupplier
    .newControllerBuilder()
    . // setters
    .build();
mSimpleDraweeView.setController(controller);

Drawee 和其他Image Loader 配合使用

依照源碼 作為例子,其他Image Loader也是可以和Drawee配合使用的,但是沒有我們還沒有Drawee和其他Image loader的配合使用的補充實現(xiàn)。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號