Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load vector drawables from an online location android

How can I load vector drawables from an online location e.g http://arsenal.com/image.xml and display it as an image

like image 553
Idee Avatar asked Sep 23 '16 14:09

Idee


1 Answers

Do you need to support VectorDrawables on older devices? If so, then I don't think it is currently possible. AFAIK the support library will only let you assign a VectorDrawable if it is a resource (ie. via setImageResource()).

http://android-developers.blogspot.co.nz/2016/02/android-support-library-232.html

The alternative would be to use SVGs instead and use one of the SVG libraries for Android.

However, if you only need to support Lollipop and later, then it should be possible using the process as set out below. Though I haven't tried it myself.

First, fetch the VectorDrawable file as a Stream. As an example, see this question.

You will then need to make an XmlPullParser instance.

xppXmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();

Then tell the parser about the stream

xpp.setInput(input, null);

Then you can get a VectorDrawable by calling inflate(). Pass it the parser instance.

VectorDrawable  vd = new VectorDrawable();
vd.inflate(getResources(), xpp, null, null);

Then you should be able to assign the drawable to your ImageView.

imageView.setImageDrawable(vd);

Good luck!

like image 93
Paul LeBeau Avatar answered Sep 19 '22 20:09

Paul LeBeau