优美的开源动效库:Lottie

QuibblerQuibbler 2020-02-11 约 7 分钟 4286 次阅读

优美的开源动效库:Lottie

1、强大的动效Lottie

Lottie是一个适用于Android,iOS,Web和Windows的库,它可以使用Bodymovin解析以json格式导出的Adobe After Effects动画,并在移动设备和Web上原生渲染它们!

GitHub:https://github.com/airbnb/lottie-android

Lottie官网:http://airbnb.io/lottie/

2、引入Lottie

在Android工程中官方唯一推荐的方式在build.gradle中添加依赖即可:

dependencies {
        ...
        implementation 'com.airbnb.android:lottie:3.2.2'
        ...
    }

3、使用Lottie

不急着知道Lottie的原理,简单举个例子先了解Lottie。在布局中添加LottieAnimationView控件:

<com.airbnb.lottie.LottieAnimationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:lottie_autoPlay="true"
        app:lottie_fileName="json/lottie.json"
        app:lottie_loop="true" />

定义了一些View 的属性,参考下面自定义的declare-styleable:

<resources>
    <declare-styleable name="LottieAnimationView">
        <attr format="string" name="lottie_fileName"/>
        <attr format="reference" name="lottie_rawRes"/>
        <attr format="string" name="lottie_url"/>
        <attr format="reference" name="lottie_fallbackRes"/>
        <attr format="boolean" name="lottie_autoPlay"/>
        <attr format="boolean" name="lottie_loop"/>
        <attr format="enum" name="lottie_repeatMode">
            <enum name="restart" value="1"/>
            <enum name="reverse" value="2"/>
        </attr>
        <attr format="integer" name="lottie_repeatCount"/>
        <attr format="string" name="lottie_imageAssetsFolder"/>
        <attr format="float" name="lottie_progress"/>
        <attr format="boolean" name="lottie_enableMergePathsForKitKatAndAbove"/>
        <attr format="color" name="lottie_colorFilter"/>
        <attr format="float" name="lottie_scale"/>
        <attr format="float" name="lottie_speed"/>
        <!-- These values must be kept in sync with the RenderMode enum -->
        <attr format="enum" name="lottie_renderMode">
            <enum name="automatic" value="0"/>
            <enum name="hardware" value="1"/>
            <enum name="software" value="2"/>
        </attr>
    </declare-styleable>
    <item name="lottie_layer_name" type="id"/>
</resources>

4、Lottie可用的资源

        最关键的是添加动画资源,官方给了一下几种方式添加资源,可以从json文件、json字符串、网络流json等等途径给LottieAnimationView设置动画资源

  • A json animation in src/main/res/raw.
  • A json file in src/main/assets.
  • A zip file in src/main/assets. See images docs for more info.
  • A url to a json or zip file.
  • A json string. The source can be from anything including your own network stack.
  • An InputStream to either a json file or a zip file.

5、Lottie资源

从下面的网站可以下载UI设计师分享的各式各样JSON动画:

LottieFiles: https://www.lottiefiles.com/

相关推荐

精选
Airbnb开源项目
开源

Airbnb开源项目

Airbnb Engineering & Data ScienceAirflow Use Apache Airflow (incubating) to author workflows as directed acyclic graphs (DAGs) of tasks12,263Airpal Web UI for PrestoDB2,502Aerosolve A machine learning

1.8k
OkHttp线程池和连接池
开源

OkHttp线程池和连接池

OkHttp线程池和连接池了解了OkHttp的网络请求流程以及拦截器实现原理,再关注OkHttp中两个重要的:OkHttp的线程池和连接池。1、OkHttp线程池在OkHttp网络请求流程一文中,我们分析了OkHttp异步和同步请求流程。请求最后都在Dispatcher中分发调度处理,最后被ExecutorService执行。1.1、DispatcherDispatcher中执行任务的执行器是ex

5.2k
Glide配置:AppGlideModule
开源

Glide配置:AppGlideModule

Glide配置:AppGlideModuleGlide用起来非常方便,基本用法见Glide的基本用法,简短的一句链式调用就能给ImageView设置图片。而且自带缓存实现,如果不进行额外的设置Glide的缓存目录、缓存大小都是默认的。应用可以自行进行定制修改,就用到AppGlideModule。1、自定义AppGlideModule配置Glide加载图片的设置、缓存设置、缓存策略,就需要应用自己继

3.7k