Android Programming Press on the image to return to the main documentation page.

SVG

Written by Andrew Graham

This library supports a subset of the SVG Basic 1.1 specification.
Typically, you can just load your vector artwork in a suitable program and then save it
as a SVG Basic 1.1 file and it will work fine.

By using vector art, the pain of supporting various screen sizes and densities in Android can be reduced.

These are the features of SVG Basic not supported:
Raster images (bitmaps).
Symbols, conditional processing.
Patterns.
Masks, filters and views.
Interactivity, linking, scripting and animation.

Bounds and Limits
It is useful when working with an SVG to know the rectangular bounds of the content.
The document (artboard) bounds themselves are typically not useful for this, as few
artists will size the content to those bounds. Instead, we offer two approaches:

Bounds
If there is a layer in the artwork called "bounds", it will not be drawn.
Rather, the first rectangle found in that layer will be interpreted as the bounds of the artwork.
This makes it easy for an artist to specify the right bounds right in the artwork without the
developer having to do additional work.

Limits
While the parser is reading the SVG, it estimates the bounds of the artwork from what is read.
This may not always be correct, but can often be relied upon for simple artwork.


Version 1.0 of this library was a very thin wrapper of the svg-android library which it required
as an external component to be referenced as a Lib by Basic4android.
svg-android

This version of the library is a very thin wrapper of the svg-android-2 library and the source code
for that is compiled into this library so no external component is needed.
This fork is used as the original author seems to have not reponded to bug fix requests so another
programmer has created this fork and applied some fixes and improvements.
Support transforms in g tags
Support transform lists (e.g. transform="rotate(..) scale(..) ...")
Support for arcs in paths
Support implied commands in paths
Support for named colors
Support for text (limited, but including font-size, type-face, and text alignment)
Support for stroke-dasharray for dotted/dashed lines
svg-android-2

The svg-android library is released for use under the Apache License Version 2.0.
Apache 2.0

Unfotunately the svg-android library is not always very informative about SVG files that it cannot
parse. Sometimes it will just throw a NullPointerException without any further information as to
where in the SVG file the problem lies. Other times it may throw an exception whose message
includes the line and column where the error is located in the SVG data file.

List of types:

SVG

SVG

This is a compact and straightforward object for parsing simple SVG files and rendering them
in an Android Canvas or as a PictureDrawable that may be assigned to the Background of a View.

Events:

None

Members:


  Bounds As RectWrapper [read only]

  CreateFromStream (istream As java.io.InputStream)

  CreatePictureDrawable As android.graphics.drawable.Drawable

  DrawPicture (canvas As CanvasWrapper, destrect As android.graphics.Rect)

  Height As Int [read only]

  Initialize (dir As String, filename As String)

  Initialize2 (inputstream As java.io.InputStream)

  Initialize3 (svgstring As String)

  IsInitialized As Boolean

  Limits As RectWrapper [read only]

  Picture As android.graphics.Picture [read only]

  Version As Double [read only]

  Width As Int [read only]

  WriteToStream (ostream As java.io.OutputStream)

Members description:

Bounds As RectWrapper [read only]
Gets the bounding rectangle for the SVG picture data, if one was specified.
Returns a Rect object whose parameters are all 0 if no bounding rectangle was specified.
CreateFromStream (istream As java.io.InputStream)
Create a new picture (already recorded) from the data in the stream.
This data was generated by a previous call to writeToStream().
CreatePictureDrawable As android.graphics.drawable.Drawable
Returns an Android PictureDrawable created from the the SVG picture data.
This may be assigned to the Background property of a View for display.
However DrawPicture may be more useful in practice.
DrawPicture (canvas As CanvasWrapper, destrect As android.graphics.Rect)
Draw this picture on the specified canvas to fit into the dest rectangle.
Height As Int [read only]
Return the height of the rendered SVG picture.
Initialize (dir As String, filename As String)
Initialize the SVG object with a picture obtained from a file name.
Initialize2 (inputstream As java.io.InputStream)
Initialize the SVG object with a picture read from an InputStream.
Initialize3 (svgstring As String)
Initialize the SVG object with a picture obtained read from an XML string.
IsInitialized As Boolean
Returns whether a picture exists within the SVG object or not
Limits As RectWrapper [read only]
Gets the bounding rectangle for the SVG that was computed upon parsing.
It may not be entirely accurate for certain curves or transformations
but is often better than nothing.
Returns a Rect object.
Picture As android.graphics.Picture [read only]
Returns an Android Picure parsed from the SVG picture data.
This is provided in case of need and is not directly usable in Basic4android.
Version As Double [read only]
Returns the version number of the library.
Width As Int [read only]
Return the width of the rendered SVG picture.
WriteToStream (ostream As java.io.OutputStream)
Write the picture contents to a stream.
The data can be used to recreate the picture by calling createFromStream.
Bounds and Limits are not set
Top