0

I am using the svg-android library to load and display SVG images:

ImageView iv = (ImageView)findViewById(R.id.imgLoginLogo);

// Set the background color to white
iv.setBackgroundColor(Color.WHITE);

// Parse the SVG file from the resource
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);

// Get a drawable from the parsed SVG and set it as the drawable for the ImageView
Drawable d = svg.createPictureDrawable();
iv.setImageDrawable(d);

Problem is the drawable I get has zero dimensions, so it is not visible.

What is wrong here ?

Thanks

Shimon

1 Answer 1

1

I use this to create a drawable:

public static Bitmap createBitmap(InputStream in, int w, int h) {
    com.larvalabs.svgandroid.SVG svg = new SVGBuilder().readFromInputStream(in).build();

    Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Picture pic = svg.getPicture();
    canvas.drawPicture(pic, new Rect(0, 0, w, h));
    return bitmap;
}

Notice that you must care about recycling the bitmap after done with it.

1
  • 1
    Thanks for responding. Turns out the problem was a missing line: had to add the line: imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); Apr 27, 2015 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.