Skip to content

Image becomes blurry when loading SVG with Glide

In the app I’m using the following sample to load an SVG image into ImageView:

                .using(Glide.buildStreamModelLoader(Uri.class, context), InputStream.class)
                .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
                .sourceEncoder(new StreamEncoder())
                .cacheDecoder(new FileToStreamDecoder<>(new SvgDecoder()))
                .decoder(new SvgDecoder())
                .listener(new SvgSoftwareLayerSetter())

An ImageView in xml looks like this:


So, the problem can be seen on the image below. The right icon was set from app’s resources, while the left one is loaded from server using Glide. For some reason image is not scaling properly and looks blurry.

images to compare

I’ve already tried the solution from this answer: Image size too small when loading SVG with Glide, but it doesn’t work.


After upgrading to glide v4 the problem has gone. Now I’m using the following code for loading svg:

            .listener(new SvgSoftwareLayerSetter())

And in my AppGlideModule I have:

public void registerComponents(@NonNull Context context, @NonNull Glide glide, Registry registry) {
    registry.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder())
            .prepend(SVG.class, new SvgEncoder())
            .append(InputStream.class, SVG.class, new SvgDecoder());

SvgDrawableTranscoder converts SVG to PictureDrawable with one simple method:

public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, PictureDrawable> {

    public Resource<PictureDrawable> transcode(@NonNull Resource<SVG> toTranscode, @NonNull Options options) {
        SVG svg = toTranscode.get();
        Picture picture = svg.renderToPicture();
        PictureDrawable drawable = new PictureDrawable(picture);
        return new SimpleResource<>(drawable);

And that’s how I implemented SvgEncoder class:

public class SvgEncoder implements ResourceEncoder<SVG>{
    public EncodeStrategy getEncodeStrategy(@NonNull Options options) {
        return EncodeStrategy.SOURCE;

    public boolean encode(@NonNull Resource<SVG> data, @NonNull File file, @NonNull Options options) {
        try {
            SVG svg = data.get();
            Picture picture = svg.renderToPicture();
            picture.writeToStream(new FileOutputStream(file));
            return true;
        } catch (Exception e) {
            return false;