Kotlin: Glide加载图片工具类

最近在学习kotlin,发现代码比java简洁不好,下面是kotlin写的Glide工具类

首先引入lib库:

api 'com.github.bumptech.glide:glide:4.9.0'
api 'jp.wasabeef:glide-transformations:3.3.0'

以下直接上代码:

/**
 * glide工具类
 *
 * @author  yimin
 * @date    2020/3/19
 */
class GlideUtils {

    companion object {

        /**
         * 加载图片(String地址)
         */
        fun loadImage(context: Context, url: String, imageView: ImageView) {
            var strategy = DiskCacheStrategy.NONE
            if (url.startsWith("http://") || url.startsWith("https://")) {
                strategy = DiskCacheStrategy.RESOURCE
            }
            val options =
                RequestOptions().centerCrop().placeholder(R.color.white).error(R.color.white)
                    .dontAnimate().diskCacheStrategy(strategy)
            Glide.with(context).load(url).apply(options).into(imageView)
        }

        /**
         * 加载图片(int资源地址)
         */
        fun loadImage(context: Context, res: Int, imageView: ImageView) {
            var strategy = DiskCacheStrategy.NONE
            val options =
                RequestOptions().centerCrop().placeholder(R.color.white).error(R.color.white)
                    .dontAnimate().diskCacheStrategy(strategy)
            Glide.with(context).load(res).apply(options).into(imageView)
        }

        /**
         * 加载图片(uri)
         */
        fun loadImage(context: Context, uri: Uri, imageView: ImageView) {
            var strategy = DiskCacheStrategy.RESOURCE
            val options =
                RequestOptions().centerCrop().placeholder(R.color.white).error(R.color.white)
                    .dontAnimate().diskCacheStrategy(strategy)
            Glide.with(context).load(uri).apply(options).into(imageView)
        }

        /**
         * 加载图片(bitmap)
         */
        fun loadImage(context: Context, bitmap: Bitmap, imageView: ImageView) {
            var strategy = DiskCacheStrategy.RESOURCE
            val options =
                RequestOptions().centerCrop().placeholder(R.color.white).error(R.color.white)
                    .dontAnimate().diskCacheStrategy(strategy)
            Glide.with(context).load(bitmap).apply(options).into(imageView)
        }

        /**
         * 加载图片(String地址)---指定宽高
         */
        fun loadImage(
            context: Context,
            url: String,
            imageView: ImageView,
            width: Int,
            height: Int
        ) {
            var strategy = DiskCacheStrategy.NONE
            if (url.startsWith("http://") || url.startsWith("https://")) {
                strategy = DiskCacheStrategy.RESOURCE
            }
            val options =
                RequestOptions().centerCrop().placeholder(R.color.white).error(R.color.white)
                    .dontAnimate().override(width, height).diskCacheStrategy(strategy)
            Glide.with(context).load(url).apply(options).into(imageView)
        }

        /**
         * 加载圆形图片
         */
        fun loadCircleImage(context: Context, url: String, imageView: ImageView) {
            var strategy = DiskCacheStrategy.NONE
            if (url.startsWith("http://") || url.startsWith("https://")) {
                strategy = DiskCacheStrategy.RESOURCE
            }
            val options = RequestOptions().centerCrop().circleCrop().placeholder(R.color.white)
                .error(R.color.white).dontAnimate().diskCacheStrategy(strategy)
            Glide.with(context).load(url).apply(options).into(imageView)

        }

        /**
         * 加载圆形图片
         */
        fun loadRoundImage(context: Context, url: String, imageView: ImageView) {
            var strategy = DiskCacheStrategy.NONE
            if (url.startsWith("http://") || url.startsWith("https://")) {
                strategy = DiskCacheStrategy.RESOURCE
            }
            val options = RequestOptions().centerCrop().circleCrop().placeholder(R.color.white)
                .error(R.color.white).dontAnimate()
                .transform(
                    RoundedCornersTransformation(
                        15,
                        0,
                        RoundedCornersTransformation.CornerType.ALL
                    )
                )
                .diskCacheStrategy(strategy)

            Glide.with(context).load(url).apply(options).into(imageView)
        }

        /**
         * 加载圆形图片---指定圆角半径
         */
        fun loadRoundImage(context: Context, url: String, imageView: ImageView, radius: Int) {
            var strategy = DiskCacheStrategy.NONE
            if (url.startsWith("http://") || url.startsWith("https://")) {
                strategy = DiskCacheStrategy.RESOURCE
            }
            val options = RequestOptions().centerCrop().circleCrop().placeholder(R.color.white)
                .error(R.color.white).dontAnimate()
                .transform(
                    RoundedCornersTransformation(
                        radius,
                        0,
                        RoundedCornersTransformation.CornerType.ALL
                    )
                )
                .diskCacheStrategy(strategy)

            Glide.with(context).load(url).apply(options).into(imageView)
        }

        /**
         * 加载圆角图片-指定任意部分圆角(图片上、下、左、右四个角度任意定义)和半径
         */
        fun loadCustomRoundImage(
            context: Context,
            url: String,
            imageView: ImageView,
            type: RoundedCornersTransformation.CornerType,
            raduis: Int
        ) {
            var strategy = DiskCacheStrategy.NONE
            if (url.startsWith("http://") || url.startsWith("https://")) {
                strategy = DiskCacheStrategy.RESOURCE
            }
            val options = RequestOptions().centerCrop().circleCrop().placeholder(R.color.white)
                .error(R.color.white).dontAnimate()
                .transform(RoundedCornersTransformation(raduis, 0, type))
                .diskCacheStrategy(strategy)
            Glide.with(context).load(url).apply(options).into(imageView)
        }

    }
}

 

 上面写了几个常用的方法,需要可自行添加。

你可能感兴趣的:(Android,Kotlin,android,kotlin,Glide)