简体   繁体   English

在多个 Animated.Values 上使用插值有什么影响?

[英]What are the implications of using interpolation over multiple Animated.Values?

I have multiple animations running in parallel and I wondered if there are any benefits to having 3 Animated.Values over having a single Animated.Value and using interpolation.我有多个并行运行的动画,我想知道拥有 3 个 Animated.Value 是否比拥有一个 Animated.Value 和使用插值有什么好处。

Consider the following approach with multiple values:考虑以下具有多个值的方法:

const opacityValue = useRef(new Animated.Value(0)).current;
const rotationValue = useRef(new Animated.Value(0)).current;
const scaleValue = useRef(new Animated.Value(0)).current;

useEffect(() => {
  Animated.parallel(
    Animated.timing(opacityValue, ...),
    Animated.timing(rotationValue, ...),
    Animated.timing(scaleValue, ...),
  ).start();
}, []);

const rotate = rotationValue.interpolate([]);

Consider this same approach with a single Animated.Value and interpolation included:考虑使用单个 Animated.Value 和插值的相同方法:

const opacityValue = useRef(new Animated.Value(0)).current;

useEffect(() => {
  Animated.timing(opacityValue, ...).start();
}, []);

const rotationValue = opacityValue.interpolate([]);
const scaleValue = opacityValue.interpolate([]);

With the latter approach, only one animation is running on the native thread so could one assume that it is more performant?对于后一种方法,只有一个 animation 在本机线程上运行,所以可以假设它的性能更高吗?

What are the implications of using one over the other?使用其中一个有什么影响?

And is there any documentation that objectively describes in which situations one is better than the other?是否有任何文档可以客观地描述在哪些情况下一种比另一种更好?

Disclaimer: I was interested in this and interested in checking out React Native, so I dived into React Native and its documentation and source code to see how it works and to understand you question.免责声明:我对此感兴趣并有兴趣查看 React Native,因此我深入研究了 React Native 及其文档和源代码,以了解它是如何工作的并理解您的问题。 I haven't ever used React Native before stumbling upon this question.在偶然发现这个问题之前,我从未使用过 React Native。 I do have decent background when it comes to interpolation, React and JS in general.一般来说,我在插值、React 和 JS 方面确实有不错的背景。

So first I thought that the .interpolate() actually drives the animations and that the interpolate call actually interpolated the given value over time, but this is actually not true.所以首先我认为.interpolate()实际上驱动动画,并且 interpolate 调用实际上会随着时间的推移对给定值进行插值,但这实际上不是真的。

The animations, specifically the updates to some value over time are driven by the Animated.timing() functions or the Animated.parallel function, when start() is called on them.动画,特别是随着时间的推移对某个值的更新是由Animated.timing()函数或Animated.parallel function 驱动的,当它们调用start()时。

Animated.timing() function takes an Animated.TimingAnimationConfig parameter in where, for example, easing function of the animation is specified. Animated.timing() function 采用Animated.TimingAnimationConfig参数,例如,指定了 animation 的缓动function。

As for the .interpolate() function, it is just like any interpolation function such as this:至于.interpolate() function,它就像任何插值 function 一样,例如:

// v0 = start value, 
// v1 = end value, 
// t = "progress" between 0 and 1 from v0 to t1
function lerp(v0, v1, t) {
    return v0*(1-t)+v1*t;
}

But it is a bit more advanced than that as it can take multiple inputRanges and outputRanges , and supports various different interpolation (or easing) functions than just linear.但它比这更先进一点,因为它可以接受多个inputRangesoutputRanges ,并且支持各种不同的插值(或缓动)函数,而不仅仅是线性。 More about it here .更多关于它的信息

But even so, it still acts as a function that just runs given numbers through some specified easing function and returns another number.但即便如此,它仍然充当 function,它只是通过一些指定的缓动 function 运行给定的数字并返回另一个数字。

So back to your question, which I now understand better.所以回到你的问题,我现在更好地理解了。 What you are asking is whether it's better to have three parallel Animated.timing functions running as an Animated.CompositeAnimation than to have only one, "base" Animated.timing function running and have the two other values interpolate their values from that.您要问的是,让三个并行的Animated.timing函数作为Animated.CompositeAnimation运行是否比只运行一个“基本” Animated.timing function 并让其他两个值从中插值更好。

Since Animated.timing is something that actually, dynamically does something over time, there should probably be requestAnimationFrame usage somewhere (for web at least, but there must be something similar for native driver as well).由于Animated.timing实际上是随着时间的推移动态地做某事的东西,因此可能应该在某处使用requestAnimationFrame (至少对于 web,但对于本机驱动程序也必须有类似的东西)。 And look at that, there is !看看那个,

So this proves that Animated.timing has to do with actual, real time repaints over time.所以这证明了Animated.timing与实际的实时重绘有关。 But are there any obvious optimizations, if multiple animations run in parallel as as Animated.CompositeAnimation ?但是,如果多个动画像Animated.CompositeAnimation一样并行运行,是否有任何明显的优化? As far as I could find and look at the code, I would say no, the animations are simply put into an array and pretty much handled normally, so no advanced batching etc. that I could find:就我能找到并查看代码而言,我会说不,动画只是简单地放入一个数组中并且几乎可以正常处理,因此我找不到高级批处理等:

But each animation will call requestAnimationFrame separately.但是每个 animation 都会分别调用requestAnimationFrame There is a discussion about the performance of multiple requestAnimationFrame calls here on Stackoverflow . Stackoverflow 上有关于多次requestAnimationFrame调用的性能的讨论。 Bottom line being: Not much of a difference.底线是:差别不大。

One more thing.还有一件事。 Each animation has it's own onUpdate method and if we look here for example, we can see that that line is pretty much equivalent to performing easing/interpolation, so each animation does interpolation on every update to figure out what it's value should be for that update.每个 animation 都有自己的onUpdate方法,例如,如果我们看这里,我们可以看到该行几乎等同于执行缓动/插值,因此每个 animation 都会在每次更新时进行插值,以确定该更新的值应该是什么.

So to conclude all of this:因此,总结所有这些:

  • (Parallel) animations all call requestAnimationFrame, but the effect on performance is negligible (并行)动画都调用requestAnimationFrame,但对性能的影响可以忽略不计
  • (Parallel) animations each perform their own easing/interpolation calculations internally on update (并行)动画在更新时在内部执行自己的缓动/插值计算
  • The interpolate function doesn't do anything significant, it just interpolates values like any interpolation function would插值 function 没有做任何重要的事情,它只是像任何插值 function 一样插值

And therefore the final answer is:因此最终的答案是:

No, there isn't any significant difference in performance.不,性能没有任何显着差异。 Animated.timing objects most likely will use more memory than if you interpolate values on the fly.与动态插值相比, Animated.timing对象很可能会使用更多的 memory。 Neither the animations nor the interpolation function do any kind of memoization either.动画和插值 function 也不做任何类型的记忆。

As for the native driver, I don't have the brain to dig that deep, but you should probably use parallel timings in that case, as the animations are offloaded from JS (and the animations will most likely do their internal interpolation offloaded as well), but even so it probably doesn't make much of a difference.至于本机驱动程序,我没有足够的头脑去挖掘那么深,但在这种情况下你可能应该使用并行计时,因为动画是从 JS 卸载的(并且动画很可能也会卸载它们的内部插值) ),但即便如此,它也可能没有太大的区别。

If you really care about performance, you might get a decent increase by utilizing WebAssembly to implement the interpolation function and using that instead of what React Native supplies.如果你真的关心性能,你可能会通过使用WebAssembly来实现插值 function 并使用它而不是 React Native 提供的东西来获得可观的提升。

Here is my codesandbox that I used to test some things.这是我用来测试一些东西的 代码框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native - 如何比较 shouldComponentUpdate 中的两个 Animated.Values? - React Native - How to compare two Animated.Values in shouldComponentUpdate? 使用多个交互式弹出窗口的含义 - Implications of using multiple interactive popup windows 使用像angularjs这样的框架有什么可访问性含义? - What are the accessibility implications of using a framework like angularjs? 使用脚本async属性有什么含义? - What are the implications of using the script async attribute? 将CORS与AWS API Gateway结合使用会带来什么安全隐患? - What are the security implications of using CORS with AWS API Gateway? getElementsByTagName(“*”)的性能影响是什么? - What are the performance implications of getElementsByTagName(“*”)? 使用`jsgif`在动画GIF上构建图层 - Using `jsgif` to build layers over animated GIFs 什么是使用.filter()过滤多个值的方法 - What is a way to filter multiple values using .filter() node.js 的含义是什么? - What are the implications of node.js? 使用 JavaScript 在多个 HTML 输入字段值之间进行动态线性插值 - Dynamic linear interpolation between multiple HTML input field values with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM