zoukankan      html  css  js  c++  java
  • Glide Picasso Fresco UIL 图片框架 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱
    MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

    Glide Picasso Fresco UIL 图片框架 MD


    目录

    Glide Google推荐 24k

    Glide 官方文档
    Glide 英文教程
    Glide 中文教程

    简介

    我们要晓得:Glide是Google推荐的图片加载库,专注于流畅的滚动

    Glide 是 Google 一位员工的大作,他完全是基于 Picasso 的,沿袭了 Picasso 的简洁风格,但是在此做了大量优化与改进。

    Glide 默认的 Bitmap 格式是 RGB_565 格式,而 Picasso 默认的是 ARGB_8888 格式,相比而言,这个内存开销要小一半。

    在磁盘缓存方面,Picasso 只会缓存原始尺寸的图片,而 Glide 缓存的是多种规格,也就意味着 Glide 会根据你 ImageView 的大小来缓存相应大小的图片尺寸,比如你 ImageView 大小是 200*200,原图是 400*400 ,而使用 Glide 就会缓存 200*200 规格的图,而 Picasso 只会缓存 400*400 规格的。这个改进就会导致 Glide 比 Picasso 加载的速度要快,毕竟少了每次裁剪重新渲染的过程。

    最重要的一个特性是 Glide 支持加载 Gif 动态图,而 Picasso 不支持该特性。

    除此之外,还有很多其他配置选项的增加。

    总体来说,Glide 是在 Picasso 基础之上进行的二次开发,各个方面做了不少改进,不过这也导致他的包比 Picasso 大不少,不过也就不到 500k,Picasso 是100多k,方法数也比 Picasso 多不少,不过毕竟级别还是蛮小的,影响不是很大。

    GitHub 上的介绍

    An image loading and caching library for Android focused on smooth scrolling

    Glide is a fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.

    Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs. Glide includes a flexible API that allows developers to plug in to almost any network stack. By default Glide uses a custom HttpUrlConnection based stack, but also includes utility libraries plug in to Google's Volley project or Square's OkHttp library instead.

    Glide's primary首要的 focus is on making scrolling any kind of a list of images as smooth and fast as possible, but Glide is also effective for almost any case where you need to fetch, resize, and display a remote image.

    基本使用

    添加依赖:

    repositories {
      mavenCentral()
      google()
    }
    
    dependencies {
      implementation 'com.github.bumptech.glide:glide:4.8.0'
      annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
    }

    加载图片:

    Glide.with(context)
        .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
        .into(ivImg);

    Glide的 with 方法不光接受 Context,还接受 Activity 和 Fragment。
    同时将 Activity/Fragment 作为 with() 参数的好处是:图片加载会和 Activity/Fragment 的生命周期保持一致,比如在 Paused 状态暂停加载,在 Resumed 的时候又自动重新加载。所以我建议传参的时候传递 Activity 和 Fragment 给Glide,而不是 Context。
    当然,如果使用 Application 作为上下文,Glide 请求将不受 Activity/Fragment 生命周期控制。

    总结

    • 优点:加载速度极快,框架体积小,四五百KB
    • 缺点:因为机制的选择不同,速度快了,但是图片质量低了,RGB565
    • 特点:根据ImageView大小来进行缓存,也就是说一张图片可能根据展示情况来缓存不同尺寸的几份

    可以 load 的资源类型

    Glide基本可以load任何可以拿到的媒体资源,如:

    load(String string)
    load SD卡资源:load("file://"+ Environment.getExternalStorageDirectory().getPath()+"/test.jpg") 
    load assets资源:load("file:///android_asset/f003.gif") 
    load raw资源:load("Android.resource://com.frank.glide/raw/raw_1")
              或 load("android.resource://com.frank.glide/raw/"+R.raw.raw_1) 
    load drawable资源:load("android.resource://com.frank.glide/drawable/news")
                   或 load("android.resource://com.frank.glide/drawable/"+R.drawable.news) 
    load ContentProvider资源:load("content://media/external/images/media/139469") 
    load http资源:load("http://img.my.csdn.net/uploads/201508/05/1438760757_3588.jpg") 
    load https资源:load("https://img.alicdn.com/tps/TB1uyhoMpXXXXcLXVXXXXXXXXXX-476-538.jpg_240x5000q50.jpg_.webp") 

    当然,load不限于String类型,还可以:

    • load(File file) file:The File containing the image。这个文件可能不存在于你的设备中,然而你可以用任何文件路径,去指定一个图片路径。
    • load(Integer resourceId) resourceId:the id of the resource containing the image。可以用R.drawable或R.mipmap
    • load(Uri uri) uri:The Uri representing the image. Must be of a type handled by UriLoader
    • load(byte[] model) model:the data to load.
    • loadFromMediaStore(Uri uri) uri:The uri representing the media.
    • load(T model) model:The model the load.
    • load(URL url):deprecated

    load的资源也可以是本地视频,但如果是一个网络 URL 的视频,它是不工作的!

    从资源 id 转换成 Uri 的方法:

    public static Uri resourceIdToUri(Context context, int resourceId) {
        return Uri.parse("android.resource://" + context.getPackageName() + "/" + resourceId);
    }

    比 Picasso 强大的地方

    • Glide 可以加载 GIF 动态图,而 Picasso 不能。同时因为 Glide 和 Activity/Fragment 的生命周期是一致的,因此 gif 的动画也会自动的随着 Activity/Fragment 的状态暂停、重放。Glide 的缓存在 gif 这里也是一样,调整大小然后缓存。
    • Glide 可以将任何的本地视频解码成一张静态图片。
    • 可以配置图片显示的动画,而 Picasso 只有一种动画:fading in。
    • 可以使用 thumbnail() 产生一个你所加载图片的 thumbnail。

    其实还有一些特性,不过不是非常重要,比如将图像转换成字节数组等。

    Picasso 16K

    Picasso是Square公司开源的一个Android平台上的图片加载框架,简单易用,一句话搞定项目中的图片加载,好用到令人发指。相比而言,这个也算是一个出来时间比较长的框架了。

    使用示例:

    Picasso.with(this)
        .load("url")
        .placeholder(R.mipmap.ic_default)
        .into(imageView);

    添加依赖

    compile 'com.squareup.picasso:picasso:2.5.2'

    A powerful image downloading and caching library for Android

    总结

    • 优点:图片质量高
    • 缺点:加载速度一般
    • 特点:只缓存一个全尺寸的图片,根据需求的大小在压缩转换

    Fresco 15K

    FaceBook出品,支持Android 2.3 (Gingerbread)及以上

    尼玛,他竟然有专门的 中文文档

    Fresco 是 Facebook 出品的新一代的图片加载库,我们知道 Android 应用程序可用的内存有限,经常会因为图片加载导致 OOM,虽然我们有各种手段去优化,尽量减少出现 OOM 的可能性,但是永远没法避免,尤其某些低端手机 OOM 更是严重。而 Facebook 就另辟蹊径,既然没法在 Java 层处理,我们就在更底层的 Native 堆做手脚。于是 Fresco 将图片放到一个特别的内存区域叫 Ashmem 区,就是属于 Native 堆,图片将不再占用 App 的内存,Java 层对此无能为力,这里是属于 C++ 的地盘,所以能大大的减少 OOM。

    添加依赖

    compile 'com.facebook.fresco:fresco:1.2.0'
    
    //**************下面的依赖需要根据需求添加******************//
    // 在 API < 14 上的机器支持 WebP 时,需要添加
    compile 'com.facebook.fresco:animated-base-support:1.2.0'
    
    // 支持 GIF 动图,需要添加
    compile 'com.facebook.fresco:animated-gif:1.2.0'
    
    // 支持 WebP (静态图+动图),需要添加
    compile 'com.facebook.fresco:animated-webp:1.2.0'
    compile 'com.facebook.fresco:webpsupport:1.2.0'
    
    // 仅支持 WebP 静态图,需要添加
    compile 'com.facebook.fresco:webpsupport:1.2.0'

    GitHub上的介绍:

    Fresco is a powerful system for displaying images in Android applications.

    Fresco takes care of image loading and display, so you don't have to. It will load images from the network, local storage, or local resources, and display a placeholder占位符 until the image has arrived. It has two levels of cache; one in memory and another in internal storage.

    In Android 4.x and lower, Fresco puts images in a special region of Android memory. This lets your application run faster - and suffer the dreaded OutOfMemoryError much less often.

    Fresco also supports:

    • streaming of progressive JPEGs
    • display of animated GIFs and WebPs
    • extensive广阔的 customization of image loading and display
    • and much more!

    Fresco 支持许多URI格式,但 Fresco 不支持 相对路径的URI。所有的 URI 都必须是绝对路径,并且带上该 URI 的 scheme。如下:

    类型 SCHEME 示例
    远程图片 http:// HttpURLConnection或者参考使用其他网络加载方案
    本地文件 file:// FileInputStream
    ContentProvider content:// ContentResolver
    asset目录下的资源 asset:// AssetManager
    res目录下的资源 res:// Resources.openRawResource
    Uri中指定图片数据 data:mime/type;base64 数据类型必须符合rfc2397规定(仅支持UTF-8)

    总结

    • 优点:支持图像渐进式呈现,大公司出品,后期维护有保障
    • 缺点:框架体积较大,3M左右会增大apk的大小;操作方式不是特别简单,有一定学习成本
    • 特点:有两级内存一级文件的缓存机制,并且有自己特别的内存区域来处理缓存,避免oom

    Universal-Image-Loader 16K

    jar包下载

    UIL可以算是老牌最火的图片加载库了,使用过这个框架的项目可以说多到教你做人,我第一次把第三方开源图片加载框架加入项目中的就是这个了,当时感觉瞬间逼格上涨,妈妈再也不用担心出现OOM和ListView图片错乱了。

    Powerful and flexible library for loading, caching and displaying images on Android.

    尼玛,虽然这哥们很久基本都不更新了,而且还不提供Gradle的支持,但目前依然是星星最多的一个图片处理库

    加载策略

    • 每一个图片的加载和显示任务都运行在独立的线程中,除非这个图片缓存在【内存】中,这种情况下图片会立即显示。
    • 如果需要的图片缓存在【本地】,他们会开启一个独立的线程队列。
    • 如果在缓存中没有正确的图片,任务线程会从线程池中获取,因此,快速显示缓存图片时不会有明显的障碍。

    控制OOM

    • 减少线程池中线程的个数,在ImageLoaderConfiguration中的.threadPoolSize中配置,推荐配置1-5
    • 在DisplayImageOptions选项中配置bitmapConfig为Bitmap.Config.RGB_565,因为默认是ARGB_8888, 使用RGB_565会比使用ARGB_8888少消耗2倍的内存
    • 在ImageLoaderConfiguration中配置图片的内存缓存为memoryCache(new WeakMemoryCache()) 或者不使用内存缓存
    • 在DisplayImageOptions选项中设置.imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者imageScaleType(ImageScaleType.EXACTLY)

    我们在使用该框架的时候尽量的使用displayImage()方法去加载图片,loadImage()是将图片对象回调到ImageLoadingListener接口的onLoadingComplete()方法中,需要我们手动去设置到ImageView上面,displayImage()方法中,对ImageView对象使用的是WeakReferences,方便垃圾回收器回收ImageView对象,如果我们要加载固定大小的图片的时候,使用loadImage()方法需要传递一个ImageSize对象,而displayImage()方法会根据ImageView对象的测量值,或者android:layout_widthandroid:layout_height设定的值,或者android:maxWidth 和/或 android:maxHeight设定的值来裁剪图片。

    总结

    • 优点:丰富的配置选项
    • 缺点:有不维护的趋势,可能被当前图片框架替代
    • 特点:三级缓存的策略

    小结

    本人四个库都使用了一遍,对比后发现Fresco确实强大,加载大图 Fresco 最强,有的图 Glide 和 Picasso 加载不出来,换上 Fresco 妥妥的,不过 Fresco 比较庞大,推荐在主要都是图片的 app 中使用,一般的 app 使用 Glide 或 Picasso 就够了!

    Glide 和 Picasso 都是非常完美的库,Glide 加载图像以及磁盘缓存的方式都要优于 Picasso,速度更快,并且 Glide 更有利于减少 OutOfMemoryError 的发生。,GIF 动画是 Glide 的杀手锏,不过 Picasso 的图片质量更高。

    建议使用 Glide,如有需要,可以 将Bitmap 格式换成 ARGB_8888、让 Glide 缓存同时缓存全尺寸和改变尺寸两种。

    2018-12-20

    附件列表

    • 相关阅读:
      UIAutomator环境搭建
      Appium环境搭建
      Java单元测试 Junit TestNG之介绍
      IDEA操作jdbc总结
      tomcat启动失败的解决办法
      Java 图书管理项目
      某某服-EDR终端任意用户登录 0day
      深X服 EDR终端检测系统RCE漏洞复现
      通达OA任意文件上传+文件包含RCE漏洞复现(附自写EXP)
      Joomla-3.4.6远程代码执行漏洞复现
    • 原文地址:https://www.cnblogs.com/baiqiantao/p/4de1411f10de3e687d619aa9d7b2a792.html
    Copyright © 2011-2022 走看看