Decoding SVG with AndroidSVG and Universal Image Loader

家住魔仙堡 提交于 2019-12-13 02:38:37

问题


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:


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.



来源:https://stackoverflow.com/questions/20960890/decoding-svg-with-androidsvg-and-universal-image-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!