Android Coil总结

发布于:2025-03-07 ⋅ 阅读:(96) ⋅ 点赞:(0)

Android Coil总结

概述

Coil 是一个用于 Android 的 Kotlin 图像加载库,旨在简化图像加载和显示的过程。它基于 Kotlin 协程,提供了简洁的 API 和高效的性能。

添加依赖

implementation "io.coil-kt:coil:2.4.0"

用法

基本用法

// 加载网络图片
binding.imageView1.load("https://www.wanandroid.com/resources/image/pc/logo.png")

// 加载本地资源图片
binding.imageView2.load(R.drawable.logo)

// 加载本地路径图片
val file = File(cacheDir.path + "/logo.png")
binding.imageView3.load(file)

占位图

binding.imageView1.load(imgUrl) {
    crossfade(true) // 淡入淡出
    placeholder(R.drawable.placeholder) // 加载时占位
    error(R.drawable.error) // 加载失败占位
}

变形

binding.imageView1.load(imgUrl) {
    transformations(CircleCropTransformation()) // 圆形
}
binding.imageView2.load(imgUrl) {
    transformations(RoundedCornersTransformation(16F)) // 圆角
}

自定义ImageLoader

val imageLoader = ImageLoader.Builder(context).crossfade(true).build()
val request = ImageRequest.Builder(context)
    .data(imgUrl)
    .target(binding.imageView1)
    .build()
imageLoader.enqueue(request)

取消加载

val disposable = binding.imageView1.load(imgUrl)
disposable.dispose() // 取消加载

协程支持

Coil 基于 Kotlin 协程,可以协程中使用。

lifecycleScope.launch {
    val imageLoader = context.imageLoader
    val request = ImageRequest.Builder(context)
        .data(imgUrl)
        .build()
    val drawable = imageLoader.execute(request).drawable
    binding.imageView1.setImageDrawable(drawable)
}

缓存

binding.imageView1.load(imgUrl) {
    diskCachePolicy(CachePolicy.ENABLED) // 启用磁盘缓存
    memoryCachePolicy(CachePolicy.ENABLED) // 启用内存缓存
}

清除缓存

val imageLoader = context.imageLoader
imageLoader.memoryCache?.clear()
imageLoader.diskCache?.clear()

监听

binding.imageView1.load(imgUrl) {
    listener(
        onStart = { Log.e("TAG", "开始") },
        onSuccess = { request, result -> Log.e("TAG", "成功") },
        onError = { request, result -> Log.e("TAG", "失败") },
        onCancel = { Log.e("TAG", "取消") }
    )
}

简单封装

object CoilImageLoader {

    private lateinit var imageLoader: ImageLoader

    // 初始化 Coil 的 ImageLoader
    // 可以在Application中初始化
    fun init(context: Context) {
        imageLoader = ImageLoader.Builder(context)
            .crossfade(true) // 启用淡入淡出效果
            .build()
    }

    // 加载网络图片
    fun loadImage(imageView: ImageView, url: String) {
        imageView.load(url, imageLoader) {
            placeholder(R.drawable.placeholder) // 设置占位图
            error(R.drawable.error) // 设置错误图
        }
    }

    // 加载圆形图片
    fun loadCircleImage(imageView: ImageView, url: String) {
        imageView.load(url, imageLoader) {
            transformations(CircleCropTransformation()) // 圆形裁剪
        }
    }

    // 加载圆角图片
    fun loadRoundedCornersImage(imageView: ImageView, url: String, radius: Float) {
        imageView.load(url, imageLoader) {
            transformations(RoundedCornersTransformation(radius)) // 圆角
        }
    }

    // 清除内存缓存
    fun clearMemoryCache() {
        imageLoader.memoryCache?.clear()
    }

    // 清除磁盘缓存
    fun clearDiskCache(context: Context) {
        imageLoader.diskCache?.clear()
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到