在Android应用开发中,使用矢量图动画可以减少资源包体积,同时避免不同分辨率下的失真问题,animated-vector是实现矢量图动画的核心标签,通过XML配置就能完成动画效果的定义。

animated-vector基础概念
animated-vector的全称是AnimatedVectorDrawable,它是Android支持库中提供的矢量动画 drawable 资源,核心作用是将矢量图(VectorDrawable)和属性动画(ObjectAnimator)关联起来,让矢量图的路径、颜色、透明度等属性按照动画规则变化。
要实现animated-vector动画,需要准备三类XML资源:
- 矢量图资源:定义静态的矢量图形,使用<vector>标签编写
- 属性动画资源:定义动画的变化规则,使用<objectAnimator>标签编写
- animated-vector资源:关联矢量图和属性动画,使用<animated-vector>标签编写
资源文件编写步骤
1. 编写静态矢量图资源
首先在res/drawable目录下创建矢量图文件,比如ic_vector_demo.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportWidth="100"
android:viewportHeight="100">
<!-- 定义一个圆形路径,给路径设置name用于后续动画关联 -->
<path
android:name="circle_path"
android:fillColor="#FF4081"
android:pathData="M50,50 m-40,0 a40,40 0 1,0 80,0 a40,40 0 1,0 -80,0" />
</vector>
2. 编写属性动画资源
在res/animator目录下创建属性动画文件,比如anim_circle_scale.xml,定义圆形的缩放动画:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="scaleX"
android:duration="1000"
android:valueFrom="1.0"
android:valueTo="1.5"
android:repeatCount="infinite"
android:repeatMode="reverse" />
这里<objectAnimator>的propertyName指定要修改的属性,valueFrom和valueTo是属性的起始和结束值,duration是动画时长,repeatCount设置无限重复,repeatMode设置反向重复。
3. 编写animated-vector资源
在res/drawable目录下创建animated-vector文件,比如av_circle_demo.xml,关联矢量图和动画:
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_vector_demo">
<target
android:name="circle_path"
android:animation="@animator/anim_circle_scale" />
</animated-vector>
这里的<target>标签的name需要和矢量图中path的name一致,animation属性指向刚才定义的属性动画资源。
在布局和代码中使用
布局中引用
直接在ImageView的src中设置animated-vector资源:
<ImageView
android:id="@+id/iv_vector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/av_circle_demo"
android:layout_gravity="center" />
代码中触发动画
在Activity或Fragment中获取ImageView的drawable,强转为AnimatedVectorDrawable后调用start方法:
import android.graphics.drawable.AnimatedVectorDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class VectorAnimActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vector_anim);
ImageView imageView = findViewById(R.id.iv_vector);
if (imageView.getDrawable() instanceof AnimatedVectorDrawable) {
AnimatedVectorDrawable animatedVectorDrawable = (AnimatedVectorDrawable) imageView.getDrawable();
// 启动动画
animatedVectorDrawable.start();
}
}
}
常见注意事项
- 矢量图中需要被动画控制的路径、群组必须设置唯一的name属性,否则animated-vector无法关联到对应的元素
- 如果使用AndroidX,需要依赖appcompat库,确保AnimatedVectorDrawable的兼容性,低版本系统也能正常显示动画
- 属性动画的propertyName需要和矢量图元素支持的可修改属性对应,比如path支持strokeColor、fillColor、trimPathStart等属性,修改其他不存在的属性会导致动画无效
- 复杂动画可以组合多个<target>标签,给同一个矢量图的不同元素设置不同的动画效果
注意:如果矢量图中有<group>标签包裹多个path,给group设置name后,也可以将动画关联到group上,修改group的缩放、旋转等属性。
animated_vector矢量图动画XML配置Android动画修改时间:2026-07-20 14:00:27