从在线位置android加载矢量可绘制对象 [英] Load vector drawables from an online location android

查看:82
本文介绍了从在线位置android加载矢量可绘制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从在线位置加载矢量可绘制对象,例如 http://arsenal.com/image.xml并将其显示为图片

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

推荐答案

您是否需要在较旧的设备上支持VectorDrawables?如果是这样,那么我认为目前不可能.AFAIK支持库仅允许您分配VectorDrawable(如果它是一种资源)(即通过 setImageResource()).

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

替代方法是改为使用SVG,并使用Android的SVG库之一.

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

但是,如果您只需要支持Lollipop及更高版本,则可以使用下面列出的过程进行操作.尽管我自己还没有尝试过.

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.

首先,以流的形式获取VectorDrawable文件.例如,请参见此问题.

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

然后,您需要创建一个 XmlPullParser 实例

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);

然后,您应该可以将可绘制对象分配给ImageView.

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

imageView.setImageDrawable(vd);

祝你好运!

这篇关于从在线位置android加载矢量可绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆