How can I load vector drawables from an online location e.g http://arsenal.com/image.xml and display it as an image
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With