Android SVG动画详细例子

简介: 本文详细讲解了在Android中利用SVG实现动画效果的方法,通过具体例子帮助开发者更好地理解和应用SVG动画。文章首先展示了动画的实现效果,接着回顾了之前的文章链接及常见问题(如属性名大小写错误)。核心内容包括:1) 使用阿里图库获取SVG图形;2) 借助工具将SVG转换为VectorDrawable;3) 为每个路径添加动画绑定属性;4) 创建动画文件并关联SVG;5) 在ImageView中引用动画文件;6) 在Activity中启动动画。文末还提供了完整的代码示例和源码下载链接,方便读者实践操作。

系列文章目录

Android SVG动画详细例子

老规矩,效果实现有源码

前言

在之前发了一篇关于SVG动画的文章,有小伙伴反应了一些问题,所以出一篇较为详细的动画例子文章,希望有所帮助。

一、看一下实现效果

1.gif

二、之前例子链接,以及问题。

文章链接:Android利用SVG实现动画效果

具体准备工作,请看链接

小提:在写动画文件时, android:propertyName="trimPathStart" 词别写错了,End也是,如果写成小写会爆异常。

三、效果的实现

1.SVG图来源:阿里图库

2.svg转换为VectorDrawable工具:http://inloop.github.io/svg2android/

也可用Android自带。

3.转化后的代码

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

    <path
        android:fillColor="#fcd765"
        android:pathData="M35.67,33.93s-8.5-6.22-4.85-17.76S34.13,28.61,35.67,33.93Z" />
    <path
        android:fillColor="#fcd765"
        android:pathData="M35,33.93S19,31.48,19.9,17.83,29.68,32.9,35,33.93Z" />
    <path
        android:fillColor="#fcd765"
        android:pathData="M31.48,34S16.28,43.59,11,26.54,26.36,36.91,31.48,34Z" />
    <path
        android:fillColor="#fcd765"
        android:pathData="M38.73,33.1s8.5-6.22,4.84-17.77S40.27,27.78,38.73,33.1Z" />
    <path
        android:fillColor="#fcd765"
        android:pathData="M37,34s4-12.79 0.61 -18.48C33.46,8.75,36.57,26.94,37,34Z" />
    <path
        android:fillColor="#fcd765"
        android:pathData="M39.43,33.1s16-2.45,15.07-16.1S44.71,32.07,39.43,33.1Z" />
    <path
        android:fillColor="#fcd765"
        android:pathData="M42.92,33.19s15.2,9.56,20.43-7.48S48,36.08,42.92,33.19Z" />
    <path
        android:strokeColor="#000"
        android:strokeWidth="1"
        android:strokeMiterLimit="10"
        android:pathData="M38.1,36.69S35.78,53.26,45.36,62.3" />
</vector>

给每个path加上与动画绑定的属性: 如:android:name="leaf1"

完整代码如下(hua.xml)

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

    <path
        android:name="leaf1"
        android:fillColor="#fcd765"
        android:pathData="M35.67,33.93s-8.5-6.22-4.85-17.76S34.13,28.61,35.67,33.93Z" />
    <path
        android:name="leaf2"
        android:fillColor="#fcd765"
        android:pathData="M35,33.93S19,31.48,19.9,17.83,29.68,32.9,35,33.93Z" />
    <path
        android:name="leaf3"
        android:fillColor="#fcd765"
        android:pathData="M31.48,34S16.28,43.59,11,26.54,26.36,36.91,31.48,34Z" />
    <path
        android:name="leaf4"
        android:fillColor="#fcd765"
        android:pathData="M38.73,33.1s8.5-6.22,4.84-17.77S40.27,27.78,38.73,33.1Z" />
    <path
        android:name="leaf5"
        android:fillColor="#fcd765"
        android:pathData="M37,34s4-12.79 0.61 -18.48C33.46,8.75,36.57,26.94,37,34Z" />
    <path
        android:name="leaf6"
        android:fillColor="#fcd765"
        android:pathData="M39.43,33.1s16-2.45,15.07-16.1S44.71,32.07,39.43,33.1Z" />
    <path
        android:name="leaf7"
        android:fillColor="#fcd765"
        android:pathData="M42.92,33.19s15.2,9.56,20.43-7.48S48,36.08,42.92,33.19Z" />
    <path
        android:name="root"
        android:strokeColor="#000"
        android:strokeWidth="1"
        android:strokeMiterLimit="10"
        android:pathData="M38.1,36.69S35.78,53.26,45.36,62.3" />
</vector>

4.动画文件 (svg_pathanim)

本案例用的一个动画文件,也可用多个。

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="4000"
    android:propertyName="trimPathStart"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:valueFrom="1"
    android:valueTo="0"
    android:valueType="floatType">
</objectAnimator>

5.在drawable文件夹下新建文件将svg与动画进行关联(hua_anim.xml)

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:drawable="@drawable/hua">

    <target
        android:animation="@animator/svg_pathanim"
        android:name="leaf1"></target>
    <target
        android:animation="@animator/svg_pathanim"
        android:name="leaf2"></target>
    <target
        android:animation="@animator/svg_pathanim"
        android:name="leaf3"></target>
    <target
        android:animation="@animator/svg_pathanim"
        android:name="leaf4"></target>
    <target
        android:animation="@animator/svg_pathanim"
        android:name="leaf5"></target>
    <target
        android:animation="@animator/svg_pathanim"
        android:name="leaf6"></target>
    <target
        android:animation="@animator/svg_pathanim"
        android:name="leaf7"></target>
    <target
        android:animation="@animator/svg_pathanim"
        android:name="root"></target>
</animated-vector>

6.在ImageView中引用第5步的文件

    <ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        app:srcCompat="@drawable/hua_anim" />

7.在Activity中启动动画

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   
    private ImageView anim_path;

    private Drawable drawable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        anim_path = (ImageView) findViewById(R.id.iv);
        anim_path.setOnClickListener(this);

}

    @Override
    public void onClick(View view) {
   
        switch (view.getId()) {
   
            case R.id.iv:
                startAnim(anim_path);
                break;
        }
    }

    /**
     * 启动动画
     *
     * @param iv
     */
    private void startAnim(ImageView iv) {
   
        drawable = iv.getDrawable();
        if (drawable instanceof Animatable) {
   
            ((Animatable) drawable).start();
        }
    }
}
相关文章
|
Android开发 开发者
Android利用SVG实现动画效果
本文介绍了如何在Android中利用SVG实现动画效果。首先通过定义`pathData`参数(如M、L、Z等)绘制一个简单的三角形SVG图形,然后借助`objectAnimator`实现动态的线条绘制动画。文章详细讲解了从配置`build.gradle`支持VectorDrawable,到创建动画文件、关联SVG与动画,最后在Activity中启动动画的完整流程。此外,还提供了SVG绘制原理及工具推荐,帮助开发者更好地理解和应用SVG动画技术。
648 30
|
10月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
1576 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
缓存 Android开发
Android Studio中如何清理gradle缓存
Android Studio中如何清理gradle缓存
|
应用服务中间件
Tomcat打不开startup.bat
Tomcat打不开startup.bat
448 2
PageTransformer实现一个层叠的卡片
(一) 开始 ViewPager实现一个层叠的卡片,先看看效果 层叠卡片 我将其用在了APP的引导页面上,这个效果虽然看上去很难,但实际上实现起来特别的简单,主要是使用PageTransformer来实现这个效果,推荐先看一下hongyang的前置教程:Android 自定义 ViewPager 打造千变万化的图片切换效果,请确保你已经掌握前置,原理里都写了,就不在累述.(主要还是因为太懒了) (二) 编码 1. 首先我们先创建一个Activity,配置好页面,就像以下效果。
2739 0
|
传感器 数据可视化
【无人机】四轴无人机的轨迹进行可视化和动画处理(Matlab代码实现)
【无人机】四轴无人机的轨迹进行可视化和动画处理(Matlab代码实现)
737 0
|
人工智能 开发者
文章和 PPT 配图有救了!SVG 绘图专家智能体大揭秘
本文分享如何使用 DeepSeek-V3-0324 和 Claude 3.5 或 3.7 绘制出高质量的图片,可以作为文章配图也可以为 PPT 配图,效率成倍增长。文章还介绍了原型图绘制、图片重绘修改和彩色报纸风的进阶案例。希望本文提供的技巧对大家有帮助,大家也可以修改提示词定制自己喜欢的风格。
1601 13
|
Ubuntu Linux Windows
wsl常用命令大全
WSL(Windows Subsystem for Linux)的常用命令,包括查看帮助、更新WSL、查看和管理Linux发行版、设置默认版本等,以帮助用户更有效地管理和使用WSL环境。
1765 1

热门文章

最新文章