Android SVG动画详细例子-程序员宅基地

技术标签: Android开发相关  view  java  android  

系列文章目录

Android SVG动画详细例子


老规矩,效果实现有源码

前言

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


一、看一下实现效果

请添加图片描述

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

文章链接: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();
        }
    }
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_42761395/article/details/123648038

智能推荐

vue3背景下,el-input嵌套在弹出框中,自动聚焦“失效”?如何实现自动聚焦_vue3 el-input 自动聚焦autofocus无效-程序员宅基地

文章浏览阅读436次,点赞15次,收藏2次。原因或许是,使用autofocus时,确实聚焦了!但是当我们又点击 显示弹出框的按钮时,input又失焦了,所以当我们看到input框时,没有自动聚焦。_vue3 el-input 自动聚焦autofocus无效

linux网络服务配置说课,《说课稿LINUX》PPT课件.ppt-程序员宅基地

文章浏览阅读222次。《《说课稿LINUX》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《说课稿LINUX》PPT课件.ppt(16页珍藏版)》请在装配图网上搜索。1、LINUX 基础应用与配置管理 桂林山水职业学院计算机系 朱笑雷 主要内容 课程定位 1 课程内容设置 2 教学方法与手段 3 教材建设 4 教学团度 5 主要内容 实践条件 6 课程考核 7 教学效果 8 课程特色 9 建设思路 10 一、课..._linux说课课件

在SpringBoot中启动时关于连接数据库失败的问题_springboot启动时数据库连接失败 不关闭-程序员宅基地

文章浏览阅读2.2k次。#在SpringBoot中启动时关于连接数据库失败的问题对照了application.yml,发现配置文件貌似没什么问题,但是在查找信息之后,发现问题正是出现在application.yml中问题出于datasource下的data-username和data-password只要将data-username和data-password改为username和password即可..._springboot启动时数据库连接失败 不关闭

antd-pro(V5)动态菜单_antdpro的菜单-程序员宅基地

文章浏览阅读4.6k次。一般情况下登录系统后菜单是由后端返回的,不是前端写死的。antd-pro也支持,修改的路径在app.tsx在 layout 里加一个menuDataRender字段先给一个() =>[]可以看到左侧菜单没了,说明配置生效了,接下来就可以围绕这个配置做文章了,我们先定义一个 menuDataRender方法。根据登录缓存到本地的数据做下处理,判断菜单里要展示哪些内容(比如替换字段,隐藏不显示的菜单,隐藏按钮等),处理好了后返回一个数组结构即可。示例代码如下export const layout: _antdpro的菜单

Linux安装使用jprofiler6分析服务器应用状态-程序员宅基地

文章浏览阅读77次。为什么80%的码农都做不了架构师?>>> ..._jprofiler6 key

苏小红C语言第四版课后习题练习7.7最大公约数三种计算方式_c语言程序设计第四版课后题答案苏小红第七章-程序员宅基地

文章浏览阅读170次。(可以看出递归算法更加侧重于计算的技巧,并且计算机计算的次数也相对更少);_c语言程序设计第四版课后题答案苏小红第七章

随便推点

Fiddler抓包,并修改请求数据_filder改数据包-程序员宅基地

文章浏览阅读4.4w次,点赞8次,收藏51次。浏览器抓包(工具:fiddler)并 修改请求内容 工具下载:https://pan.baidu.com/s/1pyKdAwgTdNNvoWA2bGlk9A 1、正常打开网页,输入要提交的内容 2、打开工具,f11暂停了页面的所有提交动作 3、这时再点击提交按钮,请求的数据就会被工具拦截 4、双击截取的数据,右侧会看到请求的具体内容,任意修改数据 5、点击绿色按钮 run ..._filder改数据包

视频格式转换器榜单:10 款最值得拥有的高清视频转换器_奇客视频转换-程序员宅基地

文章浏览阅读560次。如果您想在计算机或任何其他设备上播放高质量的视频,高清视频转换器可以帮助确保您的视频与您的操作系统和硬件兼容。您还可以使用高清转换器更改视频的分辨率,无论您是想提高质量还是降低分辨率以生成更小的文件。在下表中,我们描述了用于转换高清视频的最流行和可用的桌面程序和在线服务。它们各有优缺点,因此请根据您的需要进行选择。_奇客视频转换

Unity血条效果,图片动画_游戏血条动图-程序员宅基地

文章浏览阅读1.9k次。欢迎来到unity学习、unity培训、unity企业培训教育专区,这里有很多U3D资源、U3D培训视频,我们致力于打造业内unity3d培训、学习第一品牌。今天开始做我们的游戏了,组长给分配了任务,我负责做剧情动画,人物血条和种植植物。 一、剧情动画 动画是以多个图片的形式展现的,图片是自己制作的。 private GUITextu_游戏血条动图

环境变量的加载顺序、环境变量集合_环境变量的顺序-程序员宅基地

文章浏览阅读1k次。*******字符编码ASCII,GB2312,GBK,Unicode,UTF-8比较参考:https://blog.csdn.net/softwarenb/article/details/51994943**环境变量的加载顺序:Mac系统的环境变量,加载顺序为:a. /etc/profileb. /etc/pathsc. ~/.bash_profiled. ~/..._环境变量的顺序

科学家发现让人类幸福感飙升的密码!给大脑植入这个算法 | 精选-程序员宅基地

文章浏览阅读316次。▼大型年度AI人物评选——2017中国AI英雄风云榜已于12月4日在乌镇张榜,12月18日在北京国贸三期举行颁奖典礼。榜单评选出年度技术创新人物TOP 10;商业创新人物TOP 10,获取完整榜单请关注网易智能公众号(ID:smartman163),回复关键词“评奖”。本文系网易智能工作室出品聚焦AI,读懂下一个大时代【网易智能讯12月10日消息】不只有你会_人类大脑植入代码

正则表达式匹配中括号内的内容_正则<>里内容-程序员宅基地

文章浏览阅读3.6k次。几经研究, 终于实现了。time[2020-06-04 11:43:36](?<=\[)(.*)(?=])(pattern) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 '\(' 或 '\)'。 (?:pattern) 匹配 pattern 但不获取匹配结..._正则<>里内容

推荐文章

热门文章

相关标签