如何优雅的展示动态图标lottie-react-native

2023-11-06 21:20

本文主要是介绍如何优雅的展示动态图标lottie-react-native,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

如何优雅的展示动态图标lottie-react-native

    • 如何优雅的展示动态图标lottie-react-native
        • 一、Lottie是什么?
        • 二、Lottie能实现什么效果?
        • 三、集成过程
          • 1.安装拉取第三方包:
          • 2.获取json文件
          • 3.组件库的使用
            • Component API:
            • Methods (Imperative API):
        • 四、集成效果图
        • 五、可能遇到的问题
          • 1.Undefined symbol: protocol xxx

项目中,需要使用到动态图标,发现又好用的库,在这里分享一下,需要使用的工具库为lottie-react-native,通过导出AE软件中的动画特效,以json文件的方式导出,来实现动画的效果。

一、Lottie是什么?

Lottie是Airbnb开源的一个支持 Android、iOS 以及 ReactNative,利用Json文件的方式快速实现动画效果的库。简单点说就是Json文件记录动画路径,Android、iOS 以及 ReactNative解析展现出来。

二、Lottie能实现什么效果?

效果



三、集成过程
1.安装拉取第三方包:
yarn add lottie-react-native
yarn add lottie-ios@3.1.3

or

npm i --save lottie-react-native
npm i --save lottie-ios@3.1.3

如果是ios平台,请继续执行下面命令(react-native 版本大于0.60或更高的不需要手动react-native link,而是通过pod install安装依赖):

cd ios && pod install

至此,就完成了第三方包的引入工作。
⚠️:Android平台不需要特殊处理,除非报错崩溃了,请检查以下位置:
a.路径android/app/src/main/java/<AppName>/MainApplication.java

在顶部导入,添加

import com.airbnb.android.react.lottie.LottiePackage;

在 List getPackages();中,添加

packages.add(new LottiePackage()); 

b.路径android/app/build.gradle,dependencies依赖块添加

implementation project(':lottie-react-native') 

c.路径android/settings.gradle添加

include ':lottie-react-native'
project(':lottie-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/lottie-react-native/src/android')
2.获取json文件

这个使用的json文件,是由Adobe After Effects这个软件生成,然后通过其插件bodymovin 生成,同时这里有安装教程,具体导出过程烦请请教UI设计,他们可能会更熟悉。如果实在找不到了,就使用这里的json文件->文件地址

3.组件库的使用

假设我们已经获取到这个animation.json文件了,如下所示最基础的使用方法:

import React from 'react';
import LottieView from 'lottie-react-native';export default class BasicExample extends React.Component {render() {return <LottieView source={require('./animation.json')} autoPlay loop />;}
}

当然,还有其他接口提供使用

import React from 'react';
import LottieView from 'lottie-react-native';export default class BasicExample extends React.Component {componentDidMount() {// 手动控制播放this.animation.play();// 设置特定的开始时间和结束时间this.animation.play(30, 120);}render() {return (<LottieViewref={animation => {this.animation = animation;}}source={require('../path/to/animation.json')}/>);}
}

结合react-native组件Animated实现控制动画播放的过程

import React from 'react';
import { Animated, Easing } from 'react-native';
import LottieView from 'lottie-react-native';export default class BasicExample extends React.Component {constructor(props) {super(props);this.state = {progress: new Animated.Value(0),};}componentDidMount() {Animated.timing(this.state.progress, {toValue: 1,duration: 5000,easing: Easing.linear,}).start();}render() {return (<LottieView source={require('../path/to/animation.json')} progress={this.state.progress} />);}
}

还能够指定的更改图层的颜色

import React from 'react';
import LottieView from 'lottie-react-native';export default class BasicExample extends React.Component {render() {return (<LottieViewsource={require('../path/to/animation.json')}colorFilters={[{keypath: "button",color: "#F00000"},{keypath: "Sending Loader",color: "#F00000"}]}autoPlayloop/>);}
}
Component API:
PropDescriptionDefault
sourceMandatory - The source of animation. Can be referenced as a local asset by a string, or remotely with an object with a uri property, or it can be an actual JS object of an animation, obtained (for example) with something like require('../path/to/animation.json')None
progressA number between 0 and 1, or an Animated number between 0 and 1. This number represents the normalized progress of the animation. If you update this prop, the animation will correspondingly update to the frame at that progress value. This prop is not required if you are using the imperative API.0
speedThe speed the animation will progress. Sending a negative value will reverse the animation1
durationThe duration of the animation in ms. Takes precedence over speed when set. This only works when source is an actual JS object of an animation.undefined
loopA boolean flag indicating whether or not the animation should loop.true
autoPlayA boolean flag indicating whether or not the animation should start automatically when mounted. This only affects the imperative API.false
autoSizeA boolean flag indicating whether or not the animation should size itself automatically according to the width in the animation’s JSON. This only works when source is an actual JS object of an animation.false
styleStyle attributes for the view, as expected in a standard View, aside from border stylingNone
imageAssetsFolderNeeded for Android to work properly with assets, iOS will ignore it.None
onAnimationFinishA callback function which will be called when animation is finished. Note that this callback will be called only when loop is set to false.None
Methods (Imperative API):
MethodDescription
playPlay the animation all the way through, at the speed specified as a prop. It can also play a section of the animation when called as play(startFrame, endFrame).
resetReset the animation back to 0 progress.

更多的接口可以参见:接口参见

四、集成效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、可能遇到的问题
1.Undefined symbol: protocol xxx

在这里插入图片描述
解决方案:
1.在Xcode中打开ios / YourAppName.xcodeproj
2.在左侧的项目浏览器中,右键单击您的应用程序名称,然后单击新建文件…。 为项目创建一个空的Swift文件(添加时请确保选择了target是“您的应用名称”)
3.当Xcode询问时,按Create Bridging Header,然后不要删除Swift文件。重新运行您的构建。
参考地址:
1.https://github.com/mxcl/PromiseKit/issues/1059
2.https://stackoverflow.com/questions/57903395/about-100-error-in-xcode-undefined-symbols-for-architecture-x86-64-upgraded-re
3.Android开发之Lottie动画解析库


在这里插入图片描述

这篇关于如何优雅的展示动态图标lottie-react-native的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/359120

相关文章

Python使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...