如何停止Glide upscaling?

Jac*_*ack 6 android android-glide

我正在使用Glide图像加载库,我在调整位图大小时遇到​​了问题.

使用以下代码时:

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

        }
    });
Run Code Online (Sandbox Code Playgroud)

每个位图都会调整大小到指定的尺寸.因此,如果图像是400x300,它会升级到1200 x 1200,这是我不想要的.如何使图像小于指定的尺寸,它不会调整大小?

我正在指定尺寸,因为我希望每个大于指定尺寸的图像都要考虑到尺寸centerCrop; 然后如果图像小于指定的尺寸,我不希望它的大小调整.

Mat*_*ini 7

我想要考虑到centerCrop,调整大于指定尺寸的每个图像的大小; 然后如果图像小于指定的尺寸,我不希望它的大小调整.

您可以使用自定义转换获取此行为:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

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

        }
    });
Run Code Online (Sandbox Code Playgroud)