SVG Parser for Xamarin.Android
C# Python Makefile
Latest commit 319d3aa Nov 26, 2014 @garuma Merge pull request #3 from Cheesebaron/patch-1
Guard against edge cases caused by layout inflation
Failed to load latest commit information.
Filters Initial commit Oct 7, 2013
Internal Use culture-safe float parsing Feb 27, 2014
Resources Initial commit Oct 7, 2013
XamSvgTests Add visual test using monkeyrunner Oct 12, 2013
.gitignore Initial commit Oct 7, 2013
ISvgColorMapper.cs Initial commit Oct 7, 2013
Makefile Improve test makefile Feb 27, 2014
PictureBitmapDrawable.cs Fixes #2 Sep 19, 2014 Update Dec 20, 2013
Svg.cs Initial commit Oct 7, 2013
SvgFactory.cs Initial commit Oct 7, 2013
SvgParser.cs Initial commit Oct 7, 2013
XamSvg.csproj Use culture-safe float parsing Feb 27, 2014
XamSvg.sln Add visual test using monkeyrunner Oct 12, 2013 Add visual test using monkeyrunner Oct 12, 2013


XamSvg is a library to parse and render SVG images with Xamarin.Android

It's mainly a rough port of AndEngine SVG plugin with added sugar for normal Android development.



Preparing your SVG

First and foremost, this library is not meant to be a full blown SVG parser. Rather the idea is to support a small subset of the format to allow scalable shapes to be used on Android. This means that path, shapes, color and gradients are supported but basically nothing else (e.g. pretty much no SVG effects).

It's also possible that some higher-level SVG constructs used by graphic softwares don't meddle well with the parser. In that case, you should try to export the image in a "lower-compatibility" or "simplified" mode. The excellent (and free) Inkscape SVG editor for instance supports saving pictures in a "Optimized SVG" format that is more suitable to be consummed by the library.


The most straightforward way to use the library is through SvgFactory.

// Get a bitmap from resources (also raw SVG string, TextReader and Stream)
var bmp = SvgFactory.GetBitmap (Activity.Resources, Resource.Raw.ic_svg, 48, 48);

// Get a drawable that will automatically scale with the drawer bounds
var drawable = SvgFactory.GetDrawable (Activity.Resources, Resource.Raw.ic_svg);

If you are using the Drawable factory method, know that you should specify some bounds first. If you are using an ImageView, this can be simply done by specifying it like this in XML:

    android:scaleType="fitXY" />

Note that we also use the fitXY value for scale type to avoid any canvas transformation.

In that case, the drawable will automatically generate a bitmap representation of your SVG at the right size and density.

Color mapper

Every factory method supports an optional color mapper parameter that let you dynamically change the color of the SVG being parsed. This is useful if you want to reuse the same SVG multiple time with a different tint color.

You can create a mapper by implementing the ISvgColorMapper interface or directly create one with a Func<Color, Color> using the SvgColorMapperFactory.FromFunc method.

Low-level, directly using SvgParser

If you want to have directly access to the parsed SVG representation, you can use the methods of the SvgParser class which return a Svg instance.

That instance has a Picture property returning (surprise) a Picture object that contains all the drawing calls necessary to render the SVG.

Bigger example

Moyeu uses this library for pretty much all of its icon. Since it's also available under an open-source license, it's a good way to see how the library can be used.


This work is placed under the Apache 2.0 license.