Image grayscale using glide library

2020-07-22 18:29发布

问题:

I am using glide library to load image url in image view.

Glide.with(context)
                .load(imageurl)
                .apply(RequestOptions.circleCropTransform())
                .into(holder.thumbnail);

Actually, the image is loaded fine with rounded image.

I need the image to be loaded with rounded + grayscale image

Can this be done by using glide lib?

回答1:

You can use Android's android.graphics.ColorMatrix class to set the saturation to 0 for making an ImageView grayscale.

You can achieve what you want in two steps. 1. Use Glide to make the ImageView rounded. 2. After that use ColorMatrix class to make the ImageView grayscale.

Glide.with(context)
.load(imageurl)
.apply(RequestOptions.circleCropTransform())
.into(holder.thumbnail);

ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
holder.thumbnail.setColorFilter(filter);


回答2:

If you are using Kotlin:

            //Grey scale
            val colorMatrix =  ColorMatrix();
            colorMatrix.setSaturation(0.0f);
            val filter =  ColorMatrixColorFilter(colorMatrix);
            itemView.thumb.colorFilter = filter;