Mauker
Mauker

Reputation: 11497

Image size too small when loading SVG with Glide

I'm using Glide to load images.

On my app, I'm using the following sample to load an 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);

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. Am I doing something wrong?

The scaleType is: android:scaleType="fitXY"

img_20160219_155824

Upvotes: 2

Views: 3031

Answers (3)

Gotchu
Gotchu

Reputation: 11

The unchecked answer helps me a lot!

he glide's API has changed and now my code looks like this:

SvgDecoder.java:

public Resource<SVG> decode(
        @NonNull InputStream source, int width, int height, @NonNull Options options)
        throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);
        if (svg.getDocumentViewBox() == null)
            svg.setDocumentViewBox(0f, 0f, svg.getDocumentWidth(), svg.getDocumentHeight());

        if (width != SIZE_ORIGINAL) {
            svg.setDocumentWidth(width);
        }
        if (height != SIZE_ORIGINAL) {
            svg.setDocumentHeight(height);
        }
        return new SimpleResource<>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream", ex);
    }
}

Upvotes: 1

TreenD
TreenD

Reputation: 11

Addition to the accepted answer - for me solution didn't work, the reason behind this was no proper viewbox in svg

Adding

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

before changing svg width/height fixed scaling finally

Upvotes: 1

Mauker
Mauker

Reputation: 11497

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

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

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.

Upvotes: 3

Related Questions