[app] Fix svg rendering
This commit is contained in:
parent
04cdb6c950
commit
bfe905fabf
|
|
@ -24,7 +24,7 @@ import android.content.Context;
|
|||
import android.content.pm.PackageInfo;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.AdaptiveIconDrawable;
|
||||
import android.graphics.drawable.PictureDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
|
|
@ -40,6 +40,7 @@ import java.io.InputStream;
|
|||
|
||||
import io.github.lsposed.manager.App;
|
||||
import io.github.lsposed.manager.R;
|
||||
import io.github.lsposed.manager.util.svg.ExternalFileResolver;
|
||||
import io.github.lsposed.manager.util.svg.SvgDecoder;
|
||||
import io.github.lsposed.manager.util.svg.SvgDrawableTranscoder;
|
||||
import me.zhanghai.android.appiconloader.glide.AppIconModelLoader;
|
||||
|
|
@ -53,7 +54,8 @@ public class AppModule extends AppGlideModule {
|
|||
context.getApplicationInfo().loadIcon(context.getPackageManager()) instanceof AdaptiveIconDrawable, context));
|
||||
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(App.getOkHttpClient());
|
||||
registry.replace(GlideUrl.class, InputStream.class, factory);
|
||||
registry.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder())
|
||||
SVG.registerExternalFileResolver(new ExternalFileResolver());
|
||||
registry.register(SVG.class, Drawable.class, new SvgDrawableTranscoder(context))
|
||||
.append(InputStream.class, SVG.class, new SvgDecoder());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
package io.github.lsposed.manager.util.svg;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Picture;
|
||||
import android.util.Base64;
|
||||
|
||||
import com.caverock.androidsvg.SVG;
|
||||
import com.caverock.androidsvg.SVGExternalFileResolver;
|
||||
|
||||
public class ExternalFileResolver extends SVGExternalFileResolver {
|
||||
@Override
|
||||
public Bitmap resolveImage(String filename) {
|
||||
if (filename.startsWith("data:image/svg+xml;base64,")) {
|
||||
// com.shatyuka.zhiliao
|
||||
try {
|
||||
String base64 = filename.substring(filename.indexOf(","));
|
||||
SVG svg = SVG.getFromString(new String(Base64.decode(base64, Base64.DEFAULT)));
|
||||
float width = svg.getDocumentWidth();
|
||||
float height = svg.getDocumentHeight();
|
||||
Bitmap bitmap;
|
||||
if (width > 0 && height > 0) {
|
||||
bitmap = Bitmap.createBitmap(Math.round(width), Math.round(width), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
svg.renderToCanvas(canvas);
|
||||
} else {
|
||||
Picture picture = svg.renderToPicture();
|
||||
bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawPicture(picture);
|
||||
}
|
||||
return bitmap;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return super.resolveImage(filename);
|
||||
}
|
||||
}
|
||||
|
|
@ -20,7 +20,13 @@
|
|||
|
||||
package io.github.lsposed.manager.util.svg;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Picture;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.PictureDrawable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
|
@ -35,14 +41,33 @@ 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> {
|
||||
public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, Drawable> {
|
||||
private final Resources resources;
|
||||
|
||||
public SvgDrawableTranscoder(Context context) {
|
||||
resources = context.getResources();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Resource<PictureDrawable> transcode(
|
||||
public Resource<Drawable> transcode(
|
||||
@NonNull Resource<SVG> toTranscode, @NonNull Options options) {
|
||||
SVG svg = toTranscode.get();
|
||||
Picture picture = svg.renderToPicture();
|
||||
PictureDrawable drawable = new PictureDrawable(picture);
|
||||
float width = svg.getDocumentWidth();
|
||||
float height = svg.getDocumentHeight();
|
||||
|
||||
Drawable drawable;
|
||||
if (width > 0 && height > 0) {
|
||||
float density = resources.getDisplayMetrics().density;
|
||||
Bitmap bitmap = Bitmap.createBitmap(Math.round(width * density), Math.round(height * density), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.scale(density, density);
|
||||
svg.renderToCanvas(canvas);
|
||||
drawable = new BitmapDrawable(resources, bitmap);
|
||||
} else {
|
||||
Picture picture = svg.renderToPicture();
|
||||
drawable = new PictureDrawable(picture);
|
||||
}
|
||||
return new SimpleResource<>(drawable);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue