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自定义属性之format介绍
  2. Android的Activity动画切换
  3. android动画Android 动画实践
  4. 关于自定义布局,xml中配置属性(attrs)
  5. Android动画效果 translate、scale、alpha、rotate 切换Activity
  6. Android第四十四期 - 登录界面的动画

随机推荐

  1. 开机启动流程
  2. Android Handler详解
  3. eclipse 遭遇及解决 debug调试时 因Andro
  4. 记录Android应用程序行为-peachbox
  5. ‍Android(安卓)尺寸和分辨率
  6. Day6 快速学习OkHttp3的九大用法
  7. android webkit CSS3及Render初窥
  8. Android(安卓)完美退出 App 方法
  9. android 音效处理(1)
  10. 书籍汇总