如何停止 Glide 升级?

How to stop Glide upscaling?

我正在使用 Glide image loading library,但在调整位图大小时遇到​​了问题。

使用以下代码时:

Glide.with(getActivity())
    .load(images.get(i))
    .asBitmap().centerCrop()
    .into(new SimpleTarget<Bitmap>(1200, 1200) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {

        }
    });

每个位图都调整为指定的尺寸。所以,如果图像是 400x300,它会放大到 1200 x 1200,这是我不想要的。我要怎么做才能让图片小于指定尺寸时不会调整大小?

我正在指定尺寸,因为我希望在考虑 centerCrop 的情况下调整每张大于指定尺寸的图像;然后如果图像小于指定尺寸,我不想调整它的大小。

I want every image that's bigger than the specified dimensions to be resized taking into account centerCrop; and then if the image is smaller than the specified dimensions, I don't want it to be resized.

您可以通过自定义转换获得此行为:

public class CustomCenterCrop extends CenterCrop {

    public CustomCenterCrop(BitmapPool bitmapPool) {
        super(bitmapPool);
    }

    public CustomCenterCrop(Context context) {
        super(context);
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        if (toTransform.getHeight() > outHeight || toTransform.getWidth() > outWidth) {
            return super.transform(pool, toTransform, outWidth, outHeight);
        } else {
            return toTransform;
        }
    }
}

然后像这样使用它:

Glide.with(getActivity())
    .load(images.get(i))
    .asBitmap()
    .transform(new CustomCenterCrop(getActivity()))
    .into(new SimpleTarget<Bitmap>(1200, 1200) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {

        }
    });