Android-图片加载库Coil介绍

作者: 多仔百事宅 | 来源:发表于2021-01-21 16:40 被阅读0次

    框架介绍

    Coil是Android上的一个全新的图片加载框架,它的全名叫做coroutine image loader,即协程图片加载库。与传统的图片加载库Glide,Picasso或Fresco等相比。该具有轻量(只有大约1500个方法)、快、易于使用、更现代的API等优势。它支持GIF和SVG,并且可以执行四个默认转换:模糊,圆形裁剪,灰度和圆角。并且是全用Kotlin编写,如果你是纯Kotlin项目的话,那么这个库应该是你的首选。

    这应该是一个很新的一个图片加载库,完全使用kotlin编写,使用了kotlin的协程,图片网络请求方式默认为Okhttp,相比较于我们常用的Picasso,Glide或者Fresco,它有以下几个特点:

    1. 足够快速,它在内存、图片存储、图片的采样、Bitmap重用、暂停\取消下载等细节方面都有很大的优化(相比于上面讲的三大框架);
    2. 足够轻量,只有大概1500个核心方法,当然也是相对于PGF而言的;
    3. 足够新,也足够现代!使用了最新的Koltin协程所编写,充分发挥了CPU的性能,同时也使用了OKHttp、Okio、LifeCycle等比较新式的Android库。

    使用

    github地址为:https://github.com/coil-kt/coil/

    首先需要配置你的AS环境包含Kotlin开发环境,然后添加依赖:

    implementation("io.coil-kt:coil:1.1.1")
    

    要将图像加载到ImageView中,请使用加载扩展功能:

    // URL
    imageView.load("https://www.example.com/image.jpg")
     
    // Resource
    imageView.load(R.drawable.image)
     
    // File
    imageView.load(File("/path/to/image.jpg"))
     
    // And more...
    

    可以使用可选的配置请求:

    imageView.load("https://www.example.com/image.jpg") {
        crossfade(true)
        placeholder(R.drawable.image)
        transformations(CircleCropTransformation())
    }
    

    基本变化:

    Coil默认提供了四种变换:模糊变换(BlurTransformation)、圆形变换(CircleCropTransformation)、灰度变换(GrayscaleTransformation)和圆角变换(RoundedCornersTransformation):

    基础用法:

    imageView.load(IMAGE_URL){
         transformations(GrayscaleTransformation())
    }
    

    直接加入变换就可以, 同时可支持多种变换:

     imageView.load(IMAGE_URL) {
                transformations(GrayscaleTransformation(),
                                RoundedCornersTransformation(topLeft = 2f, topRight = 
                2f,bottomLeft = 40f, bottomRight = 40f))
    }
    

    Gif加载

    Coil基础包中是不支持Gif加载的,需要添加extend包:

    implementation("io.coil-kt:coil-gif:0.9.5")
    

    此时需要更改一下代码的方式,在imageLoader中注册Gif组件:

    val gifImageLoader = ImageLoader(this) {
                componentRegistry {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                        add(ImageDecoderDecoder())
                    } else {
                        add(GifDecoder())
                    }
                }
    }
    

    使用本组件之后,ImageView可直接使用:

    id_image_gif.load(GIF_IMAGE_URL, gifImageLoader)
    

    SVG加载

    Coil也可以进行SVG加载的,同gif一样,也是需要添加extend包的:

    implementation("io.coil-kt:coil-svg:0.9.5")
    

    代码如下:

    val svgImageLoader = ImageLoader(this){
                componentRegistry {
                    add(SvgDecoder(this@MainActivity))
                }
            }
     
    id_image_svg.load(R.drawable.ic_directions_bus_black_24dp, svgImageLoader)
    

    从Glide\Picasso迁移到Coil

    基本的用法的扩展为:

    // Glide
         Glide.with(context)
        .load(url)
        .into(imageView)
     
    // Picasso
         Picasso.get()
        .load(url)
        .into(imageView)
     
    // Coil
       imageView.load(url)
    

    图片设置ScaleType的方式:

    imageView.scaleType = ImageView.ScaleType.FIT_CENTER
     
        // Glide
        Glide.with(context)
         .load(url)
         .placeholder(placeholder)
         .fitCenter()
         .into(imageView)
     
        // Picasso
        Picasso.get()
            .load(url)
            .placeholder(placeholder)
            .fit()
            .into(imageView)
     
        // Coil (autodetects the scale type)
        imageView.load(url) {
         placeholder(placeholder)
    }
    

    相关文章

      网友评论

        本文标题:Android-图片加载库Coil介绍

        本文链接:https://www.haomeiwen.com/subject/uqrdzktx.html