上篇我们学习了怎么显示SVG矢量图像,当然还有一个更强大的功能,就是让SVG图像动起来,先上一张效果图吧:

要实现上述动画效果,就得用AnimatedVectorDrawable这个类了,它就是负责VectorDrawable矢量图形的动画的,不过我们不能像属性动画那样去通过代码进行设置动画,而是需要通过animated-vector标签在xml文件中创建一个AnimatedVectorDrawable,那么怎么实现呢?

只需要四步:

第一步:

首先就是要创建一个VectorDrawable,在drawable目录下创建一个heart.xml,并且,我们知道矢量图是通过点和线进行绘制的,而在上篇中我又讲了path中M和Z两个命令,M是绘制的起始点,然后期间通过一系列的画直线、画弧等过程,最后通过Z命令来闭合这个绘制过程,也就是说绘制一个形状时候是从M出发到Z闭合。所以,面对path中混乱的排列,我们需要把M到Z的字符串进行隔开分别创建一个path(也可以合并为一个group,给它设置个name,然后对它进行动画),到时候进行绘制动画的时候,可以保证各个path绘制的时间一致,然后分别设置一个name,和线条的颜色和宽度,所以内容如下:

heart.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <vector xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:width="256dp"
  4. android:height="256dp"
  5. android:viewportHeight="100"
  6. android:viewportWidth="100">
  7. <path
  8. android:name="heart1"
  9. android:strokeColor="#E91E63"
  10. android:strokeWidth="1"
  11. android:pathData="
  12. M58.3,10.6C51.4,3.6,40,3.7,32,10.5c-1.3-1.1-2.6-2-4.1-2.8l-5.5-3.8c-0.3-0.2-0.6-0.2-1-0.1c-0.3,0.1-0.5,0.4-0.6,0.8l-0.2,1C15,4.9,9.6,6.7,5.7,10.6C-1.8,18.1-2,30,5.4,37.8L31.3,64c0.2,0.2,0.4,0.3,0.7,0.3s0.5-0.1,0.7-0.3l25.9-26.2C65.9,30.2,65.7,18,58.3,10.6z"
  13. />
  14. <path
  15. android:name="heart2"
  16. android:strokeColor="#E91E63"
  17. android:strokeWidth="1"
  18. android:pathData="
  19. M57.2,36.4L32,61.8L6.8,36.4c-1-1-1.8-2.1-2.5-3.3l7.5,3.4c0.4,0.2,0.8,0.3,1.2,0.3c0.4,0,0.8-0.1,1.1-0.2l0.7,6.4c0,0.5,0.4,0.8,0.8,0.9l9.4,1.4c0,0,0.1,0,0.1,0c0.3,0,0.5-0.1,0.7-0.3c0.2-0.2,0.3-0.6,0.2-0.9l-4-17.6l13.3,11.9c0.2,0.2,0.4,0.3,0.7,0.3c0.1,0,0.2,0,0.2,0c0.3-0.1,0.6-0.3,0.7-0.6l3.2-8.9c0.2-0.4,0-0.9-0.4-1.2l-5.3-3.6c0.6-0.5,1.1-1.3,1.1-2.1l0.7-9.1c0-0.3-0.1-0.7-0.4-0.9c-0.3-0.2-0.7-0.2-1-0.1l-3.6,1.5l1.1-0.9c0,0,0,0,0,0c0,0,0,0,0,0c7.2-6.7,17.8-7,24.2-0.6C63.5,18.6,63.7,29.6,57.2,36.4z"
  20. />
  21. <path
  22. android:name="heart3"
  23. android:strokeColor="#E91E63"
  24. android:strokeWidth="1"
  25. android:pathData="
  26. M14.3,20.7l1.6,1.9L5.3,26.4l-0.9-9.1L14.3,20.7z"
  27. />
  28. <path
  29. android:name="heart4"
  30. android:strokeColor="#E91E63"
  31. android:strokeWidth="1"
  32. android:pathData="
  33. M19.5,18.3l1.5,3.8 l-2,1.2l-2.6-3.1L19.5,18.3z"
  34. />
  35. <path
  36. android:name="heart5"
  37. android:strokeColor="#E91E63"
  38. android:strokeWidth="1"
  39. android:pathData="
  40. M17.2,24.3C17.3,24.3,17.3,24.3,17.2,24.3l0.4,0.5L14,34c0,0,0,0,0,0l0,0c-0.1,0.3-0.3,0.5-0.5,0.6c-0.3,0.1-0.6,0.1-0.8,0l-6.8-3.1l7.6-5.9L17.2,24.3z"
  41. />
  42. <path
  43. android:name="heart6"
  44. android:strokeColor="#E91E63"
  45. android:strokeWidth="1"
  46. android:pathData="
  47. M22.4,23.5 l9.6,1.2l6.2,4.3l-2.5,6.8L22.2,23.7L22.4,23.5z"
  48. />
  49. <path
  50. android:name="heart7"
  51. android:strokeColor="#E91E63"
  52. android:strokeWidth="1"
  53. android:pathData="
  54. M33.8,21.9c0,0.3-0.2,0.6-0.4,0.7c-0.2,0.2-0.5,0.2-0.9,0.2l-9.6-1.2l-0.3-0.7 l3.3-2.8l8.4-3.6L33.8,21.9z"
  55. />
  56. <path
  57. android:name="heart8"
  58. android:strokeColor="#E91E63"
  59. android:strokeWidth="1"
  60. android:pathData="
  61. M30.4,11.9l-8.4,7L21,16.5c0,0,0-0.1-0.1-0.1l1.9-9.5c0,0,0.2,0,0.2,0s0,0,0,0l-0.2-0.4L30.4,11.9z"
  62. />
  63. <path
  64. android:name="heart9"
  65. android:strokeColor="#E91E63"
  66. android:strokeWidth="1"
  67. android:pathData="
  68. M7.1,12c3.5-3.5,8.2-5.1,13.3-4.5l-1.5,8.8l-4.1,2.4L4.5,15.3C5.3,14.1,6.1,13,7.1,12z"
  69. />
  70. <path
  71. android:name="heart10"
  72. android:strokeColor="#E91E63"
  73. android:strokeWidth="1"
  74. android:pathData="
  75. M2.6,19.6l0.8,8.3c0,0.3,0.2,0.6,0.5,0.7c0.2,0.1,0.3,0.2,0.5,0.2c0.1,0,0.2,0,0.3-0.1l2.7-1L3.3,31c0,0,0,0,0,0C1.8,27.4,1.6,23.3,2.6,19.6z"
  76. />
  77. </vector>

第二步:

我们在drawable目录下新建一个根结点为animated-vector的heart_animator.xml文件,也就是为矢量图像创建一个AnimatedVectorDrawable。(drawable指的是VectorDrawable,而target指的是对名字为name的path或者group进行动画)。其中内容如下:

heart_vector_animator.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:drawable="@drawable/heart">
  4. <target
  5. android:name="heart1"
  6. android:animation="@animator/heart_animator" />
  7. <target
  8. android:name="heart2"
  9. android:animation="@animator/heart_animator" />
  10. <target
  11. android:name="heart3"
  12. android:animation="@animator/heart_animator" />
  13. <target
  14. android:name="heart4"
  15. android:animation="@animator/heart_animator" />
  16. <target
  17. android:name="heart5"
  18. android:animation="@animator/heart_animator" />
  19. <target
  20. android:name="heart6"
  21. android:animation="@animator/heart_animator" />
  22. <target
  23. android:name="heart7"
  24. android:animation="@animator/heart_animator" />
  25. <target
  26. android:name="heart8"
  27. android:animation="@animator/heart_animator" />
  28. <target
  29. android:name="heart9"
  30. android:animation="@animator/heart_animator" />
  31. <target
  32. android:name="heart10"
  33. android:animation="@animator/heart_animator" />
  34. </animated-vector>

第三步:

就是在animator目录下创建一个objectAnimator 动画,然后在第二步中分别设置给各个target。属性trimPathEnd的值是由0~1的,所以,path绘制时候也会表现出对应的动画,内容如下:

heart_animator.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:duration="6000"
  4. android:propertyName="trimPathEnd"
  5. android:valueFrom="0"
  6. android:valueTo="1"
  7. android:valueType="floatType" />

【注】:trimPathEnd这个属性是path的(表示绘制由开始点到绘制结束点),与之类似的还有trimPathStart(表示绘制由结束点后退到开始点),看下效果就明白了:

我现在设置trimPathStart的值由0~1,则效果变为:



可以看到动画是慢慢回退到起始点的。

第四步:

最后一步,我们已经把矢量图像和对应的动画都设置好了,此时你如果运行则不会有动画效果,所以接下来就是需要我们通过代码把动画开启起来并设置给ImageView显示,代码为:

  1. ImageView mImageView = (ImageView) findViewById(R.id.imageView);
  2. AnimatedVectorDrawable mAnimatedVectorDrawable = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.heart_vector_animator);//得到对应的AnimatedVectorDrawable对象
  3. mImageView.setImageDrawable(mAnimatedVectorDrawable);
  4. if(mAnimatedVectorDrawable!=null){
  5. mAnimatedVectorDrawable.start();
  6. }

我们得到AnimatorVectorDrawable对象后,只需要把这个Drawable对象设置给ImageView,然后通过AnimatedVectorDrawable.start()开启动画就可以了。

最后看一下目录结构:

当然也可以通过更改heart中每个path的线条颜色来达到不同的效果,如:

Demo下载

Android使用SVG矢量动画(二)的更多相关文章

  1. Android使用SVG矢量创建很酷的动态效率!

    尊重原创,欢迎转载.转载请注明: FROM  GA_studio   http://blog.csdn.net/tianjian4592 一个真正酷炫的动效往往让人虎躯一震,话不多说.咱们先瞅瞅效果: ...

  2. SVG矢量动画

    一.概述 相较于png.jpg等位图通过存储像素点来记录图像,svg (Scalable Vector Graphics)拥有一套自己的语法,通过描述的形式来记录图形.Android并不直接使用原始的 ...

  3. Android 使用 SVG 矢量图

    android svg矢量图 可能包含的操作有: SVG图还包括改变颜色,透明度,大小,矩阵操作(平移.旋转.缩放),selector, (图标,背景,按钮),动画,等 setTint(int Col ...

  4. Logo小变动,心境大不同,SVG矢量动画格式网站Logo图片制作与实践教程(Python3)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_207 曾几何时,SVG(Scalable Vector Graphics)矢量动画图被坊间称之为一种被浏览器诅咒的技术,只因为糟糕 ...

  5. Android SVG矢量资源的使用方法

    VectorDrawable 与 SVG Android 5.0(Lollipop, API 21)后,新增了<vector>标签,以VectorDrawable的形式支持SVG类型矢量图 ...

  6. Android中使用SVG矢量图(一)

    SVG矢量图介绍 首先要解释下什么是矢量图像,什么是位图图像? 1.矢量图像:SVG (Scalable Vector Graphics, 可伸缩矢量图形) 是W3C 推出的一种开放标准的文本式矢量图 ...

  7. Android 开发 VectorDrawable 矢量图 (二)了解矢量图属性与绘制

    VectorDrawable 矢量图 三部曲: Android 开发 VectorDrawable 矢量图 (一)了解Android矢量图与获取矢量图 Android 开发 VectorDrawabl ...

  8. Android中矢量动画

    Android中矢量动画 Android中用<path> 标签来创建SVG,就好比控制着一支画笔,从一点到一点,动一条线. <path> 标签 支持一下属性 M = (Mx, ...

  9. Android 开发 VectorDrawable 矢量图 (三)矢量图动画

    VectorDrawable 矢量图 三部曲: Android 开发 VectorDrawable 矢量图 (一)了解Android矢量图与获取矢量图 Android 开发 VectorDrawabl ...

随机推荐

  1. HOG OpenCV 代码片段

    直接上代码: #include <opencv2/opencv.hpp> using namespace cv; #include <cmath> using namespac ...

  2. 用scheme重写Python的三大函数map reduce 和filter

    重写过程中,发现这种做法能加深对递归的理解,而且reduce还体现了函数式编程是如何通过参数传递来实现命令式编程中的状态改变的. (define (imap f x . y) (if (null? y ...

  3. 将String转换为其表示的路径画到屏幕上

    关于这个问题,我已经在另一篇blog中有所提及: CoreText精彩文字轮廓绘制动画的一点改进 不过原有的转换代码使用Obj-C写的,在这里我们尝试将其转换为Swift语言,然后利用它实现一个测试小 ...

  4. Java程序员必备知识-多线程框架Executor详解

    为什么引入Executor线程池框架 new Thread()的缺点 每次new Thread()耗费性能 调用new Thread()创建的线程缺乏管理,被称为野线程,而且可以无限制创建,之间相互竞 ...

  5. Android ListView中Item点击事件失效解决方案

    欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 在平常的开发过程中,我们的ListView可能不只是简单的显示下文本或者按钮,更多的是显示复杂的布局,这样的话,我们就得自己写布 ...

  6. Java中循环声明变量方法

    Java循环声明变量 之前想这样做,但是网上一直搜索不到,下面是我的方式 项目中 // 得到需要查询外表的数量,然后分别创建缓存,插入数据多的时候如果编码在缓存里面,就不需要再去查询数据库了.key: ...

  7. Android项目开发填坑记-Fragment的onBackPressed

    Github版 CSDN版 知识背景 Fragment在当前的Android开发中,有两种引用方式,一个是 Android 3.0 时加入的,一个是supportV4包中的.这里简称为Fragment ...

  8. Hadoop:Hadoop简介及环境配置

    http://blog.csdn.net/pipisorry/article/details/51243805 Hadoop简介 下次写上... 皮皮blog 配置hadoop环境可能出现的问题 每次 ...

  9. UE4利用Save Game创建全局变量

    因为盲目的做了一个UE4的项目,没有用到UE4的无缝加载,我只能在一个个关卡中手动切换,然后每次的数据都会重置,这对于项目来说,造成了体验感的极度下降. 然而我查了一下怎样在UE4中创建全局变量,找到 ...

  10. 安卓中的事件分发机制之View控件

    前言:Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent e ...