Use SVG on Android

I am working as an android mobile application developer from last three years. My boss asked me to use SVG instead of PNG. So last few days I have done RND about SVG and how we can use it on android native development. I am going to share some information about SVG. Folks if you have any idea about SVG and how to use it on android development please share with us. Hope below information will help to all.

What is SVG?

SVG stands for Scalable Vector Graphics

SVG is an XML-based vector image format for two-dimensional graphics. Normally SVG graphics do NOT lose any quality if they are zoomed or resized.

 

SVG Advantages

  • SVG images can be created and edited with any text editor
  • SVG images can be searched, indexed, scripted, and compressed
  • SVG images are scalable
  • SVG images can be printed with high quality at any resolution
  • SVG images are zoom able (and the image can be zoomed without degradation)
  • SVG is an open standard
  • SVG files are pure XML

 

The Android 2.x default browser does not natively support SVG.

The Android 3+ default browsers DO support SVG.

 

SGV images are a good replacement for bitmaps in many cases. Although they are not supported natively by Android, they can be easily displayed by means of third party libraries. They take up much less space (especially the zipped version – svgz), scale indefinitely without losing any detail and don’t require a separate version of an image for each screen density.

Now we will see how we can use SVG as android drawable images.

Step 1:

create an android project

step 2:

copy and paste the below code into the activity

 

import com.caverock.androidsvg.SVG;

import com.caverock.androidsvg.SVGParseException;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
try
{
SVG svg = SVG.getFromResource(this, R.raw.album);
Drawable drawable = new PictureDrawable(svg.renderToPicture());
imageView.setImageDrawable(drawable);
}
catch(SVGParseException e)
{}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

 

step 3:

copy the androidsvg-1.2.1.jar into libs folder

you can download the androidsvg-1.2.1.jar file from here

Hope it will help to implement SVG on android. Guys please share knowledge and comments here.

4 Comments

  1. Chris June 25, 2014
  2. Allen January 16, 2016
  3. Daniel May 17, 2016
  4. David June 10, 2017

Add Comment