Glide 监听图片加载状态 — 支持静态图、GIF


/**
 * Glide 监听图片加载状态 — 支持静态图、GIF
 */
fun loadImage(context: Context?, url: String?) {
    context ?: return

    if (url.isNullOrEmpty()) {
        return
    }

    val builder = Glide.with(context).load(url)

    if (isGif(url)) {
        builder.asGif()
        builder.diskCacheStrategy(DiskCacheStrategy.SOURCE)
    } else {
        builder.diskCacheStrategy(DiskCacheStrategy.ALL)
    }

    builder.crossFade(300)

    // GifDrawable 继承于 GlideDrawable
    builder.listener(object : RequestListener {
        override fun onException(p0: Exception?, p1: String?, p2: Target?, p3: Boolean): Boolean {
            // 加载失败
            return false
        }

        override fun onResourceReady(p0: GlideDrawable?, p1: String?, p2: Target?, p3: Boolean, p4: Boolean): Boolean {
            // 加载成功
            return false
        }
    }).into(imageView)
}

你可能感兴趣的:(Glide 监听图片加载状态 — 支持静态图、GIF)