Sunday, 6 January 2013

Display an SVG inside an ImageView or a Canvas


Objective

Display an SVG inside an ImageView or a Canvas.

SVG are really convenient because you don't have to create an image for each density,  since it can be scaled with no loss of quality.
Here is an example of an App entirely done with SVG :

Issue


The Android framework does not support SVG by default.


Solution

Steps
1. Download the latest version of the svg library here : http://code.google.com/p/svg-android/downloads/list
2. Add the jar to your project by copying it into the libs directory
3. Copy your SVG in your res/raw or assets/ directory
4. Load the SVG and create a Drawable or PictureDrawable depending on your need.


Details
1. Download the latest version of the svg library here: 

This library offers a limited support of SVG and is not maintained anymore.

You can eventually download a fork here which has more features of SVG supported  :
List of new features of this version:

2. Add the jar to your project by copying it into the libs/ directory

If you don't have a libs/ directory, create one at the root of your project and copy the jar inside. In Eclipse, this should automatically add it to your build path.

3. Copy your SVG in your res/raw or assets/ directory

You can copy your image directly in the res/raw folder or you can organize them in different directories if you use the assets/ directory.

4. Load the SVG and create a Drawable or PictureDrawable depending on your need. 

If your image is in the raw folder and you want to display it in an ImageView, you can use the following code :

   
        // 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
        imageView.setImageDrawable(svg.createPictureDrawable());

If your SVG is in the asset directory, you can display it like this :

 
   try {
        // Specify the path (relative to the 'assets' folder)
        final SVG svg = SVGParser.getSVGFromAsset(getAssets(), "android.svg");
        imageView.setImageDrawable(svg.createPictureDrawable());
    } catch (IOException e) {
        // Handle IOException here
    }

If you want to draw your SVG directly on a canvas, you will have to create a Picture instead of a PictureDrawable:
 
    // Parse the SVG file from the resource
    SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
    // Get the picture
    Picture picture = svg.getPicture();
    // Draw picture in canvas
    // Note: use transforms such as translate, scale and rotate to position the picture correctly
    canvas.drawPicture(picture);

You can get more details on how to use the library on the original documentation: http://code.google.com/p/svg-android/wiki/Tutorial

25 comments:

  1. Doesn't work.. gives error about widht and height (invalid float..)

    ReplyDelete
  2. The same for me: invalid float ...

    ReplyDelete
  3. With this lib, I have not the invalid float problem:

    https://github.com/applantation/svg-android

    ReplyDelete
  4. @Dareman, even that lib does have the float problem on this svg: http://www.clker.com/cliparts/C/G/j/0/u/S/animal-number-one.svg

    ReplyDelete
  5. Hey, Thanks for sharing :) just few questions,
    1- what about animation?
    2- how do you handle changing clothes directly?
    3- and what about text? does the library support text render and editing?

    I'm now working on creating an app that should render a business card template (which supposed to be in svg format) do you have thoughts?

    Thanks in advance.

    ReplyDelete
  6. Your blog is really useful for me. Thanks for sharing this useful blog..Suppose if anyone interested to learn Android Course in Chennai please visit fita academy which offers best Android Training Chennai at reasonable cost.

    ReplyDelete
  7. The prime advantage of using custom software program is that it helps users get optimum out of their expenses. Custom software are efficient enough and are extremely helpful in cost cutting. Increased productivity is another major advantage of using custom software in the business environment of a firm.winzip activation code

    ReplyDelete
  8. Android apps development company services in India, android software development company, Android app Development specially in the Android domain.
    http://www.showboxdownloadsapp.com/

    ReplyDelete
  9. Cars 3 will depict Lightning MacQueen getting into a depression because of all the new generation cars.. And the new antagonist Jackson Storm will be seen taking over Lightning. Cars 3 First Look

    ReplyDelete
  10. android apps training institute in noida - A common measure of the cost and time savings used by developers of Android applications is iOS application design and usability standards replicate the version of the Android platform application.
    free store apk

    ReplyDelete
  11. Nice post. I was checking constantly this blog and I’m impressed! Extremely useful info specially the last part I care for such information a lot. I was seeking this certain info for a long time. Thank you and good luck. blackmart alpha apk

    ReplyDelete
  12. With the launch of the Galaxy S7 and the S7 edge, people have already started to look forward to the next flagship launch from Samsung, the Galaxy S8. But if rumors are to be believed, work has already started for the next installment of the Galaxy S series, the Galaxy S9. If you want to know more about the device, then you must check out my site Samsung Galaxy S9

    ReplyDelete
  13. I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. UC News Download

    ReplyDelete
  14. Check out the specifications of the Galaxy S9

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. Thailand lottery best calendar sure number tips 01-06-2017

    ReplyDelete
  17. The developer's community is constantly working on different applications and feeds it with some of the latest technical and sophisticated advancements.Best android keyboard

    ReplyDelete
  18. The Android cell phone doorstep is on the arrive at an potent percentage of 32% each month, secrete existing handsets now released constantly. Game killer apk

    ReplyDelete
  19. T-mobile familywhere is awesome. Download it now. Visit this site for details.

    ReplyDelete
  20. AstrolApp will make it simpler to deal with the visitors by observing who has acknowledged or dismisses your occasion. Shadow Fight 3 Mod Apk

    ReplyDelete
  21. LG G7 Price, Full Specifications and Features.

    ReplyDelete
  22. It makes it very important to either be or to hire an Android development company that contains various skilled developers who can design innovative and extremely rich applications. teleprompter

    ReplyDelete