Skip to content

Description

Every time you need to paint some icons, backgrounds, charts, document pages, diagrams in vector format, you need a way to do this easily and possibly without NDK, cross-compilation issues, reducing performance, etc. The amazing library developed by our company just does it: no extra code, no extra libraries, no tuning.

svg-android

Why SVG Kit for Android

We have evaluated a lot of libraries, many of which were either too slow but over-featured (like the legendary Apache Batik) or too complicated in maintenance (like NDK, librsvg, and issues with the proper compilation for all supported platforms and architectures). We just wanted to show the vector background.

So, we have decided to make our own library. And even more, we have added a few abilities that are not supported by “huge” libraries. Even, radial gradients are supported in our library.

We tried to make it as small as possible; no advanced features from SVG 1.2 Full specs are supported. We suppose we don’t need animations, compositions, JavaScript or user input, we want to use just vector background or icon.

Approach2

Main Features

To summarize, now we have:

  • Pretty small.
  • Pure Java.
  • Reliable and fast.
  • CSS support.
  • Gradients (including radial), transformations and paints.
  • Fonts, texts, and text spans.
  • Drawing primitives, paths and polygons.
  • Strokes and fills, including alpha, dashes, joins and others.
  • XLink support included.
  • Raster images in SVG support (external, from assets or embedded).
  • SVG filters initial support.
  • SVG and SVGZ support.

Moreover, we have tested huge SVG files with dozens of megabytes each. Depending on the SVG files and transport (local or from the Internet) it might be “pretty fast” or “acceptable”, but it really works in all cases.

The following features are not supported:

Declarative animation for SVG, interactive and dynamic SVG documents, JavaScript, raster images in strange formats, HTML tags around.

It is just a small, fast and easy-to-use library that was designed to support “pure vector” icons, backgrounds and etc. No overloading on this stage. In case anybody requests it – our team will expand the library according to their needs. It’s not a big problem for our team.

Tutorial

Getting started

The core of the library is “libsvg.aar”. It does everything from fast SAX-based parsing, rendering .svg files for Android at the bitmaps and surfaces, computers corresponding CSS styles defined in .svg and more.
It provides an API (primarily SVGHelper class) to operate with files in a typical stream-like manner.

Libsvg.aar supports many graphical features defined in SVG 1.1 and SVG 1.2. But it is intended to render only static images without animation, filters and custom SVG fonts.

The feature list includes:

  • Drawing shapes (ellipse, circle, polyline, polygon, rect, line, etc.),
  • Drawing gradients (except focus points in radial gradients),
  • Drawing path elements but without markers,
  • Drawing text elements, currently working only with standard android fonts,
  • Supported such elements as: <g>, <defs>, <use>.

The list of implemented features is not so big, but it is more than enough to paint most of the typical SVG files.

SVGHelper: parse and get suitable result

To use this helper, just follow through the list below in the top-down order.

1. Add “libsvg.aar” in your project.
2. Choose the SVG source:

  • From file:
SVGHelper.noContext().open(new File(...));
  • From resources (works only with context, useContext(…) is necessary):
SVGHelper.useContext(context).open(R.raw.svg_file);
  • From web:
  • SVGHelper.noContext().open(new URL(...));
  • Set SVG code directly:
  • SVGHelper.noContext().open(“<svg> any svg source code </svg>”);

3. Determine SVG document size (if necessary). This option is required if original SVG image has no width/height and viewport attributes.

  • Define sizes:
SVGHelper.noContext().open(...).setBaseBounds(300, 400);
  • Or let library compute itself (if SVG has width/height definition),
SVGHelper.noContext().open(...).checkSVGSize();

Note: might add extra performance penalty.

4. Choosing svg transform (if necessary).

  • Set scale directly:
SVGHelper.noContext().open(...).setScale(1.7f);
  • Or set result bounds:
SVGHelper.noContext().open(...).setRequestBounds(1024, 768);

5. Additional SVG transform options.

  • Set keep aspect ratio option (by default true):
SVGHelper.noContext().open(...).setKeepAspectRatio(false); 
  • Set crop image option (by default false):
SVGHelper.noContext().open(...).setCropImage(true);

6. Choose result type.

  • Bitmap:
SVGHelper.noContext().open(...).getBitmap();
  • BitmapDrawable:
SVGHelper.useContext(context).open(...).getBitmapDrawable(); 
  • Bitmap as background of any View:
SVGHelper.useContext(context).open(...).bitmapAsBackground(view);
  • Picture:
SVGHelper.noContext().open(...).getPicture();
  • PictureDrawable:
SVGHelper.noContext().open(...).getPictureDrawable();
  • Picture as stretch background of any View:
SVGHelper.noContext().open(...).pictureAsBackground(view);

A few examples:

  • SVG from resource as button background:
SVGHelper.useContext(context).open(R.raw.svg).bitmapAsBackground(button);
  • Scaled SVG file as Bitmap:
SVGHelper.noContext().open(file).setScale(2f).getBitmap();
  • SVG from URL, determine its size and get it as 100×100 Bitmap:
SVGHelper.noContext().open(url).checkSVGSize().setRequestBounds(100,100).getBitmap();

Select Your License

Below, you can choose any of the available billing plans according to your needs from a free GPL license to an unlimited commercial license.
Can't find what you need? View our custom software development services.

Need a Similar Solution?

Related Products

TouchZ

Open source library which helps iOS developers and testers to get better insight of what's the real nature of the bug the testers bump into.

Learn more

epuBear

Lightweight and easily customizable cross-platform EPUB SDK for EPUB readers development.

Learn more

Realm Browser for Android

Realm Browser Library is a small, but very helpful library designed for viewing and editing Realm database files on Android devices.

Learn more

This site uses technical cookies and allows the sending of 'third-party' cookies. By continuing to browse, you accept the use of cookies. For more information, see our Privacy Policy.