Decoding SVG with AndroidSVG and Universal Image L

2019-08-06 19:30发布

I am trying to display svgs with uil. I wrote my own imagedecoder using androidsvg (http://code.google.com/p/androidsvg/) like this:

public class SVGImageDecoder implements ImageDecoder {

@Override
public Bitmap decode(ImageDecodingInfo imageDecodingInfo) throws IOException {
    Bitmap decodedBitmap = null;

    InputStream inputStream = getImageStream(imageDecodingInfo);
    SVG svg = null;
    try {
        svg = SVG.getFromInputStream(inputStream);
    } catch (SVGParseException e) {
        e.printStackTrace();
    }
    Picture picture = svg.renderToPicture();
    PictureDrawable pictureDrawable = new PictureDrawable(picture);
    decodedBitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),
            pictureDrawable.getIntrinsicHeight(), Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(decodedBitmap);
    // Clear background to white
    canvas.drawRGB(255, 255, 255);
    svg.renderToCanvas(canvas);
    return decodedBitmap;
}

protected InputStream getImageStream(ImageDecodingInfo decodingInfo) throws IOException {
    return decodingInfo.getDownloader().getStream(decodingInfo.getImageUri(), decodingInfo.getExtraForDownloader());
}
}

The SVG is displayed, but following problems occurs:

1.) Colors and pictures are not rendered and displayed 2.) The rendering takes too long and the logcat shows something like this:

01-06 23:48:15.310  15505-15506/de.phcom.epaper2 D/dalvikvm﹕ GC_CONCURRENT freed 2048K, 10% free 26540K/29187K, paused 1ms+29ms
01-06 23:48:15.645  15505-15506/de.phcom.epaper2 D/dalvikvm﹕ GC_CONCURRENT freed 2046K, 10% free 26541K/29187K, paused 2ms+27ms
01-06 23:48:15.970  15505-15506/de.phcom.epaper2 D/dalvikvm﹕ GC_CONCURRENT freed 2047K, 10% free 26542K/29187K, paused 1ms+28ms
01-06 23:48:16.305  15505-15506/de.phcom.epaper2 D/dalvikvm﹕ GC_CONCURRENT freed 2049K, 10% free 26541K/29187K, paused 2ms+29ms
01-06 23:48:16.650  15505-15506/de.phcom.epaper2 D/dalvikvm﹕ GC_CONCURRENT freed 2051K, 10% free 26538K/29187K, paused 1ms+27ms

So, how can I improve the performance and solve the problems? Is there a better way to integrate svgs with uil?

1条回答
forever°为你锁心
2楼-- · 2019-08-06 19:37

Your SVG basically just consists of an <image> element that references a big external image of your entire page. This is not really using SVG in the best way.

Anyway, the answer to your question is as follows:

The reason that your image isn't displaying is that AndroidSVG doesn't know where to find it. You have to create an implementation of the SVGExternalFileResolver class and pass it to SVG using the registerExternalFileResolver() method.

In your implementation of SVGExternalFileResolver you need to override the resolveImage() method. When the render needs to plot the image, it will pass in the filename from the SVG file (in this case "page_0001_img68.png") and you have to load and decode the PNG and pass back a Bitmap object.

查看更多
登录 后发表回答