Glide 加载失败 给默认图 圆角

2020-12-30  本文已影响0人  夜明智灵

设置 RequestBuilder的thumbnail 就可以了
参考
这里使用的Kotlin 定义的 ImageView的扩展方法

fun ImageView.load(
    res: Any?,
    radius: Int? = null,
    drawable: Drawable? = null,
    listener: RequestListener<Drawable>? = null
) {
    var builder = Glide.with(this).load(res).listener(listener)
    if (radius != null || drawable != null) {
        var options = RequestOptions()
        drawable?.let {
            options = options.placeholder(drawable).error(drawable)
        }
        radius?.let {
            val transformations = arrayOf(CenterCrop(), RoundedCorners(radius))
            options = options.transform(*transformations)
            builder.thumbnail(loadTransform(context, drawable, *transformations))
        }
        builder = builder.apply(options)

    }
    builder.into(this)
}

/**
 * 处理需要圆角但加载失败的情况
 */

private fun loadTransform(
    context: Context,
    drawable: Drawable?,
    vararg transformations: Transformation<Bitmap>
): RequestBuilder<Drawable>? {
    if (drawable == null)
        return null
    return Glide.with(context).load(drawable).apply(RequestOptions().transform(*transformations))
}
上一篇下一篇

猜你喜欢

热点阅读