关于Vector及AnimatedVectorDrawable的一些基本知识,请参见徐医生的Android Vector曲折的兼容之路 ,写的很全,很棒。
读完上文之后,我们对静态的vector和动态的AnimatedVectorDrawable有了一定的了解,并做了一些小的尝试,本文里边主要说的是一些兼容性的问题及解决方案。
从AppCompat23.2开始,Google开始支持在低版本上使用Vector,兼容性还是相当不错的,但是动态的AnimatedVectorDrawable在低版本的兼容上边问题就很大了,除了目前不能在L之下的版本上使用以外,还有一些其他的坑。
AnimatedVectorDrawableCompat animatedVectorDrawable = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.vectalign_animated_vector_drawable_start_to_end);
ImageView imageView = (ImageView) findViewById(R.id.image_svg);
imageView.setImageDrawable(animatedVectorDrawable);
animatedVectorDrawable.start();
上述代码可以让我们在Android L以上去播放一个AnimatedVectorDrawable,但在低版本上xml中的标签animated-vector会报错:org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag animated-vector
即使用上了AnimatedVectorDrawableCompat也无效,所以如果不用三方库(后边会说),只能将其分开两种版本来写:API21以上用动画,A