Android中的Glide多重转换

Android中的Glide多重转换,android,android-glide,Android,Android Glide,我一直在使用Glide在我的应用程序中加载图像。我在ImageView中加载图像时使用了一个自定义转换 问题是我想对获取的图像应用自定义转换¢erCrop。但Glide仅使用我的自定义转换,并使用fitXY在ImageView中显示图像 这是我的密码: Glide.with(context) .load(uri) .placeholder(R.drawable.image_id) .transform(new CustomTransformation(contex

我一直在使用Glide在我的应用程序中加载图像。我在
ImageView
中加载图像时使用了一个自定义转换
问题是我想对获取的图像应用自定义转换&
centerCrop
。但Glide仅使用我的自定义转换,并使用
fitXY
ImageView
中显示图像
这是我的密码:

Glide.with(context)
    .load(uri)
    .placeholder(R.drawable.image_id)
    .transform(new CustomTransformation(context))
    .centerCrop()
    .into(imageView);

我如何达到预期的结果?任何帮助都将不胜感激。

制作您自己的
自定义转换
,该转换扩展了
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);

您可以像这样应用多个转换:

 Glide.with(getContext())
            .load(url)
            .bitmapTransform(new CenterCrop(getContext()), new BlurTransformation(getContext(), BLUR_RADIUS), new GrayscaleTransformation(getContext()))
            .into(imageView);
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不再接受一个以上的
位图转换
)是创建一个
组合转换
,如下所示:

 Glide.with(getContext())
            .load(url)
            .bitmapTransform(new CenterCrop(getContext()), new BlurTransformation(getContext(), BLUR_RADIUS), new GrayscaleTransformation(getContext()))
            .into(imageView);
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 v4.6.1中,我发现
多重转换
类使这变得很容易:

MultiTransformation<Bitmap> multiTransformation = new MultiTransformation<>(new CustomTransformation(), new CircleCrop());

Glide.with(DemoActivity.this).load(file)
                .apply(RequestOptions.bitmapTransform(multiTransformation))
                .into(mPreviewImageView);
MultiTransformation MultiTransformation=新的多重转换(新的CustomTransformation(),新的CircleCrop());
Glide.with(DemoActivity.this).load(文件)
.apply(请求选项.bitmapTransform(多重转换))
.进入(mPreviewImageView);

在Glide 4.11中,我们可以使用
多重转换
和转换列表:

GlideApp.with(imageView)
    .load(url)
    .transform(MultiTransformation(CenterCrop(), RoundedCorners(10), Rotate(30)))
    // .error(...)
    .into(imageView)
2021语法。。。 看来你现在需要“新”了:

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金额,例如)

感谢
组合转换(中心裁剪(…)、模糊转换(…)
!我知道我们可以使用
多重转换(CenterCrop(),RoundedCorners(10))