Glide图片加载封装

1、导入依赖包:

implementation  'com.github.bumptech.glide:glide:4.11.0'
implementation  'com.github.bumptech.glide:compiler:4.11.0'

// 指定圆角效果
implementation 'jp.wasabeef:glide-transformations:4.3.0'

2、ImageLoader.kt
图片加载工具类,使用扩展函数对第三方图片框架的简单封装;

package com.jrzfveapp.extension
import androidx.annotation.DrawableRes
import com.blankj.utilcode.util.SizeUtils
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

/**
 * 指定圆角
 * @param cornerType RoundedCornersTransformation.CornerType
 */
fun ImageView.load(url: String?, round: Float = 0f, cornerType: RoundedCornersTransformation.CornerType, @DrawableRes placeImg: Int? = null) {
    val option = RequestOptions()
        .diskCacheStrategy(DiskCacheStrategy.ALL)//全部缓存,原图与压缩图
        .skipMemoryCache(false)//注:是否跳过内存缓存,设置为false,如为true的话每次闪烁也正常~
        .dontAnimate()//取消Glide自带的动画

    if (placeImg != null) {
        option.placeholder(placeImg)
    }

    if (round != 0f) {
        option.transform(CenterCrop(),
            RoundedCornersTransformation(round.toInt(), 0,cornerType))
    }

    Glide.with(this.context)
        .load(url ?: "")
        .apply(option)
        .into(this)
}

/**
 * Glide加载图片,可以指定圆角弧度。
 *
 * @param url 图片地址
 * @param round 圆角,单位dp
 * @param placeImg 占位图
 */
fun ImageView.load(url: String?, round: Float = 0f, @DrawableRes placeImg: Int? = null) {
    val option = RequestOptions()
        .diskCacheStrategy(DiskCacheStrategy.ALL)//全部缓存,原图与压缩图
        .skipMemoryCache(false)//注:是否跳过内存缓存,设置为false,如为true的话每次闪烁也正常~
        .dontAnimate()//取消Glide自带的动画

    if (placeImg != null) {
        option.placeholder(placeImg)
    }

    if (round != 0f) {
        option.transform(CenterCrop(), RoundedCorners(SizeUtils.dp2px(round)))
    }

    Glide.with(this.context)
        .load(url ?: "")
        .apply(option)
        .into(this)
}

fun ImageView.load(
        url: String,
        @DrawableRes placeImg: Int? = null,
        vararg transformations: Transformation<Bitmap>
) {
    val option = RequestOptions();
    if (transformations != null) {
        transformations.forEach {
            option.transform(it)
        }
    }
    if (placeImg == null) {
        Glide.with(this.context).load(url).apply(option).into(this)
    } else {
        Glide.with(this.context).load(url).apply(option).placeholder(placeImg).into(this)
    }
}

fun ImageView.load(
        @DrawableRes img: Int,
        @DrawableRes placeImg: Int? = null,
        vararg transformations: Transformation<Bitmap>
) {
    val option = RequestOptions();
    if (transformations != null) {
        transformations.forEach {
            option.transform(it)
        }
    }
    if (placeImg == null) {
        Glide.with(this.context).load(img).apply(option).into(this)
    } else {
        Glide.with(this.context).load(img).apply(option).placeholder(placeImg).into(this)
    }
}

3、使用:

  imageView.load(
                item.coverUrl,
                context.resources.getDimension(R.dimen.dp_8),
                RoundedCornersTransformation.CornerType.TOP,
                placeImg = R.mipmap.ic_home_template_none
            )

参考链接:

Glide官网

Android Glide 4.9 常见方法总结

猜你喜欢

转载自blog.csdn.net/zhijiandedaima/article/details/131081762