AndroidSVG
AndroidSVG is a SVG parser and renderer for Android. It has almost complete support for the static visual elements of the SVG 1.1 and SVG 1.2 Tiny specifications (except for filters). AndroidSVG correctly renders the SVG Acid Test.
AndroidSVG is licensed under the Apache License v2.0.
Where to get it
The current release is version 1.4.
See the 1.4 release notes for all the new features and bug fixes in this release.
To use AndroidSVG in your app
If you haven't already, remember to add Maven Central to the repository list in you main/root build.gradle
file.
repositories {
google()
jcenter()
mavenCentral()
}
Then in your app module build.gradle
file, add:
implementation 'com.caverock:androidsvg-aar:1.4'
Direct links to the v1.4 AAR and JAR files, and to older versions, are available on the Download page.
Sample App
For examples of how to use the library in a real world app, check out our AndroidSVG Sample App
Editor support
A design goal of this project is to correctly render SVG files that have been exported from the most popular vector editors. AndroidSVG has been tested with files generated by Adobe Illustrator, Sketch, Inkscape, Xara, Corel Draw, and many others.
API Introduction
All interaction with AndroidSVG is via the SVG
class.
Typically, you will call one of the SVG loading and parsing classes then call the renderer, passing it a canvas to draw upon.
- Use one of the static
SVG.getFromX()
methods to read and parse the SVG file. They will return an instance of theSVG
class. - Then to render, you can either call
renderToPicture()
to get an AndroidPicture
instance, or callrenderToCanvas()
to render directly to a Canvas.
For more information, see the Documentation or read the Javadoc.
Usage example
Download the latest version and copy it to your libs
folder
in Eclipse. Then in your app, you can do something like the following.
// Read an SVG from the assets folder SVG svg = SVG.getFromAsset(getContext().getAssets(), filename); // Create a canvas to draw onto if (svg.getDocumentWidth() != -1) { Bitmap newBM = Bitmap.createBitmap((int) Math.ceil(svg.getDocumentWidth()), (int) Math.ceil(svg.getDocumentHeight()), Bitmap.Config.ARGB_8888); Canvas bmcanvas = new Canvas(newBM); // Clear background to white bmcanvas.drawRGB(255, 255, 255); // Render our document onto our canvas svg.renderToCanvas(bmcanvas); }
If you just want to use an SVG icon in your layout, you can use the supplied SVGImageView custom view class.
What features of SVG are supported?
AndroidSVG supports the following SVG elements:
Fully supported
<circle>
<clipPath>
<defs>
<desc>
<ellipse>
<g>
<line>
<linearGradient>
<marker>
<mask>
<path>
<pattern>
<polygon>
<polyline>
<rect>
<solidColor>
<stop>
<svg>
<switch>
<symbol>
<title>
<use>
<view>
Supported with some limitations
<image>
- Loading of external SVG files is not supported.
<text>
- The multi-value forms of
x
,y
,dx
, anddy
are not supported. Some of the other text features, such asalignment-baseline
etc, are not supported. <textPath>
- Same restrictions as
<text>
<tref>
- Same restrictions as
<text>
. Also, externalhref
references are not supported. <tspan>
- Same restrictions as
<text>
<pattern>
- Patterned strokes are not currently supported.
<radialGradient>
- The
fx
andfy
attributes are not currently supported, due to a limitation of the AndroidCanvas
class. <style>
- We use a simplified CSS parser that supports most, but not all, of the latest CSS standard.
Not supported at all
- SMIL and CSS animation is not supported.
- Filter Effects via
<filter>
are not supported. <foreignObject>
is not supported.
For more information on what elements, attributes and properties are supported, see the SVG Implementation Details page.
Find a bug?
Please file an bug report and include as much detail as you can. If possible, please include a sample SVG file showing the error.
If you wish to contact the author with feedback on this project, you can email me at androidsvgfeedback@gmail.com.
If you have found AndroidSVG useful and use it in your project, please let me know. I'd love to hear about it!