前言
目录
1. 简介
- 介绍:Picasso,可译为“毕加索”,是Android中一个图片加载开源库
大概是因为其使用使用方法简单、优雅所以这样取名
- 主要作用:实现图片加载
2. 功能特点
2.1 功能列表
![功能列表](http://upload-images.jianshu.io/upload_images/944365-1464f8627b71f758.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)- 从上面可以看出,Picasso不仅实现了图片异步加载的功能,还解决了Android中加载图片时需要解决的一些常见问题
- 接下来,我会对Picasso的每个功能点进行详细的介绍
2.2 功能介绍
2.2.1 图片的异步加载(最基础功能)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
2.2.2 图片转换
使用最少的内存完成复杂的图片转换,转换图片以适合所显示的ImageView,来减少内存消耗
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.2.3 加载过重 & 错误处理
Picasso支持加载过程中和加载错误时显示对应图片
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.2.4 在Adapter中的回收不在视野的ImageView和取消已经回收的ImageView下载进程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2.2.5 从不同资源源加载
支持多种数据源 网络、本地、资源、Assets 等
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
2.2.6 自动添加磁盘和内存二级缓存功能
2.2.7 支持优先级处理
每次任务调度前会选择优先级高的任务,比如 App 页面中 Banner 的优先级高于 Icon 时就很适用。
2.2.8 支持飞行模式、并发线程数根据网络类型而变
手机切换到飞行模式或网络类型变换时会自动调整线程池最大并发数,比如 wifi 最大并发为 4, 4g 为 3,3g 为 2
2.2.9 “无”本地缓存
无”本地缓存,不是说没有本地缓存,而是 Picasso 自己没有实现,交给了 Square 的另外一个网络库 okhttp 去实现,这样的好处是可以通过请求 Response Header 中的 Cache-Control 及 Expired 控制图片的过期时间。
3. Demo实例
没有Demo的代码讲解不是好文章,让我们来一步步学会使用Picasso。
步骤1:在gradle添加依赖
- 1
- 1
步骤2:添加网络权限
- 1
- 1
步骤1和步骤2是Picasso使用的前提,千万别忘了!!!!
步骤3:在MainActivity中
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
还有具体其他功能需要配置的自己按照我上面写的进行配置就好了~
这里再贴上Picasso的Github地址:请点击这里
4. 特点
4.1 优点
- 使用简单、方便(一行代码的事情)
- 由于同样是出品自Square的,Square 公司的其他开源库如 Retrofit 或者 OkHttp和Picasso搭配使用兼容性会更好些,占用体积也会少些
所以,如果项目已经使用了 Square 公司的其他开源库(如 Retrofit 或者 OkHttp),在满足需求的前提下建议使用Picasso
4.2 缺点
- 功能较为简单-图片加载;
- 性能(加载速度等等)较其他图片加载库(Glide、Fresco)较差
- 自身无实现“本地缓存”