2

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

Mauker
  • 11,237
  • 7
  • 58
  • 76
  • Did you set a scaleType property on the ImageView? – Desdroid Feb 19 '16 at 19:39
  • Yes, it's `android:scaleType="fitXY"`. – Mauker Feb 20 '16 at 02:42
  • I'm not sure, but it's possible this is related to [this AndroidSVG issue](https://github.com/BigBadaboom/androidsvg/issues/79). I haven't had the time to investigate that bug report yet. Is it just `fitXY` mode where this happens? – Paul LeBeau Feb 20 '16 at 06:50

3 Answers3

3

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.

Mauker
  • 11,237
  • 7
  • 58
  • 76
1

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

TreenD
  • 11
  • 3
1

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);
    }
}
Gotchu
  • 11
  • 1