我一直在使用Glide在我的应用中加载图片。我在ImageView
中加载图片时使用了自定义转换
问题是我想应用我的自定义转换&提取的图像均为centerCrop
。但Glide仅使用我的自定义转换,并在ImageView
中使用fitXY
显示图像
这是我的代码:
Glide.with(context)
.load(uri)
.placeholder(R.drawable.image_id)
.transform(new CustomTransformation(context))
.centerCrop()
.into(imageView);
如何达到预期效果?任何帮助将不胜感激。
答案 0 :(得分:4)
自制CustomTransformation
扩展CenterCrop
,然后在进行自定义转换之前覆盖transform()
来电super
。
例如:
Glide.with(Context)
.load(url)
.asBitmap()
.transform(new CenterCrop(context) {
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
// Call super to have your image center cropped
toTransform = super.transform(pool, toTransform, outWidth, outHeight);
// Apply your own custom transformation
return ImageUtils.fastblur(toTransform, BLUR_RADIUS);
}
@Override
public String getId() {
return "com.example.imageid"
}
})
.placeholder(placeholder)
.into(imageView);
答案 1 :(得分:4)
在Glide v4.6.1中,我发现MultiTransformation
类使这很容易:
MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(new CustomTransformation(), new CircleCrop());
Glide.with(DemoActivity.this).load(file)
.apply(RequestOptions.bitmapTransform(multiTransformation))
.into(mPreviewImageView);
答案 2 :(得分:2)
您可以应用以下多种转换:
Glide.with(getContext())
.load(url)
.bitmapTransform(new CenterCrop(getContext()), new BlurTransformation(getContext(), BLUR_RADIUS), new GrayscaleTransformation(getContext()))
.into(imageView);
答案 3 :(得分:1)
看来你现在需要“new”了:
protected void setPicture() {
String url = doc.get("image");
// the rounding of the products_box is 12dp
int twelve = _dp(12);
Glide.with(itemView.getContext())
.load(url)
.transform(
new MultiTransformation(
new CenterCrop(),
new RoundedCorners(twelve)))
.into(happy_product_image);
}
(请注意,像往常一样,您需要转换 DP 金额,例如 https://stackoverflow.com/a/66459077/294884 )
答案 4 :(得分:0)
更好的方式(特别是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)
答案 5 :(得分:0)
在Glide 4.11中,我们可以将MultiTransformation
与转换列表一起使用:
GlideApp.with(imageView)
.load(url)
.transform(MultiTransformation(CenterCrop(), RoundedCorners(10), Rotate(30)))
// .error(...)
.into(imageView)