微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

        }
    });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐