简体   繁体   English

使用 Glide 加载 SVG 时图像尺寸太小

[英]Image size too small when loading SVG with Glide

I'm using Glide to load images.我正在使用Glide加载图像。

On my app, I'm using the following sample to load an SVG image into my ImageView that is inside a CardView .在我的应用程序中,我使用以下示例将 SVG 图像加载到ImageView内的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);

The ImageView has a fixed width of 102dp and a fixed height of 94dp in the XML. But the images are getting smaller than they should after they are being loaded. ImageView的固定宽度为102dp ,XML 的固定高度为94dp 。但是图像在加载后变得比应有的小。 Am I doing something wrong?难道我做错了什么?

The scaleType is: android:scaleType="fitXY" scaleType 是: android:scaleType="fitXY"

img_20160219_155824

I decided to open this question as an issue on the libs repository , and then I was able to fix the problem.我决定在 libs 存储库上将此问题作为问题打开,然后我就能够解决该问题。

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 具有固定大小有关,因此要修复它,我必须修改我的SvgDecoder.decode方法,并添加以下三行:

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

The method now looks like this:该方法现在看起来像这样:

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.现在它可以正常工作。

Addition to the accepted answer - for me solution didn't work, the reason behind this was no proper viewbox in svg除了接受的答案 - 对我来说解决方案不起作用,这背后的原因是 svg 中没有合适的视图框

Adding添加

if (svg.documentViewBox == null)
svg.setDocumentViewBox(0f, 0f, svg.documentWidth, svg.documentHeight)
        

before changing svg width/height fixed scaling finally在最终更改 svg 宽度/高度固定缩放之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM