更好的方法(尤其是 Glide 4 不再接受一个以上的BitmapTransformation
)是创建一个CombinedTransformation
,如下所示:
class CombinedTransformation(vararg val transformations: Transformation<Bitmap>)
: Transformation<Bitmap> {
override fun transform(context: Context, resource: Resource<Bitmap>,
outWidth: Int, outHeight: Int): Resource<Bitmap> {
var out = resource
for (transformation in transformations) {
out = transformation.transform(context, out, outWidth, outHeight)
}
return out
}
override fun updateDiskCacheKey(messageDigest: MessageDigest) {
transformations.forEach { it.updateDiskCacheKey(messageDigest) }
}
}
然后像这样使用它(再次,Glide 4):
val options = RequestOptions()
options.transform(CombinedTransformation(CenterCrop(...), BlurTransformation(...)))
Glide.with(context).load(url).apply(options).into(imageView)