Comparison of several image loading frameworks for Android

1. Basic information of the four major image caches

Universal ImageLoader is an early open source image cache that was used by many applications in the early days.
Picasso is a Square open source project, and its leader is JakeWharton, so it is widely known.
Glide is an open source project by Google employees, used by some Google Apps, and featured at Google I/O 2014.
Fresco is Facebook's open source image cache in the first half of this year. The main features include:
  1. The two memory caches plus the native cache constitute the third-level cache;
  2. Support streaming, which can display pictures in a blurry and progressive manner similar to those on web pages;
  3. Better support for multi-frame animation pictures, such as Gif, WebP.
advantage:
1. Pictures are stored in The anonymous shared memory of the Android system is not in the heap memory of the virtual machine. The intermediate buffer data of the picture is also stored in the local heap memory. Therefore, the application has more memory usage, and the oom will not be caused by the loading of the picture. Reduce the interface lag caused by frequent garbage collector calls to recycle Bitmap, and the performance is higher.
2. Progressively load JPEG images, support images from blurry to clear loading
3. The image can be displayed in the ImageView with any center point, not just the center of the image.
4. The resize of JPEG images is also done in native, not in the heap memory of the virtual machine, which also reduces OOM
5. Good support for the display of GIF images
shortcoming:
1. The frame is large, which affects the size of the Apk
2. It is more complicated to use
2. Basic Concepts
Before the formal comparison, first understand a few common concepts of image caching:
  1. RequestManager: request generation and management module;
  2. Engine: The engine part is responsible for creating tasks (getting data) and scheduling execution;
  3. GetDataInterface: Data acquisition interface, responsible for acquiring data from various data sources. For example, MemoryCache gets data from memory cache, DiskCache gets data from local cache, downloader gets data from network, etc.
  4. Displayer:资源(图片)显示器,用于显示或操作资源。 比如 ImageView,这几个图片缓存都不仅仅支持 ImageView,同时支持其他 View 以及虚拟的 Displayer 概念。
  5. Processor 资源(图片)处理器, 负责处理资源,比如旋转、压缩、截取等。
以上概念的称呼在不同图片缓存中可能不同,比如 Displayer 在 ImageLoader 中叫做 ImageAware,在 Picasso 和 Glide 中叫做 Target。
三、 共同优点
1. 使用简单
都可以通过一句代码可实现图片获取和显示。
2. 可配置度高,自适应程度高
图片缓存的下载器(重试机制)、解码器、显示器、处理器、内存缓存、本地缓存、线程池、缓存 算法等大都可轻松配置。
自适应程度高,根据系统性能初始化缓存配置、系统信息变更后动态调整策略。
比如根据 CPU 核数确定最大并发数,根据可用内存确定内存缓存大小,网络状态变化时调整最大并发数等。
3. 多级缓存
都至少有两级缓存、提高图片加载速度。
4. 支持多种数据源
支持多种数据源,网络、本地、资源、Assets 等
5. 支持多种 Displayer
不仅仅支持 ImageView,同时支持其他 View 以及虚拟的 Displayer 概念。
其他小的共同点包括支持动画、支持 transform 处理、获取 EXIF 信息等。
四、ImageLoader 设计及优点


1. 总体设计及流程
整个库分为 ImageLoaderEngine,Cache 及 ImageDownloader,ImageDecoder,BitmapDisplayer,BitmapProcessor 五大模块,其中 Cache 分为 MemoryCache 和 DiskCache 两部分。
简单的讲就是 ImageLoader 收到加载及显示图片的任务,并将它交给 ImageLoaderEngine,ImageLoaderEngine 分发任务到具体线程池去执行,任务通过 Cache 及 ImageDownloader 获取图片,中间可能经过 BitmapProcessor 和 ImageDecoder 处理,最终转换为Bitmap 交给 BitmapDisplayer 在 ImageAware 中显示。
2. I mageLoader 优点
(1) 支持下载进度监听
(2) 可以在 View 滚动中暂停图片加载
通过 PauseOnScrollListener 接口可以在 View 滚动中暂停图片加载。
(3) 默认实现多种内存缓存算法这几个图片缓存都可以配置缓存算法,不过 ImageLoader 默认实现了较多缓存算法,如 Size 最大先删除、使用最少先删除、最近最少使用、先进先删除、时间最长先删除等。
(4) 支持本地缓存文件名规则定义
缺点:
 缺点在于不支持GIF图片加载,  缓存机制没有和http的缓存很好的结合, 完全是自己的一套缓存机制
五、Picasso 设计及优点


1. 总体设计及流程
整个库分为 Dispatcher,RequestHandler 及 Downloader,PicassoDrawable 等模块。
Dispatcher 负责分发和处理 Action,包括提交、暂停、继续、取消、网络状态变化、重试等等。
简单的讲就是 Picasso 收到加载及显示图片的任务,创建 Request 并将它交给 Dispatcher,Dispatcher 分发任务到具体 RequestHandler,任务通过 MemoryCache 及 Handler(数据获取接口) 获取图片,图片获取成功后通过 PicassoDrawable 显示到 Target 中。
需要注意的是上面 Data 的 File system 部分,Picasso 没有自定义本地缓存的接口,默认使用 http 的本地缓存,API 9 以上使用 okhttp,以下使用 Urlconnection,所以如果需要自定义本地缓存就需要重定义 Downloader。
2. Picasso 优点
(1) 自带统计监控功能
支持图片缓存使用的监控,包括缓存命中率、已使用内存大小、节省的流量等。
(2) 支持优先级处理
每次任务调度前会选择优先级高的任务,比如 App 页面中 Banner 的优先级高于 Icon 时就很适用。
(3) 支持延迟到图片尺寸计算完成加载
(4) 支持飞行模式、并发线程数根据网络类型而变
手机切换到飞行模式或网络类型变换时会自动调整线程池最大并发数,比如 wifi 最大并发为 4, 4g 为 3,3g 为 2。
这里 Picasso 根据网络类型来决定最大并发数,而不是 CPU 核数。
(5) “无”本地缓存
无”本地缓存,不是说没有本地缓存,而是 Picasso 自己没有实现,交给了 Square 的另外一个网络库 okhttp 去实现,这样的好处是可以通过请求 Response Header 中的 Cache-Control 及 Expired 控制图片的过期时间。
缺点
 于不支持GIF, 并且它可能是想让服务器去处理图片的缩放, 它缓存的图片是未缩放的, 并且默认使用ARGB_8888格式缓存图片, 缓存体积大.

六、Glide 设计及优点

1. 总体设计及流程
整个库分为 RequestManager(请求管理器),Engine(数据获取引擎)、Fetcher(数据获取器)、MemoryCache(内存缓存)、DiskLRUCache、Transformation(图片处理)、Encoder(本地缓存存储)、Registry(图片类型及解析器配置)、Target(目标)等模块。
简单的讲就是 Glide 收到加载及显示资源的任务,创建 Request 并将它交给RequestManager,Request 启动 Engine 去数据源获取资源(通过 Fetcher ),获取到后 Transformation 处理后交给 Target。
Glide 依赖于 DiskLRUCache、GifDecoder 等开源库去完成本地缓存和 Gif 图片解码工作。
2. Glide 优点
(1) 图片缓存->媒体缓存
Glide 不仅是一个图片缓存,它支持 Gif、WebP、缩略图。甚至是 Video,所以更该当做一个媒体缓存。
(2) 支持优先级处理
(3) 与 Activity/Fragment 生命周期一致,支持 trimMemory
Glide 对每个 context 都保持一个 RequestManager,通过 FragmentTransaction 保持与 Activity/Fragment 生命周期一致,并且有对应的 trimMemory 接口实现可供调用。
(4) 支持 okhttp、Volley
Glide 默认通过 UrlConnection 获取数据,可以配合 okhttp 或是 Volley 使用。实际 ImageLoader、Picasso 也都支持 okhttp、Volley。
(5) 内存友好
① Glide 的内存缓存有个 active 的设计
从内存缓存中取数据时,不像一般的实现用 get,而是用 remove,再将这个缓存数据放到一个 value 为软引用的 activeResources map 中,并计数引用数,在图片加载完成后进行判断,如果引用计数为空则回收掉。
② 内存缓存更小图片
Glide 以 url、view width、viewheight、屏幕的分辨率等做为联合 key,将处理后的图片缓存在内存缓存中,而不是原始图片以节省大小
③ 与 Activity/Fragment 生命周期一致,支持 trimMemory
④ 图片默认使用默认 RGB 565 而不是 ARGB888
虽然清晰度差些,但图片更小,也可配置到 ARGB_888。
其他:Glide 可以通过 signature 或不使用本地缓存支持 url 过期
7.Fresco是目前最强大的图片加载组件之一 。
  • Fresco中设计有一个叫做image pipeline的模块。它负责从网络,从本地文件系统,本地资源加载图片。 为了最大限度节省空间和CPU时间,它含有3级缓存设计(2级内存,1级文件)。
Fresco中设计有一个叫做Drawees模块, 方便地显示loading图,当图片不再显示在屏幕上时,及时地释放内存和空间占用。
Fresco支持Android2.3(API level 9)及其以上系统。
特点-内存管理
  • 一个没有未压缩的图片,即Android中的Bitmap,占用大量的内存。大的内存占用势必引发更加频繁的GC。 在5.0以下,GC将会显著地引发界面卡顿。
在5.0以下系统,Fresco将图片放到一个特别的内存区域。当然,在图片不显示的时候,占用的内存会自动被释放。 这会使得APP更加流畅,减少因图片内存占用而引发的OOM。
特点-渐进式呈现图片
  • 渐进式的JPEG图片格式已经流行数年了,渐进式图片格式先呈现大致的图片轮廓,然后随着图片下载的继续, 呈现逐渐清晰的图片,这对于移动设备,尤其是慢网络有极大的利好,可带来更好的用户体验。
特点-Gif图和WebP格式
  • 支持加载Gif图,支持WebP格式。
特点-图像的呈现
  • 1、自定义居中焦点(对人脸等图片显示非常有帮助)。
  • 2、圆角图,当然圆圈也行。
  • 3、下载失败之后,点击重现下载。
  • 4、自定义占位图,自定义overlay, 或者进度条。
  • 5、指定用户按压时的overlay。
特点-图像的加载
  • 1、为同一个图片指定不同的远程路径,或者使用已经存在本地缓存中的图片。
  • 2、先显示一个低解析度的图片,等高清图下载完之后再显示高清图。
  • 3、加载完成回调通知。
  • 4、对于本地图,如有EXIF缩略图,在大图加载完成之前,可先显示缩略图。
  • 5、缩放或者旋转图片。
  • 6、处理已下载的图片。
  • 7、WebP支持。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528048&siteId=291194637