APP开发实战95-动态Vector基础

来源:互联网 发布:海量数据的处理 编辑:程序博客网 时间:2024/06/05 07:47

24.5Vector动态图的使用

24.5.1动态Vector基础

    动态的Vector需要通过animated-vector标签来进行实现,它就像一个粘合剂,将控件与Vector图像粘合在了一起,一个基础的animated-vector代码如下所示:

<?xmlversion="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
   android:drawable="@drawable/vector_drawable">
    <target
        android:name="star"
       android:animation="@animator/star_anim" />
</animated-vector>

目标图像是drawable//vector_drawable,name属性,就是在静态Vector图像中group或者path标签的name属性。

 

目标图像:

    <vectorxmlns:android="http://schemas.android.com/apk/res/android"
    android:width="500px"
    android:height="500px"
   android:viewportHeight="500"
   android:viewportWidth="500">
    <group
        android:name="star_group"
        android:scaleX="5.0"
       android:scaleY="5.0">
        <path
            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" />
    </group>
</vector>

这里的Vector图像比之前的要多了一个group标签。group标签的作用有两个:

    对Path进行分组,由于后面需要针对Path进行动画,所以可以让具有同样动画效果的Path在同一个Group中

拓展动画效果,单个的path标签是没有translateX和translateY属性的,因此无法使用属性动画来控制pathtranslateY,而group标签是有的,所以我们需要先将相关的path标签元素包裹在一个个的group标签中。

 

动画效果star_anim.xml,就是基础的属性动画:

<?xmlversion="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="5000"
       android:propertyName="trimPathStart"
       android:repeatCount="infinite"
        android:repeatMode="restart"
        android:valueFrom="1"
        android:valueTo="0"/>
    <objectAnimator
        android:duration="5000"
       android:propertyName="strokeColor"
       android:repeatCount="infinite"
       android:repeatMode="restart"
        android:valueFrom="@color/colorAccent"
       android:valueTo="@color/colorPrimaryDark" />
</set>

在代码中使用:

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

0 0
原创粉丝点击