VectorDrawable与AnimatedVectorDrawable

时间:2022-04-26
本文章向大家介绍VectorDrawable与AnimatedVectorDrawable,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

VectorDrawable 

Android L开始提供了新的API VectorDrawable 可以使用SVG类型的资源,也就是矢量图。先来一个例子吧。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="256dp"
    android:height="256dp"
    android:viewportHeight="32"
    android:viewportWidth="32">

    <path
        android:fillColor="#e11c00"
        android:pathData="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" />
</vector>

上面的代码绘制出来的就是这么一个图像,控制显示心形的就是上面path这个标签,一个path代表一个元素,绘制的内容是pathData下的一长串字符,里面是SVG绘制的一系列命令,提供moveTo、lineTo、close等操作。那么接下来说说这些M,c.l标签吧。

  • 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取顺时针方向  有个图解: 

有了这些,我们可以随意的定制矢量图了,来一发,我们定义一个三角形

<vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:height="64dp"
     android:width="64dp"
     android:viewportHeight="600"
     android:viewportWidth="600" >
     <group
         android:name="rotationGroup"
         android:pivotX="300.0"
         android:pivotY="300.0"
         android:rotation="45.0" >
         <path
             android:name="v"
             android:fillColor="#000000"
             android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
     </group>
 </vector>

我们说说这些常用标签的含义:

path 元素里面的 pathData 就是矢量图的路径数据,除此之外还可以设置其他属性。 path 元素一共包含如下属性:

  • android:name 定义该 path 的名字,这样在其他地方可以通过名字来引用这个路径
  • android:pathData 和 SVG 中 d 元素一样的路径信息。
  • android:fillColor 定义填充路径的颜色,如果没有定义则不填充路径
  • android:strokeColor 定义如何绘制路径边框,如果没有定义则不显示边框
  • android:strokeWidth 定义路径边框的粗细尺寸
  • android:strokeAlpha 定义路径边框的透明度
  • android:fillAlpha 定义填充路径颜色的透明度
  • android:trimPathStart 从路径起始位置截断路径的比率,取值范围从 0 到1
  • android:trimPathEnd 从路径结束位置截断路径的比率,取值范围从 0 到1
  • android:trimPathOffset 设置路径截取的范围 Shift trim region (allows showed region to include the start and end), in the range from 0 to 1.
  • android:strokeLineCap 设置路径线帽的形状,取值为 butt, round, square.
  • android:strokeLineJoin 设置路径交界处的连接方式,取值为 miter,round,bevel.
  • android:strokeMiterLimit 设置斜角的上限,Sets the Miter limit for a stroked path.注: 当strokeLineJoin设置为 “miter” 的时候, 绘制两条线段以锐角相交的时候,所得的斜面可能相当长。当斜面太长,就会变得不协调。strokeMiterLimit 属性为斜面的长度设置一个上限。这个属性表示斜面长度和线条长度的比值。默认是 10,意味着一个斜面的长度不应该超过线条宽度的 10 倍。如果斜面达到这个长度,它就变成斜角了。当 strokeLineJoin 为 “round” 或 “bevel” 的时候,这个属性无效。

根元素 vector 是用来定义这个矢量图的,该元素包含如下属性:

  • android:name 定义该drawable的名字
  • android:width 定义该 drawable 的内部(intrinsic)宽度,支持所有 Android 系统支持的尺寸,通常使用 dp
  • android:height 定义该 drawable 的内部(intrinsic)高度,支持所有 Android 系统支持的尺寸,通常使用 dp
  • android:viewportWidth 定义矢量图视图的宽度,视图就是矢量图 path 路径数据所绘制的虚拟画布
  • android:viewportHeight 定义矢量图视图的高度,视图就是矢量图 path 路径数据所绘制的虚拟画布
  • android:tint 定义该 drawable 的 tint 颜色。默认是没有 tint 颜色的
  • android:tintMode 定义 tint 颜色的 Porter-Duff blending 模式,默认值为 src_in
  • android:autoMirrored 设置当系统为 RTL (right-to-left) 布局的时候,是否自动镜像该图片。比如 阿拉伯语。
  • android:alpha 该图片的透明度属性

有时候我们需要对几个路径一起处理,这样就可以使用 group 元素来把多个 path 放到一起。 group 支持的属性如下:

  • android:name 定义 group 的名字
  • android:rotation 定义该 group 的路径旋转多少度
  • android:pivotX 定义缩放和旋转该 group 时候的 X 参考点。该值相对于 vector 的 viewport 值来指定的。
  • android:pivotY 定义缩放和旋转该 group 时候的 Y 参考点。该值相对于 vector 的 viewport 值来指定的。
  • android:scaleX 定义 X 轴的缩放倍数
  • android:scaleY 定义 Y 轴的缩放倍数
  • android:translateX 定义移动 X 轴的位移。相对于 vector 的 viewport 值来指定的。
  • android:translateY 定义移动 Y 轴的位移。相对于 vector 的 viewport 值来指定的。

通过上面的属性可以看出, group 主要是用来设置路径做动画的关键属性的。

最后, vector 还支持 clip-path 元素。定义当前绘制的剪切路径。注意,clip-path 只对当前的 group 和子 group 有效。

  • android:name 定义 clip path 的名字
  • android:pathData 和 android:pathData 的取值一样。

从上面 vector 支持的属性可以看出,功能还是比较丰富的。例如 前面提到的三角形,通过 group 可以把其旋转 90度

AnimatedVectorDrawable

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

AnimatedVectorDrawable类可以去创建一个矢量资源的动画。

你通常在三个XML文件中定义矢量资源的动画载体:

<vector>元素的矢量资源,在res/drawable/(文件夹)

<animated-vector>元素的矢量资源动画,在res/drawable/(文件夹)

< objectAnimator>元素的一个或多个对象动画器,在res/anim/(文件夹)

矢量资源动画能创建<group>和<path>元素属性的动画。<group>元素定义了一组路径或子组,并且<path>元素定义了要被绘制的路径。

当你想要创建动画时去定义矢量资源,使用android:name属性分配一个唯一的名字给组和路径,这样你可以从你的动画定义中查询到它们。

接下来我就以旋转的小三角为例:

然后在看一下animated-vector文件:

<span style="font-size:14px;"><animated-vector xmlns:android="http://schemas.android.com/apk/res/android"  
                 android:drawable="@drawable/vectordrawable" >  
  <target  
      android:name="rotationGroup"  
      android:animation="@anim/rotation" />  
  <target  
      android:name="v"  
      android:animation="@anim/path_morph" />  
</animated-vector>  </span>

从上面代码我们可以看出配置了两个动画,一个是旋转动画一个是变化形状的动画。 旋转动画:

<objectAnimator  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:duration="6000"  
    android:propertyName="rotation"  
    android:valueFrom="0"  
    android:valueTo="360"/>  
那么在布局中怎么用呢。
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/container"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_gravity="center"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    android:gravity="center_horizontal"  
    android:orientation="vertical"  
    tools:context=".ExampleActivity">  
  <TextView  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:layout_margin="@dimen/margin"  
      android:text="@string/example_from_documentation"  
      android:drawableBottom="@drawable/avd"/>  
  <TextView  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:layout_margin="@dimen/margin"  
      android:text="@string/rotation_only"  
      android:drawableBottom="@drawable/avd_rotation_only"/>  
  <TextView  
      android:layout_width="wrap_content"  
      android:layout_height="wrap_content"  
      android:layout_margin="@dimen/margin"  
      android:text="@string/path_morph_only"  
      android:drawableBottom="@drawable/avd_path_morph_only"/>  
</LinearLayout> 

变化形状动画:

<span style="font-size:14px;"><objectAnimator  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:duration="3000"  
    android:propertyName="pathData"  
    android:valueFrom="M300,70 l 0,-70 70,70 0,0   -70,70z"  
    android:valueTo="M300,70 l 0,-70 70,0  0,140 -70,0 z"  
    android:valueType="pathType"/>  </span>

3个TextView,我们只需要关注第一个TextView即可,因为其他都是一样的配置。 配置了一个avb也就是上面贴的animated-vector文件。

最后看一下Activity的启动动画代码:

<span style="font-size:14px;">TextView textView = (TextView) view;  
for (final Drawable drawable : textView.getCompoundDrawables()) {  
  if (drawable instanceof Animatable) {  
    ((Animatable) drawable).start();  
  }  
} </span><span style="font-size: 18px;"> </span>