Click here to Skip to main content
Click here to Skip to main content

Tagged as

Android ImageView and Drawable with SVG Support

, 14 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This article introduces custom classes SvgImageView and SvgDrawable which allow to use SVG image just like any other image.


This article shows how to use SVG images in Android. The goal is to use SVG images just like any other images, using Drawable and ImageView classes. New classes are introduced for this purpose: SvgDrawable and SvgImageView.

These classes support all Android scale types for images: fitXY, fitStart, fitEnd, centerCrop and so on. What is important, the image is stretched in vector format, before rasterization. I believe this improves overall performance.


You are encouraged to read the article by Igor Kushnarev beforehand. Carl D. Worth, Anton Persson and Igor Kushnarev have done a great job. This article is a continuation of their work.

I needed to change their implementation because of the following reasons:

  • Actually a lot (if not all) SVG images have the desired width and height. They are written in the file. And I want to use them too. The original implementation did not care about SVG width and height.
  • I need a Drawable class to work with image, not just an ImageView. Because Drawable can be used in many other Android classes.
  • I want to use advantage of scale types for images: fitXY, fitStart, fitEnd, centerCrop and so on.
  • FitXY scale type requires non-uniform scaling of image. This is required but was not supported by the original native code.
  • I do not want standard ImageView class to scale rasterized bitmap in case of SVG Drawable. I want that vector image would be scaled according to scale type before rasterization. This will improve performance and quality of image.


Well, if you have read the article by Igor Kushnarev, you should already have this:

  • Eclipse with Java, Android and CDT support
  • Android SDK
  • Android NDK
  • Sequoyah Android Native Support in Eclipse

Points of Interest

Width and height of SVG image

Please look at /jni/libsvg-android/svg_android.c.

I've added two functions that return width and height of SVG image to Java class SvgRaster:

  • Java_com_libsvg_SvgRaster_svgAndroidGetWidth(),
  • Java_com_libsvg_SvgRaster_svgAndroidGetHeight().

They use values from DOM model, which were set on parsing the XML structure.

Scaling Support

I've been selecting from two basic approaches to implement Android scale types support:

  1. Rewrite the whole ImageView class. Because it was written specially for raster bitmaps, it takes the whole work to make matrix transformation, not letting the Drawable itself to take care about scaling.
  2. Cheat on ImageView class. Make it believe that Drawable has a good size and should not be scaled using Matrix transform. Let the SVG image make scaling in vector, before rasterization.

I've chosen the second approach because it is easier.

Before ImageView class prepares matrix transform, we let SvgDrawable to adjust its width and height to match those from ImageView with respect to scaleType.

For this purpose, we rewrite setScaleType(), setImageDrawable() and onSizeChanged().

public void setScaleType (ImageView.ScaleType scaleType) {
    if (! mIsSvg) {

    SvgDrawable svg = (SvgDrawable) getDrawable(); 
    // Let SVG scale itself!
    int vWidth = getWidth() - getPaddingLeft() - getPaddingRight();
    int vHeight = getHeight() - getPaddingTop() - getPaddingBottom();
    svg.adjustToParentSize(vWidth, vHeight);

Notice that we first call super.setImageDrawable(null). Then we change SVG image scaling. And then return it with super.setImageDrawable(svg). This trick works!

SvgDrawable Class

This is a rather simple implementation based on ideas of Igor Kushnarev.

Scaling support required new function:

public void adjustToParentSize(int vWidth, int vHeight) 

It is called from SvgImageView when onSizeChanged(), setScaleType() and on load.

Please also notice that we choose how to render svg image in onBoundsChange():

  • using non-uniform scaling - SvgRaster.svgAndroidRenderToArea()
  • using uniform scaling - SvgRaster.svgAndroidRenderToAreaUniform

Non-uniform Scaling Support in libsvgandroid

Please look at /jni/libsvg-android/svg_android_render.c.

I've changed function:

svg_status_t svgAndroidRenderToArea(JNIEnv *env, 
svg_android_t *svg_android, jobject android_canvas, int x, int y, int w, int h) { 
    svg_android->fit_to_area = -1; 
    svg_android->fit_uniform = 0; 
    return svg_render (svg_android->svg, &SVG_ANDROID_RENDER_ENGINE, svg_android);

svg_android->fit_uniform is a newly added member, which is used in: _svg_android_set_viewport_dimension().

In svgAndroidRenderToArea(), it is set to zero (false).

In svgAndroidRenderToAreaUniform(), it is set to -1 (true).

Using SvgImageView

Look at /res/layout/main2.xml.

Part of this file:

<TableLayout xmlns:android=""

Notice the tags <com.libsvg.SvgImageView>. They have all the attributes of standard ImageView.


Upon successful compile and run, you should see the following picture of smiles in a table.
Table has two rows with 3 images on each row.


It demonstrates different scale types for SVG images.

Currently, from left to right, from top to bottom:
fitXY, fitStart, fitEnd,
centerCrop, center, centerInside


Hope this article will help to improve Android development. If you have something to add - please write here and share your code!

Sincerely yours,
Pavel B. Chernov


  • 14th December, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


Russian Federation Russian Federation
No Biography provided

Comments and Discussions

QuestionSvg Rendering problem PinmemberMember 961927128-Feb-13 2:35 
Questioncalling the libsvg from a separate project PinmemberMember 961927118-Feb-13 3:58 
QuestionVM Aborting error Pinmemberewdfewfew2328-Feb-12 9:57 
Questionhow to scroll the image PinmemberMember 198809227-Feb-12 23:16 
AnswerRe: how to scroll the image PinmemberManfeel18-Sep-12 15:27 
QuestionThanks for your work + Drawable question ! PinmemberErnesto S31-Dec-11 8:01 
QuestionThank you Pinmembermcflysoft27-Aug-11 1:51 
Generallayout problem Pinmemberpeter5031826-Mar-11 4:30 
QuestionSVG that doesn't work Pinmemberfeeder18034-Feb-11 9:01 
AnswerRe: SVG that doesn't work PinmemberAlejandro Xalabarder4-Feb-11 16:23 
GeneralRe: SVG that doesn't work Pinmemberfeeder18035-Feb-11 1:49 
GeneralRe: SVG that doesn't work Pinmemberdiov5-Feb-11 2:21 
GeneralRe: SVG that doesn't work Pinmemberdiov5-Feb-11 8:21 
This is what i've discovered.
Here is the link to Android Dalvik JNI source code:[^]
Please notice the function
jint PushLocalFrame(JNIEnv* env, jint capacity)
which calls:
bool ensureLocalCapacity(int capacity)
which uses: kJniLocalRefMax constant.
According to Google Groups thread[^]
there is a limit of 512 local references (i.e. Java objects), that could be created during one JNI native call.
You may ask: What does it mean, why should we care about java objects?
Well, as far as I understand Anton Perrson's code, it uses Android Java classes:, and a lot of others - to actually render vector image.
So when we call svglib to render itself, it issues a lot of Java calls to actually draw image using java classes.
The more paths in SVG you have - the more calls will be made to create objects and to draw them!
So during one JNI call to SvgRenderToArea we are not allowed to create more than 512 objects.
In your case this is not enought!
I've found no way to increase the limit of 512 local references!
I think, You will fail to load such a big complicated file in svglib.
I suggest You to try agglib - it does not use Java classes, but it does rendering in C-code.
Good luck!
GeneralRe: SVG that doesn't work PinmemberAlejandro Xalabarder6-Feb-11 11:16 
GeneralRe: SVG that doesn't work Pinmemberfeeder18036-Feb-11 22:01 
GeneralRe: SVG that doesn't work Pinmemberdiov7-Feb-11 1:49 
GeneralRe: SVG that doesn't work PinmemberAlejandro Xalabarder7-Feb-11 14:14 
GeneralRe: SVG that doesn't work PinmemberAnton Persson25-Feb-11 11:09 
GeneralRe: SVG that doesn't work Pinmemberdiov25-Feb-11 15:15 
GeneralLowest Android Version Support? MVO5 PinmemberNic_Roche17-Dec-10 12:28 
GeneralRe: Lowest Android Version Support? MVO5 Pinmemberdiov17-Dec-10 12:55 
GeneralRe: Lowest Android Version Support? MVO5 PinmemberNic_Roche30-Dec-10 16:16 
GeneralThank you guys! Pinmemberdiov17-Dec-10 9:39 
GeneralMy vote of 5 PinmemberRaviRanjankr17-Dec-10 4:20 
GeneralMy vote of 5 Pinmembersam.hill14-Dec-10 6:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 14 Dec 2010
Article Copyright 2010 by diov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid