public class SVG extends Object
All interaction with AndroidSVG is via this class.
Typically, you will call one of the SVG loading and parsing classes then call the renderer, passing it a canvas to draw upon.
getFromX() methods to read and parse the SVG file. They will
return an instance of this class.
renderToX() methods to render the document.
SVG.registerExternalFileResolver(myResolver);
SVG svg = SVG.getFromAsset(getContext().getAssets(), svgPath);
Bitmap newBM = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas bmcanvas = new Canvas(newBM);
bmcanvas.drawRGB(255, 255, 255); // Clear background to white
svg.renderToCanvas(bmcanvas);
For more detailed information on how to use this library, see the documentation at http://code.google.com/p/androidsvg/| Modifier and Type | Method and Description |
|---|---|
static void |
deregisterExternalFileResolver()
De-register the current
SVGExternalFileResolver instance. |
float |
getDocumentAspectRatio()
Returns the aspect ratio of the document as a width/height fraction.
|
String |
getDocumentDescription()
Returns the contents of the
<desc> element in the SVG document. |
float |
getDocumentHeight()
Returns the height of the document as specified in the SVG file.
|
PreserveAspectRatio |
getDocumentPreserveAspectRatio()
Return the "preserveAspectRatio" attribute of the root
<svg>
element in the form of an PreserveAspectRatio object. |
String |
getDocumentSVGVersion()
Returns the SVG version number as provided in the root
<svg> tag of the document. |
String |
getDocumentTitle()
Returns the contents of the
<title> element in the SVG document. |
RectF |
getDocumentViewBox()
Returns the viewBox attribute of the current SVG document.
|
float |
getDocumentWidth()
Returns the width of the document as specified in the SVG file.
|
static SVG |
getFromAsset(AssetManager assetManager,
String filename)
Read and parse an SVG from the assets folder.
|
static SVG |
getFromInputStream(InputStream is)
Read and parse an SVG from the given
InputStream. |
static SVG |
getFromResource(Context context,
int resourceId)
Read and parse an SVG from the given resource location.
|
static SVG |
getFromResource(Resources resources,
int resourceId)
Read and parse an SVG from the given resource location.
|
static SVG |
getFromString(String svg)
Read and parse an SVG from the given
String. |
float |
getRenderDPI()
Get the current render DPI setting.
|
static String |
getVersion()
Returns the version number of this library.
|
Set<String> |
getViewList()
Returns a list of ids for all
<view> elements in this SVG document. |
static boolean |
isInternalEntitiesEnabled() |
static void |
registerExternalFileResolver(SVGExternalFileResolver fileResolver)
Register an
SVGExternalFileResolver instance that the renderer should use when resolving
external references such as images, fonts, and CSS stylesheets. |
void |
renderToCanvas(Canvas canvas)
Renders this SVG document to a Canvas object.
|
void |
renderToCanvas(Canvas canvas,
RectF viewPort)
Renders this SVG document to a Canvas object.
|
void |
renderToCanvas(Canvas canvas,
RenderOptions renderOptions)
Renders this SVG document to a Canvas object.
|
Picture |
renderToPicture()
Renders this SVG document to a Picture object.
|
Picture |
renderToPicture(int widthInPixels,
int heightInPixels)
Renders this SVG document to a
Picture. |
Picture |
renderToPicture(int widthInPixels,
int heightInPixels,
RenderOptions renderOptions)
Renders this SVG document to a
Picture. |
Picture |
renderToPicture(RenderOptions renderOptions)
Renders this SVG document to a
Picture. |
void |
renderViewToCanvas(String viewId,
Canvas canvas)
Renders this SVG document to a Canvas using the specified view defined in the document.
|
void |
renderViewToCanvas(String viewId,
Canvas canvas,
RectF viewPort)
Renders this SVG document to a Canvas using the specified view defined in the document.
|
Picture |
renderViewToPicture(String viewId,
int widthInPixels,
int heightInPixels)
Renders this SVG document to a
Picture using the specified view defined in the document. |
void |
setDocumentHeight(float pixels)
Change the height of the document by altering the "height" attribute
of the root
<svg> element. |
void |
setDocumentHeight(String value)
Change the height of the document by altering the "height" attribute
of the root
<svg> element. |
void |
setDocumentPreserveAspectRatio(PreserveAspectRatio preserveAspectRatio)
Change the document positioning by altering the "preserveAspectRatio"
attribute of the root
<svg> element. |
void |
setDocumentViewBox(float minX,
float minY,
float width,
float height)
Change the document view box by altering the "viewBox" attribute
of the root
<svg> element. |
void |
setDocumentWidth(float pixels)
Change the width of the document by altering the "width" attribute
of the root
<svg> element. |
void |
setDocumentWidth(String value)
Change the width of the document by altering the "width" attribute
of the root
<svg> element. |
static void |
setInternalEntitiesEnabled(boolean enable)
Tells the parser whether to allow the expansion of internal entities.
|
void |
setRenderDPI(float dpi)
Set the DPI (dots-per-inch) value to use when rendering.
|
public static SVG getFromInputStream(InputStream is) throws SVGParseException
InputStream.is - the input stream from which to read the file.SVGParseException - if there is an error parsing the document.public static SVG getFromString(String svg) throws SVGParseException
String.svg - the String instance containing the SVG document.SVGParseException - if there is an error parsing the document.public static SVG getFromResource(Context context, int resourceId) throws SVGParseException
context - the Android context of the resource.resourceId - the resource identifier of the SVG document.SVGParseException - if there is an error parsing the document.public static SVG getFromResource(Resources resources, int resourceId) throws SVGParseException
resources - the set of Resources in which to locate the file.resourceId - the resource identifier of the SVG document.SVGParseException - if there is an error parsing the document.public static SVG getFromAsset(AssetManager assetManager, String filename) throws SVGParseException, IOException
assetManager - the AssetManager instance to use when reading the file.filename - the filename of the SVG document within assets.SVGParseException - if there is an error parsing the document.IOException - if there is some IO error while reading the file.public static void setInternalEntitiesEnabled(boolean enable)
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [
<!ENTITY hello "Hello World!">
]>
<svg>
<text>&hello;</text>
</svg>
Entities are useful in some circumstances, but SVG files that use them are quite rare. Note
also that enabling entity expansion makes you vulnerable to the
Billion Laughs Attack
Entity expansion is enabled by default.enable - Set true if you want to enable entity expansion by the parser.public static boolean isInternalEntitiesEnabled()
public static void registerExternalFileResolver(SVGExternalFileResolver fileResolver)
SVGExternalFileResolver instance that the renderer should use when resolving
external references such as images, fonts, and CSS stylesheets.
Note: prior to release 1.3, this was an instance method of (@code SVG}. In 1.3, it was changed to a static method so that users can resolve external references to CSSS files while the SVG is being parsed.
fileResolver - the resolver to use.public static void deregisterExternalFileResolver()
SVGExternalFileResolver instance.public void setRenderDPI(float dpi)
You should not normally need to alter the DPI from the default of 96 as recommended by the SVG and CSS specifications.
dpi - the DPI value that the renderer should use.public float getRenderDPI()
public Picture renderToPicture()
An attempt will be made to determine a suitable initial viewport from the contents of the SVG file. If an appropriate viewport can't be determined, a default viewport of 512x512 will be used.
Canvas.drawPicture()public Picture renderToPicture(int widthInPixels, int heightInPixels)
Picture.widthInPixels - the width of the initial viewportheightInPixels - the height of the initial viewportCanvas.drawPicture(Picture)public Picture renderToPicture(RenderOptions renderOptions)
Picture.renderOptions - options that describe how to render this SVG on the Canvas.Canvas.drawPicture(Picture)public Picture renderToPicture(int widthInPixels, int heightInPixels, RenderOptions renderOptions)
Picture.widthInPixels - the width of the PictureheightInPixels - the height of the PicturerenderOptions - options that describe how to render this SVG on the Canvas.Canvas.drawPicture(Picture)public Picture renderViewToPicture(String viewId, int widthInPixels, int heightInPixels)
Picture using the specified view defined in the document.
A View is an special element in a SVG document that describes a rectangular area in the document.
Calling this method with a viewId will result in the specified view being positioned and scaled
to the viewport. In other words, use renderToPicture() to render the whole document, or use this
method instead to render just a part of it.
viewId - the id of a view element in the document that defines which section of the document is to be visible.widthInPixels - the width of the initial viewportheightInPixels - the height of the initial viewportCanvas.drawPicture(), or null if the viewId was not found.public void renderToCanvas(Canvas canvas)
canvas - the canvas to which the document should be rendered.public void renderToCanvas(Canvas canvas, RectF viewPort)
canvas - the canvas to which the document should be rendered.viewPort - the bounds of the area on the canvas you want the SVG rendered, or null for the whole canvas.public void renderToCanvas(Canvas canvas, RenderOptions renderOptions)
canvas - the canvas to which the document should be rendered.renderOptions - options that describe how to render this SVG on the Canvas.public void renderViewToCanvas(String viewId, Canvas canvas)
A View is an special element in a SVG documents that describes a rectangular area in the document.
Calling this method with a viewId will result in the specified view being positioned and scaled
to the viewport. In other words, use renderToPicture() to render the whole document, or use this
method instead to render just a part of it.
If the <view> could not be found, nothing will be drawn.
viewId - the id of a view element in the document that defines which section of the document is to be visible.canvas - the canvas to which the document should be rendered.public void renderViewToCanvas(String viewId, Canvas canvas, RectF viewPort)
A View is an special element in a SVG documents that describes a rectangular area in the document.
Calling this method with a viewId will result in the specified view being positioned and scaled
to the viewport. In other words, use renderToPicture() to render the whole document, or use this
method instead to render just a part of it.
If the <view> could not be found, nothing will be drawn.
viewId - the id of a view element in the document that defines which section of the document is to be visible.canvas - the canvas to which the document should be rendered.viewPort - the bounds of the area on the canvas you want the SVG rendered, or null for the whole canvas.public static String getVersion()
public String getDocumentTitle()
<title> element in the SVG document.IllegalArgumentException - if there is no current SVG document loaded.public String getDocumentDescription()
<desc> element in the SVG document.IllegalArgumentException - if there is no current SVG document loaded.public String getDocumentSVGVersion()
<svg> tag of the document.IllegalArgumentException - if there is no current SVG document loaded.public Set<String> getViewList()
<view> elements in this SVG document.
The returned view ids could be used when calling and of the renderViewToX() methods.
IllegalArgumentException - if there is no current SVG document loaded.public float getDocumentWidth()
If the width in the document is specified in pixels, that value will be returned.
If the value is listed with a physical unit such as "cm", then the current
RenderDPI value will be used to convert that value to pixels. If the width
is missing, or in a form which can't be converted to pixels, such as "100%" for
example, -1 will be returned.
IllegalArgumentException - if there is no current SVG document loaded.public void setDocumentWidth(float pixels)
<svg> element.pixels - The new value of width in pixels.IllegalArgumentException - if there is no current SVG document loaded.public void setDocumentWidth(String value) throws SVGParseException
<svg> element.value - A valid SVG 'length' attribute, such as "100px" or "10cm".SVGParseException - if value cannot be parsed successfully.IllegalArgumentException - if there is no current SVG document loaded.public float getDocumentHeight()
If the height in the document is specified in pixels, that value will be returned.
If the value is listed with a physical unit such as "cm", then the current
RenderDPI value will be used to convert that value to pixels. If the height
is missing, or in a form which can't be converted to pixels, such as "100%" for
example, -1 will be returned.
IllegalArgumentException - if there is no current SVG document loaded.public void setDocumentHeight(float pixels)
<svg> element.pixels - The new value of height in pixels.IllegalArgumentException - if there is no current SVG document loaded.public void setDocumentHeight(String value) throws SVGParseException
<svg> element.value - A valid SVG 'length' attribute, such as "100px" or "10cm".SVGParseException - if value cannot be parsed successfully.IllegalArgumentException - if there is no current SVG document loaded.public void setDocumentViewBox(float minX,
float minY,
float width,
float height)
<svg> element.
The viewBox generally describes the bounding box dimensions of the document contents. A valid viewBox is necessary if you want the document scaled to fit the canvas or viewport the document is to be rendered into.
By setting a viewBox that describes only a portion of the document, you can reproduce the effect of image sprites.
minX - the left coordinate of the viewBox in pixelsminY - the top coordinate of the viewBox in pixels.width - the width of the viewBox in pixelsheight - the height of the viewBox in pixelsIllegalArgumentException - if there is no current SVG document loaded.public RectF getDocumentViewBox()
android.graphics.RectF object, or null if not set.IllegalArgumentException - if there is no current SVG document loaded.public void setDocumentPreserveAspectRatio(PreserveAspectRatio preserveAspectRatio)
<svg> element. See the
documentation for PreserveAspectRatio for more information
on how positioning works.preserveAspectRatio - the new preserveAspectRatio setting for the root <svg> element.IllegalArgumentException - if there is no current SVG document loaded.public PreserveAspectRatio getDocumentPreserveAspectRatio()
<svg>
element in the form of an PreserveAspectRatio object.<svg> element.IllegalArgumentException - if there is no current SVG document loaded.public float getDocumentAspectRatio()
If the width or height of the document are listed with a physical unit such as "cm",
then the current renderDPI setting will be used to convert that value to pixels.
If the width or height cannot be determined, -1 will be returned.
IllegalArgumentException - if there is no current SVG document loaded.