android – 如何停止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) {

        }
    });

每个位图都会调整大小到指定的尺寸.因此,如果图像是400×300,它会升级到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,GlideAnimation glideAnimation) {

        }
    });

相关文章

###实现效果*本实例主要实现用ViewPage和Fragment实现选项卡...
一、安装 JDK 下载JDK最新版本,下载地址如下: http://www....
这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...