24.5Vector动态图的使用
动态的Vector需要通过animated-vector标签来进行实现,它就像一个粘合剂,将控件与Vector图像粘合在了一起,一个基础的animated-vector代码如下所示:
Android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vector_drawable">
android:name="star"
android:animation="@animator/star_anim" />
目标图像是drawable//vector_drawable,name属性,就是在静态Vector图像中group或者path标签的name属性。
目标图像:
android:width="500px"
android:height="500px"
android:viewportHeight="500"
android:viewportWidth="500">
android:name="star_group"
android:scaleX="5.0"
android:scaleY="5.0">
android:name="star"
android:pathData="M50.0,90.0 L 82.9193546357,27.2774101308 L 12.5993502926,35.8158045183 L59.5726265715,88.837672697 L 76.5249063296,20.0595700732 L10.2916450361,45.1785327898 L 68.5889268818,85.4182410261 L68.5889268818,14.5817589739 L 10.2916450361,54.8214672102 L76.5249063296,79.9404299268 L 59.5726265715,11.162327303 L12.5993502926,64.1841954817 L 82.9193546357,72.7225898692 L 50.0,10.0 L17.0806453643,72.7225898692 L 87.4006497074,64.1841954817 L40.4273734285,11.162327303 L 23.4750936704,79.9404299268 L89.7083549639,54.8214672102 L 31.4110731182,14.5817589739 L31.4110731182,85.4182410261 L 89.7083549639,45.1785327898 L23.4750936704,20.0595700732 L 40.4273734285,88.837672697 L87.4006497074,35.8158045183 L 17.0806453643,27.2774101308 L 50.0,90.0Z"
android:strokeColor="@color/colorAccent"
android:strokeWidth="2" />
这里的Vector图像比之前的要多了一个group标签。group标签的作用有两个:
对Path进行分组,由于后面需要针对Path进行动画,所以可以让具有同样动画效果的Path在同一个Group中
拓展动画效果,单个的path标签是没有translateX和translateY属性的,因此无法使用属性动画来控制pathtranslateY,而group标签是有的,所以我们需要先将相关的path标签元素包裹在一个个的group标签中。
动画效果star_anim.xml,就是基础的属性动画:
android:duration="5000"
android:propertyName="trimPathStart"
android:repeatCount="infinite"
android:repeatMode="restart"
android:valueFrom="1"
android:valueTo="0"/>
android:duration="5000"
android:propertyName="strokeColor"
android:repeatCount="infinite"
android:repeatMode="restart"
android:valueFrom="@color/colorAccent"
android:valueTo="@color/colorPrimaryDark" />
在代码中使用:
ImageViewimageView = (ImageView) findViewById(R.id.image_view);
Drawable drawable = imageView.getDrawable();
//AnimatedVectorDrawableCompat实现了Animatable接口
if (drawable instanceof Animatable){
((Animatable) drawable).start();
}
参考;http://blog.csdn.net/eclipsexys/article/details/51838119