Android VectorDrawable与SVG

来源:互联网 发布:手机腾讯视频网络错误1 编辑:程序博客网 时间:2024/06/11 21:56

VectorDrawable

Android L开始提供了新的API VectorDrawable 可以使用SVG类型的资源,也就是矢量图。在xml文件中的标签是<vector>,下面是一个例子

<code class="language-xml hljs  has-numbering"><span class="hljs-comment"><!-- res/drawable/heart.xml --></span><span class="hljs-tag"><<span class="hljs-title">vector</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>    <!<span class="hljs-attribute">--</span> <span class="hljs-attribute">intrinsic</span> <span class="hljs-attribute">size</span> <span class="hljs-attribute">of</span> <span class="hljs-attribute">the</span> <span class="hljs-attribute">drawable</span> <span class="hljs-attribute">--</span>></span>    android:height="256dp"    android:width="256dp"    <span class="hljs-comment"><!-- size of the virtual canvas --></span>    android:viewportWidth="32"    android:viewportHeight="32">  <span class="hljs-comment"><!-- draw a path --></span>  <span class="hljs-tag"><<span class="hljs-title">path</span> <span class="hljs-attribute">android:fillColor</span>=<span class="hljs-value">"#8fff"</span>      <span class="hljs-attribute">android:pathData</span>=<span class="hljs-value">"M20.5,9.5                        c-1.955,0,-3.83,1.268,-4.5,3                        c-0.67,-1.732,-2.547,-3,-4.5,-3                        C8.957,9.5,7,11.432,7,14                        c0,3.53,3.793,6.257,9,11.5                        c5.207,-5.242,9,-7.97,9,-11.5                        C25,11.432,23.043,9.5,20.5,9.5z"</span> /></span><span class="hljs-tag"></<span class="hljs-title">vector</span>></span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul>

这样就定义好了一个静态的矢量图,可以像一般的图片资源使用,设置到imageView中会显示出一个心形。控制显示心形的就是上面path这个标签,一个path代表一个元素,绘制的内容是pathData下的一长串字符,里面是SVG绘制的一系列命令,提供moveTo、lineTo、close等操作,可以和Graphics 中的Path操作对应起来,具体可以查看SVG path Ref,后面会简要说明一下。
VectorDrawable定义的是一张静态图,还有一个类AnimatedVectorDrawable,可以让矢量图有动画效果。一般需要三个步骤:

  • 定义VectorDrawable
<code class="language-xml hljs  has-numbering"><span class="hljs-tag"><<span class="hljs-title">vector</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>     <span class="hljs-attribute">android:height</span>=<span class="hljs-value">"64dp"</span>     <span class="hljs-attribute">android:width</span>=<span class="hljs-value">"64dp"</span>     <span class="hljs-attribute">android:viewportHeight</span>=<span class="hljs-value">"600"</span>     <span class="hljs-attribute">android:viewportWidth</span>=<span class="hljs-value">"600"</span> ></span>     <span class="hljs-tag"><<span class="hljs-title">group</span>         <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"rotationGroup"</span>         <span class="hljs-attribute">android:pivotX</span>=<span class="hljs-value">"300.0"</span>         <span class="hljs-attribute">android:pivotY</span>=<span class="hljs-value">"300.0"</span>         <span class="hljs-attribute">android:rotation</span>=<span class="hljs-value">"45.0"</span> ></span>         <span class="hljs-tag"><<span class="hljs-title">path</span>             <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"v"</span>             <span class="hljs-attribute">android:fillColor</span>=<span class="hljs-value">"#000000"</span>             <span class="hljs-attribute">android:pathData</span>=<span class="hljs-value">"M300,70 l 0,-70 70,70 0,0 -70,70z"</span> /></span>     <span class="hljs-tag"></<span class="hljs-title">group</span>></span> <span class="hljs-tag"></<span class="hljs-title">vector</span>></span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li></ul>
  • 定义AnimatedVectorDrawable,给上面矢量图的元素添加动画
<code class="language-xml hljs  has-numbering"><span class="hljs-tag"><<span class="hljs-title">animated-vector</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>   <span class="hljs-attribute">android:drawable</span>=<span class="hljs-value">"@drawable/vectordrawable"</span> ></span>     <span class="hljs-tag"><<span class="hljs-title">target</span>         <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"rotationGroup"</span>         <span class="hljs-attribute">android:animation</span>=<span class="hljs-value">"@anim/rotation"</span> /></span>     <span class="hljs-tag"><<span class="hljs-title">target</span>         <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"v"</span>         <span class="hljs-attribute">android:animation</span>=<span class="hljs-value">"@anim/path_morph"</span> /></span> <span class="hljs-tag"></<span class="hljs-title">animated-vector</span>></span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
  • 定义动画文件
<code class="language-xml hljs  has-numbering"><span class="hljs-tag"><<span class="hljs-title">objectAnimator</span>     <span class="hljs-attribute">android:duration</span>=<span class="hljs-value">"6000"</span>     <span class="hljs-attribute">android:propertyName</span>=<span class="hljs-value">"rotation"</span>     <span class="hljs-attribute">android:valueFrom</span>=<span class="hljs-value">"0"</span>     <span class="hljs-attribute">android:valueTo</span>=<span class="hljs-value">"360"</span> /></span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
<code class="language-xml hljs  has-numbering"><span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>></span>     <span class="hljs-tag"><<span class="hljs-title">objectAnimator</span>         <span class="hljs-attribute">android:duration</span>=<span class="hljs-value">"3000"</span>         <span class="hljs-attribute">android:propertyName</span>=<span class="hljs-value">"pathData"</span>         <span class="hljs-attribute">android:valueFrom</span>=<span class="hljs-value">"M300,70 l 0,-70 70,70 0,0   -70,70z"</span>         <span class="hljs-attribute">android:valueTo</span>=<span class="hljs-value">"M300,70 l 0,-70 70,0  0,140 -70,0 z"</span>         <span class="hljs-attribute">android:valueType</span>=<span class="hljs-value">"pathType"</span>/></span> <span class="hljs-tag"></<span class="hljs-title">set</span>></span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li></ul>

由于矢量图的特点,AnimatedVectorDawable可以实现一些很特别的效果,对VectorDrawable里的pathData做动画,可以从一个图形渐变到另一个图形,比如Material Design里的toolbar icon;对trimPathStart、trimPathEnd做动画,可以得到图形的绘制轨迹。

SVG Path Data

主要有以下一些命令

  • M: move to 移动绘制点
  • L:line to 直线
  • Z:close 闭合
  • C:cubic bezier 三次贝塞尔曲线
  • Q:quatratic bezier 二次贝塞尔曲线
  • A:ellipse 圆弧

每个命令都有大小写形式,大写代表后面的参数是绝对坐标,小写表示相对坐标。参数之间用空格或逗号隔开

命令详解:

  • M (x y) 移动到x,y
  • L (x y) 直线连到x,y,还有简化命令H(x) 水平连接、V(y)垂直连接
  • Z,没有参数,连接起点和终点
  • C(x1 y1 x2 y2 x y),控制点x1,y1 x2,y2,终点x,y
  • Q(x1 y1 x y),控制点x1,y1,终点x,y
  • A(rx ry x-axis-rotation large-arc-flag sweep-flag x y)
    rx ry 椭圆半径
    x-axis-rotation x轴旋转角度
    large-arc-flag 为0时表示取小弧度,1时取大弧度
    sweep-flag 0取逆时针方向,1取顺时针方向
    有个图解:

应用

在github上看到一个VectorDrawable应用的例子,实现了一个动态效果的searchbar,原理就是对VectorDrawable trimPathStart这个属性做动画。最初的设计在这里,照着实现一下:

Reference

  • https://developer.android.com/training/material/drawables.html
  • https://developer.android.com/reference/android/graphics/drawable/VectorDrawable.html
  • https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html
  • http://www.w3.org/TR/SVG11/paths.html#PathData
转自:http://blog.csdn.net/xu_fu/article/details/44004841
0 0
原创粉丝点击