Android animated-vector怎么实现矢量图动画XML配置

来源:网站主作者:天马头衔:网络博主
导读:本期聚焦于小伙伴创作的《Android animated-vector怎么实现矢量图动画XML配置》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《Android animated-vector怎么实现矢量图动画XML配置》有用,将其分享出去将是对创作者最好的鼓励。

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

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

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。