2016-09-23 68 views

回答

1

你需要支持旧设备VectorDrawables?如果是这样,那么我认为目前是不可能的。 AFAIK如果支持库是一个资源(即通过setImageResource()),它将只允许您分配VectorDrawable。

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

另一种方法是使用SVGs代替并使用Android版SVG库之一。

但是,如果您只需要支持棒棒糖和更高版本,那么应该可以使用下面列出的过程。虽然我自己没有尝试过。

首先,将VectorDrawable文件作为Stream获取。作为例子,参见this question

然后您需要制作一个XmlPullParser实例。

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

然后告诉解析器有关流

xpp.setInput(input, null); 

然后你就可以通过调用inflate()得到VectorDrawable。将它传递给解析器实例。

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

然后,您应该能够将drawable分配给ImageView。

imageView.setImageDrawable(vd); 

祝你好运!