Skip to content

Commit

Permalink
svg support
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed Dec 5, 2019
1 parent 53236c3 commit 16615b1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ dependencies {
implementation 'am.util:viewpager:25.3.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
implementation 'com.jakewharton:disklrucache:2.0.2'
implementation 'com.caverock:androidsvg:1.2.1'
}
35 changes: 35 additions & 0 deletions app/src/main/java/com/bumptech/glide/samples/svg/SvgDecoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.bumptech.glide.samples.svg;

import androidx.annotation.NonNull;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.SimpleResource;
import com.caverock.androidsvg.SVG;
import com.caverock.androidsvg.SVGParseException;

import java.io.IOException;
import java.io.InputStream;

/**
* Decodes an SVG internal representation from an {@link InputStream}.
*/
public class SvgDecoder implements ResourceDecoder<InputStream, SVG> {

@Override
public boolean handles(@NonNull InputStream source, @NonNull Options options) {
// TODO: Can we tell?
return true;
}

public Resource<SVG> decode(
@NonNull InputStream source, int width, int height, @NonNull Options options)
throws IOException {
try {
SVG svg = SVG.getFromInputStream(source);
return new SimpleResource<>(svg);
} catch (SVGParseException ex) {
throw new IOException("Cannot load SVG from stream", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.bumptech.glide.samples.svg;

import android.graphics.Picture;
import android.graphics.drawable.PictureDrawable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.SimpleResource;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
import com.caverock.androidsvg.SVG;

/**
* Convert the {@link SVG}'s internal representation to an Android-compatible one ({@link Picture}).
*/
public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, PictureDrawable> {
@Nullable
@Override
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package soko.ekibun.bangumi.util;

import android.content.Context;
import android.graphics.drawable.PictureDrawable;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.samples.svg.SvgDecoder;
import com.bumptech.glide.samples.svg.SvgDrawableTranscoder;
import com.caverock.androidsvg.SVG;
import okhttp3.*;
import okio.*;

Expand All @@ -20,9 +25,15 @@
@GlideModule
public class ProgressAppGlideModule extends AppGlideModule {

// Disable manifest parsing to avoid adding similar modules twice.
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
super.registerComponents(context, glide, registry);
public boolean isManifestParsingEnabled() {
return false;
}

public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
registry.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder())
.append(InputStream.class, SVG.class, new SvgDecoder());
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(new Interceptor() {
@Override
Expand Down

0 comments on commit 16615b1

Please sign in to comment.