为了账号安全,请及时绑定邮箱和手机立即绑定

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下载
官方微信

举报

0/150
提交
取消