安卓中的Glide多重转换

7

我一直在使用Glide来加载我的应用程序中的图像。我有一个自定义转换器,我在加载ImageView中的图像时使用它。
问题是我想在获取的图像上同时应用我的自定义转换和centerCrop。但是Glide只使用了我的自定义转换,并使用fitXYImageView中显示图像。
以下是我的代码:

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

我该如何达到想要的结果?非常感谢您提供的任何帮助。
6个回答

10

在 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);

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);

2
您可以这样应用多个转换:
 Glide.with(getContext())
            .load(url)
            .bitmapTransform(new CenterCrop(getContext()), new BlurTransformation(getContext(), BLUR_RADIUS), new GrayscaleTransformation(getContext()))
            .into(imageView);

2

2021语法...

看起来现在你需要到处使用"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://dev59.com/ZnA75IYBdhLWcg3wZ4Jr#66459077

2
在Glide 4.11中,我们可以使用MultiTransformation与一系列的转换:
GlideApp.with(imageView)
    .load(url)
    .transform(MultiTransformation(CenterCrop(), RoundedCorners(10), Rotate(30)))
    // .error(...)
    .into(imageView)

1
更好的方法(特别是对于不再接受多个BitmapTransformation的Glide 4)是创建一个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)

感谢使用CombinedTransformation(CenterCrop(...), BlurTransformation(...))!我明白我们可以使用MultiTransformation(CenterCrop(), RoundedCorners(10)) - CoolMind

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接