亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

OkHttp自定義網絡加載

標簽:
Android

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

OkHttp

OkHttp 是一个流行的开源网络请求库。Image pipeline有一个使用OkHttp替换掉了Android默认的网络请求的补充。

如果需要使用OkHttp, 不要使用这个下载页面的gradle依赖配置,应该使用下面的依赖配置

[代码]xml代码:

?

1

2

3

4

5

dependencies {

  // your project's other   dependencies

  compile   "com.facebook.fresco:fresco:0.9.0+"

  compile   'com.facebook.fresco:imagepipeline-okhttp:0.9.0+'

}

配置Image pipeline这时也有一些不同,不再使用ImagePipelineConfig.newBuilder,而是使用OkHttpImagePipelineConfigFactory:

[代码]xml代码:

?

1

2

3

4

5

6

7

8

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。

[代码]java代码:

?

1

2

3

4

5

ImagePipelineConfig config =   ImagePipelineConfig.newBuilder()

  .setNetworkFetchProducer(myNetworkFetchProducer);

  . // other setters

  .build();

Fresco.initialize(context, config);

Drawee is not tied to a particular image loading mechanism and can be used with other image loaders. Drawee 并不是吊死在特定的一种图片加载机制上,它同样适用于其他image loader。

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

Drawee 和 Volley ImageLoader配合使用

我们有一个Drawee使用Volley的 ImageLoader的补充实现。

我们仅仅对那些已经深度使用Volley ImageLoader的应用推荐这个组合。

同样地,如要使用,使用下面的依赖,而不是下载页面给出的依赖:

[代码]xml代码:

?

1

2

3

4

dependencies {

  // your project's other   dependencies

  compile:   "com.facebook.fresco:drawee-volley:0.9.0+"

}

初始化Volley ImageLoader

这时,不需要再调用Fresco.initialize了,需要的是初始化Volley。

[代码]java代码:

?

1

2

3

4

5

Context context;

ImageLoader imageLoader; // build   yourself

VolleyDraweeControllerBuilderSupplier   mControllerBuilderSupplier

    = new VolleyDraweeControllerBuilderSupplier(context,   imageLoader);

SimpleDraweeView.initialize(mControllerBuilderSupplier);

不要让 VolleyDraweeControllerBuilderSupplier离开作用域,你需要它来创建DraweeController,除非你只使用SimpleDraweeView.setImageURI

DraweeControllers 和 Volley ImageLoader 配合使用

不是调用Fresco.newControllerBuilder, 而是:

[代码]java代码:

?

1

2

3

4

5

VolleyController controller =   mControllerBuilderSupplier

    .newControllerBuilder()

    . // setters

    .build();

mSimpleDraweeView.setController(controller);

Drawee 和其他Image Loader 配合使用

依照源码 作为例子,其他Image Loader也是可以和Drawee配合使用的,但是没有我们还没有Drawee和其他Image loader的配合使用的补充实现。

 

原文链接:http://www.apkbus.com/blog-705730-60548.html

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 1
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消