使用React Native动画构建车速表-程序员宅基地

技术标签: python  

Animation in mobile app enhances the user experience. It provides a perfect visual hint and makes it appealing to the user.

移动应用中的动画可增强用户体验。 它提供了完美的视觉提示,并吸引了用户。

In this post, we will learn how to implement a speedometer animation using React Native Animated API. We will make use of interpolator and rotate animation.

在本文中,我们将学习如何使用React Native Animated API实现车速表动画。 我们将使用插值器并旋转动画。

Ground Rules

基本规则

Before we begin, let us understand the basic rules that we need to follow while using interpolation. If you are already aware of these, feel free to skip to the next section

在开始之前,让我们了解使用插值时需要遵循的基本规则。 如果您已经知道这些,请随时跳到下一部分

  • The values in inputRange need not necessarily start at 0. inputRange: [-1 0 , 1] is an valid range.

    inputRange中的值不一定必须从0开始。inputRange:[-1 0,1]是有效范围。
  • The values in the inputRange always are in increasing order. Otherwise, you will get an error as “Invariant Violation: Input Range must be monotonically non-decreasing

    inputRange中的值始终按升序排列。 否则,您将收到错误消息“ Invariant Violation:输入范围必须单调非递减

  • Values in outputRange can be in any order unlike values in inputRange. But inputRange and outputRange must be of the same length. Following is the snippet from “react-native” Animated API that validates the same.

    与inputRange中的值不同,outputRange中的值可以采用任何顺序。 但是inputRange和outputRange必须具有相同的长度。 以下是“ react-native” Animated API的代码片段,用于验证相同内容。
  • While interpolating colors, the values must be in RGB/A or HSL color. Hex is not supported. So if you want to animate a color you’ll need to convert your hex to one of the formats interpolation allows.

    插值颜色时,值必须为RGB / A或HSL颜色。 不支持十六进制。 因此,如果要设置颜色的动画,则需要将十六进制转换为插值允许的格式之一。

Interpolation

插补

As per docs, an interpolation maps input ranges to output ranges, typically using a linear interpolation but also supports easing functions.

根据文档,插值通常使用线性插值将输入范围映射到输出范围,但还支持缓动功能。

The inputRange looks at the Animated.Value(val) and then determines how it should be interpreted to the output range.Here are the inputRange and outputRange we will be using

inputRange会查看Animated.Value(val)并确定如何将其解释为输出范围,这是我们将要使用的inputRange和outputRange

inputRange  : [ -3, -2, -1, 1, 2, 3]  
outputRange : ['-100deg','-60deg','-20deg', '20deg', '60deg', '100deg']

For every value in inputRange there should be an equivalent value in outputRange.

对于inputRange中的每个值,在outputRange中应该有一个等效值。

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
nput Value and its equivalent output value 输入值及其等效输出值

Interpolation estimates unknown values that fall between known values which we pass as an inputRange.

插值法估计的未知值落在我们作为输入范围传递的已知值之间。

Let say if we start our animation with value 0 even if it does not exist in the inputRange that we have provided, interpolation algorithm will use the known values from inputRange and calculate the rotation angle at which out Indicator svg should be at, (0 deg) as shown below:

假设如果我们从提供的inputRange中不存在值0开始动画,则插值算法将使用inputRange中的已知值并计算指标svg应位于的旋转角度(0度) 如下所示:

Image for post

SVGWe will be using two SVG images, one for “Speedometer” and the other one as “Indicator” on which we will perform rotate animations. Since the “Indicator” SVG we are using is upright 90deg, our initial angle is negative(-ve). The SVG you are using will determine the interpolation rotation angles.

SVG我们将使用两张SVG图像,一个用于“ Speedometer”,另一个用于“ Indicator”,我们将在该图像上执行旋转动画。 由于我们使用的“指示器” SVG为直角90度,因此我们的初始角度为负(-ve)。 您使用的SVG将确定插值旋转角度。

SVG in React Native React Native doesn’t support SVG out of the box. We will use react-native-svg which provides SVG support on React Native. To use SVG images as any other UI components like Button, Text, etc, we will use SVGR tool.

SVG在阵营本地阵营本地不支持SVG开箱。 我们将使用react-native-svg在React Native上提供SVG支持。 要将SVG图像用作按钮,文本等其他UI组件,我们将使用SVGR工具。

CodeWe initialized Animated.Value() with -3, this means the initial rotation angle at which our “Indicator” is placed will be -100deg and it will be our starting position.

代码我们使用-3初始化了Animated.Value(),这意味着放置“指示器”的初始旋转角度为-100度,这是我们的起始位置。

state = { value : new Animated.Value(-3)};

Interpolation will start with the initial value provided above

插值将从上面提供的初始值开始

const animInterpolation = this.state.value.interpolate({       inputRange : [ -3, -2, -1, 1, 2, 3],    
outputRange: ['-100deg','-60deg','-20deg', '20deg', '60deg', '100deg']});

Pass the interpolation to rotate

通过插值旋转

<Animated.View style={
        {
        transform:[{
        rotate:animInterpolation}],position:"absolute", top:40, }}>
<Indicator/>
</Animated.View>

Along with this, I am also interpolating the background colors of a container view. This is totally optional

与此同时,我也在插入容器视图的背景色。 这是完全可选的

const colorInterpolation = this.state.value.interpolate({       inputRange: [ -3, -2, -1, 1, 2, 3],   outputRange : ["rgba(255,255,255, 0.9)", "rgba(68, 196, 161, 1)", "rgba(228, 233, 237, 1)", "rgba(52, 152, 219, 1)","rgba(250, 216,   89, 1)", "rgba(192, 57, 43, 1)"]
});

Setting the interpolation on backgroundColor of the container view.

在容器视图的backgroundColor上设置插值。

<Animated.View style={[{ backgroundColor:colorInterpolation}, {width:"100%", height:"100%",justifyContent:"center", alignItems:"center"}]}>

Final Result

最后结果

Image for post

Github Project

Github项目

翻译自: https://medium.com/@pallavi.mp88/speedometer-react-native-animation-7e5c5e22792b

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_26638123/article/details/108170698

智能推荐

Python 入门的60个基础练习_练习python基础语法-程序员宅基地

文章浏览阅读4.2w次,点赞329次,收藏2.7k次。Python 入门的60个基础练习_练习python基础语法

iOS6和iOS7代码的适配(2)——status bar_ios7 statusbar-程序员宅基地

文章浏览阅读1w次。用Xcode5运行一下应用,第一个看到的就是status bar的变化。在iOS6中,status bar是系统在处理,应用_ios7 statusbar

gdb调试时No symbol "var" defined in current context && No Register_no registers调试显示-程序员宅基地

文章浏览阅读2.1k次。问题描述:,在gdb调试程序输出变量:p var,会提示No symbol "var" in current context.原因:程序编译时开启了优化选项,那么在用GDB调试被优化过的程序时,可能会发生某些变量不能访问,或是取值错误码的情况。这个是很正常的,因为优化程序会删改程序,整理程序的语句顺序,剔除一些无意义的变量等,所以在GDB调试这种程序时,运行时的指令和你所编写指_no registers调试显示

IDGeneratorUtil 主键id生成工具类_idgeneratorutils.generateid()-程序员宅基地

文章浏览阅读3.4k次。import java.util.Random;import org.drools.util.UUIDGenerator;/** * * * 类名称:GenerateIdUtil * 类描述: 主键生成工具类 * @author chenly * 创建时间:Jul 10, 2012 8:10:43 AM * 修改人: * 修改时间:Jul 10, 2012 8..._idgeneratorutils.generateid()

关于汇编 BX 和 BLX 跳转指令_汇编blx-程序员宅基地

文章浏览阅读5k次。BX:跳转到寄存器reg给出的目的地址处,如:BX R2BLX:跳转到寄存区reg给出的目的地址处并将返回地址存储到LR(R14)使用这两个指令时有一点特别需要注意:跳转的目的地址必须是奇数,若不是奇数则在后面加1,如某函数的起始地址是0x80000f00,则要跳转到此函数则应该跳转到0x80000f01处!否则会进入硬件错误中断!..._汇编blx

前端vue,打包整合进后端springboot的resources里面后,运行只要刷新就报404_前端项目放入resource-程序员宅基地

文章浏览阅读2.6k次,点赞2次,收藏4次。vue打包后,其实就剩index.html和一堆静态资源,页面的加载和替换都是通过刷新index.html种的dom来实现的(应该是这样,可能表述不是很好),所以做个重定向就可以了。(博主是这么解决的,网上还有很多人是各种路径错误,大家可以尝试下自己是哪个原因)import org.springframework.boot.web.server.ConfigurableWebServerFa..._前端项目放入resource

随便推点

添加远程github仓库时报错 Warning: Permanently added the RSA host key for IP address 52.74.223.119_cmd warning: permanently added-程序员宅基地

文章浏览阅读9.7k次。1.问题展示2.解决方案1.任意窗口, 打开git bash2.命令行界面, 输入cd C:3.cat ~/.ssh/id_rsa.pub正常下面应该显示一大串公钥如果没有,显示如下图, 则进行下一步, 创建公钥4.创建公钥, 输入 ssh-keygen5.然后一直下一步, 直到出现6.再次输入cat ~/.ssh/id_rsa.pub下面一大串数字便是公钥,复制这些字符串, 打开github, 点击头像, 打开settings, 打开SSH and GPG Keys_cmd warning: permanently added

SQL*Plus 使用技巧1-程序员宅基地

文章浏览阅读154次。[code="java"]1. SQL/Plus 常用命令 a. help [topic] 查看命令的使用方法,topic表示需要查看的命令名称。 如: help desc; b. host 该命令可以从SQL*Plus环境切换到操作系统环境,以便执行操作系统命名。 c. host [command] 在sql*plus环境中执行操作系统命令,如:host notepad.exe..._sql+plus的使用方法

域控服务器搭建与管理论文,校园网络服务器的配置与管理 毕业论文.doc-程序员宅基地

文章浏览阅读441次。该文档均来自互联网,如果侵犯了您的个人权益,请联系我们将立即删除!**学校毕 业 论 文**学校园网络服务器的配置与管理姓 名: **学 号: **指导老师:系 名:专 业: 计算机网络技术班 级:二0一一年十二月十五日摘 要随着网络技术的不断发展和Internet的日益普及,许多学校都建立了校园网络并投入使用,这无疑对加快信息处理,提高工作效..._服务器配置与应用论文

mysql单实例多库与多实例单库_数据库单实例和多实例-程序员宅基地

文章浏览阅读1k次。一、单实例多库:一个mysql实例,创建多个数据目录。规划:实例路径:/usr/local/mysql数据目录路径:(1)/usr/local/mysql/data(2)/usr/local/mysql/data2步骤:安装mysql。配置my.cnf文件。初始化各个数据库。用mysqld_multi启动。1、安装mysql。平常安装。2、m..._数据库单实例和多实例

MFC解决找不到MFC90.DLL的问题_microsoft v90.debugmfc-程序员宅基地

文章浏览阅读6.3k次。今天装了第三方的MFC软件库Xtreme ToolkitPro v15.0.1,听说搞MFC的人都知道它的强大,我刚学习,所以装了一个,然后想运行一下它自带的例子看看。出现一个“找不到mfc90.dll“的问题,百度一下,记录如下:vs2008已经打过sp1补丁,编译C++程序会提示找不到mfc90.dll文件的错误,但是如果是release版的话就能正常运行csdn看到解决方案,粘贴_microsoft v90.debugmfc

XeLaTeX-中文排版解决方案_latex 中文排版 texlive-程序员宅基地

文章浏览阅读2.1k次。以前使用CJK进行中文的排版,需要自己生成字体库,近日,出现了XeTeX,可以比较好的解决中文字体问题,不需要额外生成LaTeX字体库,直接使用计算机系统里的字体,本文以在Linux下为例说明XeTeX的使用。操作系统: UbuntuTeX:除了texlive包外,还需要安装的包是texlive-xetex。字体:可以使用fc-list查看你自己的字体库,注意字体的完整名称,在XeTe..._latex 中文排版 texlive