android 动画_牛逼!用Android矢量图动画实现一辆掘金牌小黄车!

点击上方“技术最TOP”,星标公众号

重磅干货,第一时间送达

640?wx_fmt=jpeg

作者:新小梦,链接:https://juejin.im/post/5f0417e5f265da22fd637d32

看完本文,每人送一台小黄车,掘金牌的~

不得不说,矢量图在项目中用得少之又少,却很香!可缩放矢量图形(SVG)是一套语法规范,常在前端中使用,而VectorDrawable(Android中的矢量图)只实现了SVG的部分语法。使用VectorDrawable代替位图可以减小 APK 的大小,因为可以针对不同的屏幕密度调整同一文件的大小,而不会降低图片质量,同时可以实现一些复制的效果图。

可以从下面两个地方获得常用矢量图:

  • IconFont
  • Android Stuido 自带的Vector Asset Studio工具

「Android版本兼容问题」

由于兼容低版本问题,导致矢量图得不到推广吧?但是现在大多数手机系统都Android 5.0起步了吧。

矢量图VectorDrawable仅支持到Android 4.4,通过支持库可最低支持到Android 2.1。

矢量图动画AnimatedVectorDrawable仅支持到Android 5.0,通过支持库最低支持到Android 3.0。 「Gralde配置」

    android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}

dependencies {
//需要 Android 支持库 23.2 或更高版本以及 Android Plugin for Gradle 2.0 或更高版本
implementation 'com.android.support:appcompat-v7:23.2.0'
}
复制代码

「美图减压鉴赏:」640?wx_fmt=gif

矢量图

通过Android Studio的Vector Asset Studio工具来获取一张矢量图。

640?wx_fmt=other根据个人喜好配置Vector Assert。640?wx_fmt=other会在drawable文件夹生成资源文件,例如这里生成ic_menu.xml文件:

"http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal"> android:fillColor="@android:color/white"
android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z"/>
复制代码

没了解过矢量图,相信是看不懂path标签pathData属性的内容。

vector标签设置矢量图大小,widthheight属性分别为24dpviewportWidthviewportHeight属性表示画布的大小,表示将矢量图等分的份数,这里划分为24*24。可以理解在一张24dp*24dp的图片上有24*24个小方格,通过这些小方格,可以绘制不同图案。

path可以理解为路径,图片绘制的内容。fillColor属性表示填充颜色,pathData属性表示在图片上作画内容。

pathData属性的值是SVG的语法内容,通过下面的内容就可以了解pathData属性值的含义了。

常用命令:

  • 「M x,y」 移动到坐标(x,y)。M3,18表示将画笔移动到坐标(3,18)

  • 「L x,y」从当前的位置画一条直线到指定的位置(x,y)。

  • 「H x」 画水平线到x位置。

  • 「V y」 画垂直线到y位置。

  • 「Z」 闭合,连接终点和起点

  • 「A rx,ry,xRotationAnagle,radianFlag,sweepFlag,x,y」 画弧线,理解为椭圆的一部分,rxry表示 x轴和y轴半径,即椭圆的长短轴问题;xRotationAnagle表示x轴旋转角度(搞不明白用法);radianFlag 0表示取小弧度,1表示取大弧度;sweepFlag 0表示逆时针,表示1顺时针画弧线;xy弧线的终点位置,起点位置为画笔所在的地方。

  • 「C x1,y1,x2,y2,x3,y3」 三次贝赛曲线

  • 「S x2,y2,x,y」 光滑三次贝塞尔曲线

  • 「Q x1,y1,x2,y2」 二次贝赛曲线

  • 「T x,y」 映射

ps:大写表示绝对坐标,小写表示相对坐标。

pathData标签内容进行解读:

"http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal"> android:fillColor="@android:color/white"
android:pathData="M3,18 h18 v-2 L3,16 v2 z M3,13 h18 v-2 L3,11 v2 z M3,6 v2 h18 L21,6 L3,6 z"/>
复制代码

M3,18 将画笔移动到坐标(3,18);

h18从坐标(3,18)到坐标(21,18)画一条水平直线;

v-2从坐标(21,18)到坐标(21,16)画一条垂直直线;

L3,16从坐标(21,18)到坐标(3,16)画一条直线;

v2从坐标(3,16)到坐标(3,18)画一条垂直直线;

z 闭合起点和终点。

到这里,最底部的直线就会画出来了,其他两条线是相同原理。

「注意事项」:不要将pathData的值抽离出来放到string.xml文件,不然在兼容5.0以下机型生成png图片,会报pathData错误。

path标签除了pathData属性,还有哪些可用的属性呢?

path标签

path标签可用属性:

  • name:路径名称,可在其他地方引用,例如矢量图动画引用;
  • strokeWidth:线条宽度,单位为viewportHeightviewportWidth中的1等分;。
  • strokeColor:线条颜色;
  • strokeAlpha:线条透明度。0f->1f
  • strokeLineJoin:线条拐角的形状。圆角round、斜切尖角miter、斜角bevel,例如正方形的四个角;
  • strokeLineCap:线条线帽形状。圆角round、正方形square、臂butt
  • strokeMiterLimit:斜线miter与strokeWidth的比例上限。如果比例值超过这个值,不再显示尖角而是bevel斜角。当strokeLineJoin属性设置为miter才生效。
  • fillColor:填充颜色;
  • fillType:填充类型,取值nonZeroevenOdd;
  • fillAlpha:填充透明度;
  • trimPathStart:从路径开始位置剪掉比率的内容,留下剩下的,0f->1f;
  • trimPathEnd:从路径开始位置截取比率的内容,留下截取的内容,0f->1f;
  • trimPathOffsettrimPathStarttrimPathEnd的偏移量0f->1f;

例如:

XML布局如下:

"http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"> android:name="triangle"
android:pathData="M3,18 h18 v-5 L3,13 v5 z "
android:strokeWidth="1"
android:strokeLineJoin="round"
android:strokeAlpha="0.9"
android:strokeColor="@color/white"
android:trimPathStart="0.1"
android:strokeLineCap="round"
android:trimPathOffset="0.15"
/>
复制代码

PreView效果如下:

group标签

group标签主要是将多个path标签组合起来,子标签也可以是group标签,其属性作用于所有子标签,有以下属性:

  • name: 定义group标签名称;
  • translateX: x轴位移;
  • translateY: y轴位移;
  • rotation: 旋转;
  • scaleX: x轴缩放;
  • scaleY: y轴缩放;
  • pivotX: 缩放与旋转参考点X;
  • pivotY: 缩放与旋转参考点y;栗子:

XML布局代码:

"http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"> android:name="triangleGroup"
android:rotation="10"
android:translateX="1"
android:translateY="1"
android:scaleX="0.5f"
android:scaleY="0.5f"
android:pivotX="0.5"
android:pivotY="0.5"> android:fillColor="@color/colorPrimary"
android:pathData="M6,6 L9,12 H3 z"
android:strokeWidth="0.5"
android:strokeColor="@color/white"
android:strokeLineJoin="round" /> android:fillColor="@color/chart_color_1"
android:pathData="M18,6 L21,12 H15 z"
android:strokeWidth="0.5"
android:strokeColor="@color/white"
android:strokeLineJoin="bevel" />
复制代码

效果图:

矢量图动画

只要胆子大,没有实现不了的矢量图,加上点动画效果那就更炫酷吊了。属性动画了解多少呢?好文链接==>:Android属性动画,看完这篇够用了吧

「矢量图动画步骤」

  1. 实现矢量图
  2. 实现属性动画
  3. 实现矢量属性动画粘合剂
  4. 布局引用,代码开始动画
轨迹动画

轨迹动画主要利用属性动画设置矢量图的trimPathStarttrimPahtEnd属性。要正确实现轨迹动画的前提条件:矢量图是一笔画出的,不能存在多次挪画笔的操作。

示例:

  1. 在drawable文件夹下创建vector_text.xml文件:
<?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:width="240dp"
android:height="240dp"
android:viewportWidth="24"
android:viewportHeight="24"> android:name="pathText"
android:pathData="M9,6 L12,12 L15,6.5 18,12 21,6"
android:strokeWidth="0.5"
android:strokeColor="@color/white"
android:strokeLineCap="round"
android:strokeLineJoin="round" />
复制代码

画了一个白色的W:

2. 在animator文件夹下创建animator_text.xml文件。定义一个属性动画,操作矢量图的trimPathEnd属性。

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">

android:duration="2000"
android:propertyName="trimPathEnd"
android:valueFrom="0"
android:valueTo="1"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:valueType="floatType" />
//这里顺便操作矢量图的画笔颜色
android:duration="2000"
android:propertyName="strokeColor"
android:valueFrom="@color/white"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:valueTo="@android:color/holo_blue_dark"
android:valueType="colorType" />

set>
复制代码
  1. drawable文件夹下创建animator_vector_text.xml文件,组合矢量图和属性动画,成为它两的粘合剂。由于兼容问题,需要在drawable-v21文件夹下创建。
"http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vector_text"> android:name="pathText"
android:animation="@animator/animator_text" />
复制代码

animated-vector标签的drawable属性值是第一步定义的矢量图文件名。target标签的name属性是我们在矢量图中定义的;而animation属性则是第二部定义的属性动画文件名。

  1. 在布局在引用animator_vector_text文件
            android:id="@+id/iv"
app:srcCompat="@drawable/vector_animator_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
复制代码
  1. 在代码中开始动画:
    val animatable = iv.drawable as Animatable
animatable.start()
复制代码

「效果图:」

路径动画

路径动画是利用矢量图中相同的关键点进行变幻的过程。

Android 5.0前不支持路径动画。

示例:

  1. 在drawable文件夹下创建vector_line.xml文件:
<?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:width="240dp"
android:height="240dp"
android:viewportWidth="24"
android:viewportHeight="24"> android:name="pathLine"
android:pathData="M9,6 L12,6 L15,6 18,6 21,6"
android:strokeWidth="0.5"
android:strokeColor="@color/white"
android:strokeLineCap="round"
android:strokeLineJoin="round" />
复制代码

我们定义了几个关键点,画了一条直线:

2. 在animator文件夹下创建animator_morphing.xml文件。定义一个属性动画,操作矢量图的pathData属性。valueFrom是第一步创建直线矢量图的属性pathData的值,valueToW矢量图的pathData的值。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
android:duration="2000"
android:propertyName="pathData"
android:valueFrom="M9,6 L12,6 L15,6 18,6 21,6"
android:valueTo="M9,6 L12,12 L15,6.5 18,12 21,6"
android:valueType="pathType" />
set>
复制代码
  1. drawable文件夹下创建animator_vector_line.xml文件,组合矢量图和属性动画,成为它两的粘合剂。由于兼容问题,需要在drawable-v21文件夹下创建。
<?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vector_line"> android:name="pathLine"
android:animation="@animator/animator_morphing" />
复制代码
  1. 在布局在引用animator_vector_text文件
            android:id="@+id/iv"
app:srcCompat="@drawable/vector_animator_line"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
复制代码
  1. 在代码中开始动画效果:
    val animatable = iv.drawable as Animatable
animatable.start()
复制代码

「效果图」

到这里就结束了,下面是属于大家的小黄车~

大家的小黄车

实例demo演示,用了2小时给大家制作的小黄车,希望不要嫌弃:640?wx_fmt=gif

  1. 在drawable文件夹下创建vertor_bicycle.xml文件夹,内容如下:
<?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:width="300dp"
android:height="300dp"
android:viewportWidth="200"
android:viewportHeight="200"> android:name="leftWheel"
android:pivotX="40"
android:pivotY="70"> android:name="leftWheelAxle"
android:pathData="M 40,70 L23,80M 40,70 L40,50 M 40,70 L57,80"
android:strokeWidth="1"
android:strokeColor="@color/white" /> android:pathData="M 60,70 A 20,20,0,1,1,60,69 z"
android:strokeWidth="1"
android:strokeColor="@color/white" /> android:name="rightWheel"
android:pivotX="130"
android:pivotY="70"> android:name="rightWheelAxle"
android:pathData="M 130,70 L113,80 M 130,70 L130,50 M 130,70 L147,80"
android:strokeWidth="1"
android:strokeColor="@color/white" /> android:pathData="M 150,70 A 20,20,0,1,1,150,69 z"
android:strokeWidth="1"
android:strokeColor="@color/white" /> android:name="chainBox"
android:fillColor="@color/colorPrimary"
android:pathData="M 35,62 h54 v12 H35 z"
android:strokeWidth="1"
android:strokeColor="@color/white" /> android:pathData="M 50,69 L 65,40 L 80,69 M 86,65 L110,31
v 20 L130,70 "
android:strokeWidth="2"
android:strokeColor="@color/colorPrimary"
android:strokeLineJoin="round" /> android:pathData="M105,73 A 20,20,0,1,1,125,85"
android:strokeWidth="2"
android:strokeColor="@color/colorPrimary"
android:strokeLineJoin="round"
android:trimPathEnd="0.4" /> android:pathData="M 110,31 V20 l -10,-4 h -3 M110,21 l -4,-10 h-3"
android:strokeWidth="2"
android:strokeColor="@color/white"
android:strokeLineCap="round"
android:strokeLineJoin="round" /> android:fillColor="@color/white"
android:pathData="M 111,41 h 21 v -12 H 111 z"
android:strokeWidth="1"
android:strokeColor="@color/white"
android:strokeLineCap="square"
android:strokeLineJoin="round" /> android:fillColor="@color/colorPrimary"
android:pathData="M 121,30 L122,31 L121,32 L120,31 z"
android:strokeWidth="0.5"
android:strokeColor="@color/colorPrimary"
android:strokeLineCap="square"
android:strokeLineJoin="miter" /> android:pathData=" M119,33 L121,35,L123,33
M118,34 L121,37,L124,34"
android:strokeWidth="0.5"
android:strokeColor="@color/colorPrimary"
android:strokeLineCap="square"
android:strokeLineJoin="miter" /> android:fillColor="@color/white"
android:pathData="M 55,40 h 20 v-4 H56 v-3h-2"
android:strokeWidth="1"
android:strokeColor="@color/white"
android:strokeLineCap="square"
android:strokeLineJoin="round" /> android:name="pedal"
android:pivotX="82"
android:pivotY="68"> android:pathData="M 82,68 L 98,80"
android:strokeWidth="0.5"
android:strokeColor="@color/white"
android:strokeLineCap="round" /> android:fillColor="@color/white"
android:pathData="M 96,80 A 2,2,0,1,1,96,81 z"
android:strokeWidth="1"
android:strokeColor="@color/white" />
复制代码

「预览图:」

640?wx_fmt=other2. 在animator文件夹下创建animator_wheel.xml文件,实现车轮和脚踏旋转属性动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
android:interpolator="@android:interpolator/linear"
android:propertyName="rotation"
android:repeatCount="infinite"
android:valueType="floatType"
android:valueFrom="0f"
android:valueTo="360f"
android:repeatMode="restart"
android:duration="2000"/>
//可以再增加其他动画效果,例如颜色变化
set>
复制代码
  1. animator文件夹下创建animator_bicycle_left_to_right.xml布局文件,实现单车从左到右移动属性动画:
"http://schemas.android.com/apk/res/android"
android:propertyName="translationX"
android:valueFrom="-600f"
android:valueTo="900f"
android:valueType="floatType"
android:repeatCount="infinite"
android:repeatMode="restart"
android:duration="9000"
android:interpolator="@android:interpolator/linear"
/>复制代码
  1. drawable文件夹下创建verctor_animator_bicycle.xml文件,实现单车矢量图和属性动画的粘合剂,也就是最终的矢量图动画。由于兼容问题,需要在drawable-v21文件夹下创建。
<?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vertor_bicycle"> android:animation="@animator/animator_wheel"
android:name="leftWheel"/> android:animation="@animator/animator_wheel"
android:name="rightWheel"/> android:animation="@animator/animator_wheel"
android:name="pedal"/>
复制代码
  1. 在布局中引用verctor_animator_bicycle.xml文件,
<?xml version="1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/black"> android:id="@+id/iv"
app:srcCompat="@drawable/verctor_animator_bicycle"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:background="@color/white"
android:padding="10dp"
android:text="开始"
android:textColor="@color/colorPrimary"
android:textSize="16sp" />
复制代码
  1. AppCompatActivity中代码调用:
    btnStart.setOnClickListener {
val animatable = iv.drawable as Animatable
animatable.start()
}
复制代码

此时效果图:640?wx_fmt=gif7. 加上从左到右的属性动画:

    btnStart.setOnClickListener {
val animatable = iv.drawable as Animatable
animatable.start()

val animator = AnimatorInflater.loadAnimator(this, R.animator.animator_bicycle_left_to_right)
animator.setTarget(iv)
animator.start()
}
复制代码

效果图:

640?wx_fmt=gif好了哇,大家的小黄车已经造好,请给文章点个赞领取,如果不满意,可以自行定制小黄车哦~

「要啥自行车,想要个赞而已」

参考文章:

Android属性动画,看完这篇够用了吧

Android过渡动画,让APP更富有生机

官网

Android 5.0+ 高级动画开发系列 矢量图动画

Android Vector曲折的兼容之路

---END---

推荐阅读:
FFmpeg获取视频首帧转封面图Bitmap
公众号又出新功能,微信版“热搜”要来了?
Android v1、v2、v3签名详解
还分不清 Cookie、Session、Token、JWT?
牛逼!国外程序员小哥靠 GitHub 打赏年入 70 万,网友直呼羡慕
为什么 HashMap 加载因子是0.75?
10M/S!百度网盘偷偷更新,终于实现免费不限速了!
Android 实现 图片 转 字符画 效果
面试官:说说Java中的 volatile 关键字?
2020年最佳恶意软件删除工具Top 10
【译】Flutter | 深入理解布局约束
深入剖析Java中的装箱和拆箱
App为了漂亮脸蛋也要美颜,Theme 与 Style 的使用,附一键变装 demo

640?wx_fmt=png

更文不易,点个“在看”支持一下?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值