Load vector drawables from an online location android

南楼画角 提交于 2020-12-01 12:08:15

问题


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


回答1:


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!



来源:https://stackoverflow.com/questions/39663106/load-vector-drawables-from-an-online-location-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!