Android高级动画篇之SVG矢量动画范例

作者:FranzLiszt1847 时间:2022-09-28 11:01:00 

效果视频

Android高级动画篇之SVG矢量动画范例

目录结构

Android高级动画篇之SVG矢量动画范例

SVG常用指令

L :为从当前点绘制到直线给定的点,后面跟着的为x,y坐标

M :为将画笔移动到某一点,但只是移动画笔,并没有绘制过程,所有没有产生绘制动作

A :为绘制一段弧线,允许弧线不闭合

初始化状态

效果图

Android高级动画篇之SVG矢量动画范例

制作静态SVG图型

首先在drawablw目录中建立一个svg_pic.xml文件夹

分别给两条直线名为Path1和Path2


<vector xmlns:android="http://schemas.android.com/apk/res/android"
   android:width="200dp"
   android:height="200dp"
   android:viewportHeight="100"
   android:viewportWidth="100">

<group>
       <path
           android:name="path1"
           android:pathData="
           M 20,80
           L 50,80 80,80"
           android:strokeColor="#cc0099"
           android:strokeLineCap="round"
           android:strokeWidth="5"/>

<path
           android:name="path2"
           android:pathData="
           M 20,20
           L 50,20 80,20"
           android:strokeColor="#cc0099"
           android:strokeLineCap="round"
           android:strokeWidth="5"/>
   </group>

</vector>

动画变换

在res目录下建立一个anim文件,在anim文件建立两个动画变化文件,分别为cross_anim1.xml和cross_anim2.xml
其中的valueFrom与valueTo属性分别对应了变换的起始坐标

cross_anim1.xml


<?xml version="1.0" encoding="utf-8"?>
<set
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:ordering="sequentially">
   <objectAnimator
       android:duration="500"
       android:propertyName="pathData"
       android:valueFrom="M 20,80 L 50,80 80,80"
       android:valueTo="M 20,80 L 50,50 80,80"
       android:valueType="pathType"
       android:interpolator="@android:anim/bounce_interpolator">
   </objectAnimator>
</set>

cross_anim2.xml


<set
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:ordering="sequentially">
   <objectAnimator
       android:duration="500"
       android:interpolator="@android:anim/bounce_interpolator"
       android:propertyName="pathData"
       android:valueFrom="
           M 20,20
           L 50,20 80,20"
       android:valueTo="
           M 20,20
           L 50,50 80,20"
       android:valueType="pathType"/>
</set>

动画黏合

最好通过animated-vector进行粘合,在drawable目录下创建link_anim.xml文件
drawable绑定svg静态图型的初始状态
target将两条直线的样式与变换进行绑定


<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
   android:drawable="@drawable/svg_pic">
   <target android:name="path1" android:animation="@anim/cross_anim1"/>
   <target android:name="path2" android:animation="@anim/cross_anim2"/>
</animated-vector>

引用


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <ImageView
       android:layout_gravity="center"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:srcCompat="@drawable/link_anim"
       android:onClick="anim"/>
</LinearLayout>

public void anim(View view) {
       ImageView imageView = (ImageView)view;
       Drawable drawable = imageView.getDrawable();
       if (drawable instanceof Animatable){
           ((Animatable)drawable).start();
       }
   }

解决低版本异常问题

在build.gradle文件的defaultConfig中添加如下语句


vectorDrawables.useSupportLibrary = true

来源:https://blog.csdn.net/News53231323/article/details/121235211

标签:Android,矢量,动画,SVG
0
投稿

猜你喜欢

  • Java命令设计模式优雅解耦命令和执行提高代码可维护性

    2023-11-23 06:25:46
  • IntelliJ IDEA优化配置的实现

    2023-01-01 03:29:55
  • JAVA中常见异常类

    2021-11-09 09:47:20
  • Java如何通过线程解决生产者/消费者问题

    2023-09-27 00:31:08
  • Java中id,pid格式数据转树和森林结构工具类实现

    2021-07-10 08:46:17
  • Java Socket编程实例(一)- TCP基本使用

    2023-11-11 08:42:50
  • 浅析c# 线程同步

    2022-09-19 18:43:03
  • Java实现导入导出Excel文件的方法(poi,jxl)

    2021-09-13 21:12:35
  • Java实现经典游戏打砖块游戏的示例代码

    2021-06-25 13:30:16
  • Java基于servlet监听器实现在线人数监控功能的方法

    2021-08-19 11:38:24
  • Android自定义可循环的滚动选择器CycleWheelView

    2023-04-06 00:43:16
  • Spring Boot与RabbitMQ结合实现延迟队列的示例

    2021-08-31 02:02:01
  • Mybatis selectKey 如何返回新增用户的id值

    2022-07-14 05:10:38
  • Java JDK动态代理实现原理实例解析

    2022-04-23 05:19:10
  • SpringCloud HystrixDashboard服务监控详解

    2021-12-16 03:35:47
  • 关于@ApiImplicitParams、ApiImplicitParam的使用说明

    2023-11-09 10:49:34
  • C#下使用XmlDocument操作XML详解

    2022-08-27 16:38:53
  • MyBatis 如何配置多个别名 typeAliasesPackage

    2021-11-16 06:35:54
  • C#实现ini文件读写操作

    2022-02-26 11:01:19
  • C 语言进制之间的转换

    2021-06-17 07:19:31
  • asp之家 软件编程 m.aspxhome.com