Android 制作逐渐显示动画(描边动画、矢量动画VectorDrawble)

传统的Android提供的动画只有平移、缩放、显示等效果,那么怎么才能实现逐渐绘制动画? 答案是使用矢量图动画。

一、 矢量图支持VectorDrawble

Android 系统从5.0开始支持矢量图,可以通过 Android Studio自带的 Vector Assert 工具将SVG、PSD 转成VectorDrawble。

矢量图兼容5.0以下使用需要v7支持库和以下配置:
compile 'com.android.support:appcompat-v7:23.2.0'
android {
  defaultConfig {
    vectorDrawables.useSupportLibrary = true
  }
}
如下是Vector Assert 工具的使用方法:
image
image
转化成的VectorDrable格式如下:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="root"
    android:height="62dp"
    android:viewportHeight="140.13"
    android:viewportWidth="121.38"
    android:width="53dp">

    <group android:name="all_line">

        <path
            android:name="line1"
            android:pathData="M59.94,3.22
            l-58.9,36.2
            v11
            L59.94,14.22
            l58.9,36.2
            L118.84,39.42
            Z"
            android:strokeColor="@color/splash_logo_stroke"
            android:strokeWidth="1"/>


        <path
            android:name="line2"
            android:pathData="M59.94,24.72
            l58.9,36.2
            v11
            L59.94,35.72
            l-58.9,36.2
            L1.04,60.92
            Z"
            android:strokeColor="@color/splash_logo_stroke"
            android:strokeWidth="1"/>


        <path
            android:name="line3"
            android:pathData="M59.94,45.72
            l-58.9,36.2
            v11
            L59.94,56.72
            l58.9,36.2
            L118.84,81.92
            Z"
            android:strokeColor="@color/splash_logo_stroke"
            android:strokeWidth="1"/>


        <path
            android:name="line4"
            android:pathData="M1.95,103.66
            l7.41,4.54
            L60.09,77.75
            L67.69,81.69
            l-49.15,30.2
            l7.78,4.77
            L77.11,88.07
            l6.83,4.2
            L35.69,123.38
            l7.8,4.78
            l49.5,-30.43
            l6.26,3.85
            L52.55,133.71
            l7.8,4.78
            l58.09,-35.71
            L61.44,66.55
            l-58,35.64
            Z"
            android:strokeColor="@color/splash_logo_stroke"
            android:strokeWidth="1"/>
    </group>


</vector>

它的原图是这张:
image
转化好的VectorDrawble 可以直接设置为ImageView的src属性。

转化好的VectorDrawblew看起来好像挺复杂,是不是完全看不懂。没关系,只要转化的图形没有问题,你不用关心其内部数据。

我们直接进入矢量动画部分。

二、矢量图动画:AnimatedVectorDrawable

矢量动画的实现通过在xml定义<animated-vector>标签(其对应的Java对象是AnimatedVectorDrawable)实现,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/splash_logo_vector">
    <target
        android:name="line1"
        android:animation="@animator/splash_animator_draw"/>

    <target
        android:name="line2"
        android:animation="@animator/splash_animator_draw"/>

    <target
        android:name="line3"
        android:animation="@animator/splash_animator_draw"/>

    <target
        android:name="line4"
        android:animation="@animator/splash_animator_draw"
        />
</animated-vector>

可以看到<animated-vector>标签是主要元素是target,它其实就起到一个桥梁作用,将动画和VectorDraw中的图形联系起来。

比如target1,它将属性动画splash_animator_draw和vectorDrawble对象中name等于line1的对象联系起来,即:动画将生效在line1对象上。

这里给出这个描边的属性动画文件,trimPathEnd代表线段的结束位置,trimPathEnd从0到1,就意味着逐渐描边:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <objectAnimator
        android:duration="1000"
        android:interpolator="@android:interpolator/linear"
        android:propertyName="trimPathEnd"
        android:valueFrom="0"
        android:valueTo="1"
        android:valueType="floatType"
        >
    </objectAnimator>
</set>
必须说明的是,并不是所有的VectorDrawble属性都可以利用属性动画,以下是可以被“动画”的属性集合:
VectorDrawble 成员 可以被“动画”的属性
<vector> alpha
<group> rotation
~ pivotX
~ pivotY
~ scaleX
~ scaleY
~ translateX
~ translateY
<path> pathData
~ fillColor
~ strokeColor
~ strokeWidth
~ strokeAlpha
~ fillAlpha
~ trimPathStart
~ trimPathEnd
~ trimPathOffset
<clip-path> pathData
使用矢量动画
  1. 将animated-vector 这个xml文件当做图片设置在ImageView的src属性上(为了兼容5.0以下,可以使用srcCompat属性):
   <ImageView
        android:id="@+id/iv_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/center_view"
        android:layout_centerHorizontal="true"
        app:srcCompat="@drawable/animated_splash_logo"
        />
  1. 在代码里开启动画:
 Drawable drawable = mIvLogo.getDrawable();
//AnimatedVectorDrawable实现了Animatable接口
 if (drawable instanceof Animatable) {
     ((Animatable) drawable).start();
  }
  1. 运行效果:


    image

三、VectorDrawble进阶

之前展示了由svg图片通过Android Studio Vector Assert工具转换过来的VectorDrawble,里面的实际上svg画图语法,<path>标签里的pathData包含了画图的路径(坐标)和语法

在实际使用过程中有几率遇到这样两个问题(笔者都遇到过):

  • Vector Assert 转换的数据不准确,比如线条有点歪。

  • 动画效果是一个组合效果,Vector Assert工具自动转换的画线条的方式不符合你的动画需求。
    举一个极端的例子:VectorDrawble画了一条线条(只有一个path标签),但是你的动画效果需要的是线条的左边波浪抖动,线条的右边颜色闪烁。
    那么你需要把这一个线条标签,拆成左右两条线段标签,分别应用不同的动画。

其实以上两个问题都要求你掌握进阶知识,svg画图语法。

官方svg语法解析:https://www.w3.org/TR/SVG/paths.html
不喜欢看英文,去这篇博客:http://www.jianshu.com/p/a3cb1e23c2c4

四、另一个实现描边动画的方法:Lottie动画框架

  • Lottie 是 Airbnb 开源的一个动画渲染库,同时支持 Android、iOS、React Native 平台。
  • Lottie 目前只支持渲染播放 After Effects 动画。
  • Lottie 使用从 bodymovin (开源的 After Effects 插件)导出的json数据来作为动画数据

对于Android程序员,Lottie的动画使用很简单,步骤如下:

1.接入Lottie

dependencies {  
   //lottie动画库
    compile 'com.airbnb.android:lottie:2.2.1'
}
  1. 管视觉设计师要一个After Effects软件中通过bodymovin插件导出的动画json文件。

  2. 把动画json文件放入Android的assets文件夹。

  3. 在布局文件里使用Lottie动画控件,设置好lottie_fileName

 <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        app:lottie_autoPlay="true"
        app:lottie_fileName="splash_animation.json"
        app:lottie_loop="false"
        />

  1. OK,运行程序,改动画就自动播放起来了(设置了lottie_autoPlay="true")

总结

从动画实现上来说,Lottie动画应该是最简单的方法了。

Lottie的优点很明显:

  • 程序员减少了工作量,不用管动画实现的细节了。
  • 各端的动画能保持统一,不会因为程序员技术实现而有差异。
  • 动画实现能最大程度按照视觉的原始设计,免去了各端联调。

Lottie的缺点:

  • Lottie对于视觉也是有学习成本的。
  • 增加了视觉设计师的工作。
  • 在实际使用中发现Lottie动画配合属性动画一起使用有卡顿。

好了,如果觉得本文对你有帮助,请关注、留言、点赞我,谢谢!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容