24.5Vector动态图的使用

24.5.1动态Vector基础

动态的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

更多相关文章

  1. Android启动画面实现
  2. Android4: 旋转屏幕时禁止Activity重启
  3. android动画Android(安卓)动画实践
  4. Android的Activity动画切换
  5. android自定义属性之format介绍
  6. android 动画
  7. Android(安卓)init.rc init.vendor.rc 介绍
  8. android一些坑与小技巧
  9. 大家网Android开发规范

随机推荐

  1. Android(安卓)ImageView用法(xml)
  2. Android使用AIDL跨进程数据共享
  3. Android各种类型Dialog点击空白处自动关
  4. Android模拟器使用SD卡
  5. 搭建一个android 简单的框架应用
  6. Android : 一键分享(纲领)
  7. Android 2.2开始支持安装APP2SD卡上
  8. Android中padding与layout_margin的区别
  9. 一定是我打开的姿势不对——Android下使
  10. Android短信拦截机制适配的坑(下)--4.4以