使用Glide加载SVG时图像尺寸太小 [英] Image size too small when loading SVG with Glide

查看:362
本文介绍了使用Glide加载SVG时图像尺寸太小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Glide 加载图像.

I'm using Glide to load images.

在我的应用上,我使用以下示例将SVG图像加载到CardView内部的ImageView中.

On my app I'm using the following sample to load a SVG image into my ImageView that is inside a CardView.

GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> requestBuilder;

requestBuilder = Glide.with(mContext)
        .using(Glide.buildStreamModelLoader(Uri.class, mContext), InputStream.class)
        .from(Uri.class)
        .as(SVG.class)
        .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
        .sourceEncoder(new StreamEncoder())
        .cacheDecoder(new FileToStreamDecoder<>(new SVGDecoder()))
        .decoder(new SVGDecoder())
        .placeholder(R.drawable.modulo)
        .error(R.drawable.banner_error)
        .animate(android.R.anim.fade_in)
        .listener(new SvgSoftwareLayerSetter<Uri>());

requestBuilder
        .diskCacheStrategy(DiskCacheStrategy.NONE)
        .load(Uri.parse("http://foo.bar/blah"))
        .into(cardHolder.iv_card);

ImageView在XML上具有固定宽度102dp和固定高度94dp.但是,图像在加载后变得越来越小.我在做错什么吗?

The ImageView have a fixed width of 102dp and a fixed height of 94dp on the XML. But the images are getting smaller than they should after they are being loaded. Am I doing something wrong?

scaleType为:android:scaleType="fitXY"

The scaleType is: android:scaleType="fitXY"

推荐答案

我决定以有关libs信息库的问题,然后我得以解决该问题.

I decided to open this question as an issue on the libs repository, and then I was able to fix the problem.

事实证明,问题与我的SVG具有固定大小有关,因此要解决此问题,我必须修改我的SvgDecoder.decode方法,并添加以下三行:

As it turned out, the problem was related to my SVG having a fixed size, so to fix it I had to modify my SvgDecoder.decode method, and add those three lines:

svg.setDocumentWidth(width);
svg.setDocumentHeight(height);
svg.setDocumentPreserveAspectRatio(PreserveAspectRatio.STRETCH);

该方法现在如下所示:

public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);

        svg.setDocumentWidth(width);
        svg.setDocumentHeight(height);
        svg.setDocumentPreserveAspectRatio(PreserveAspectRatio.STRETCH);

        return new SimpleResource<>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream.", ex);
    }
}

现在它可以正常工作了.

Now it's working as it should.

这篇关于使用Glide加载SVG时图像尺寸太小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆