美文网首页
react-native-renimated2 插值interp

react-native-renimated2 插值interp

作者: 朱传武 | 来源:发表于2021-11-12 21:51 被阅读0次

今天用了一天的react-native-renimated2,这玩意确实强大,对插值一直没详细研究过,今天用到一个动画,不得不用插值,就详细看了下,demo如下

import React from "react";
import { View, StyleSheet, Dimensions } from "react-native";
import Animated, {
  useSharedValue,
  useAnimatedScrollHandler,
  useAnimatedStyle,
  interpolate,
  Extrapolate,
} from "react-native-reanimated";
import { t } from "react-native-tailwindcss";

export const HEADER_IMAGE_HEIGHT = Dimensions.get("window").width / 3;

export default function Dashboard() {
  const scrollY = useSharedValue(0);
  const scrollHandler = useAnimatedScrollHandler({
    onScroll: (e) => {
      scrollY.value = e.contentOffset.y;
      console.log("====================================");
      console.log(scrollY.value);
      console.log("====================================");
    },
  });
  const animatedStyles = useAnimatedStyle(() => {
    const scale = interpolate(scrollY.value, [100, 0], [3, 1], {
      extrapolateRight: Extrapolate.CLAMP,
    });
    return {
      transform: [{ scale: scale }],
    };
  });

  return (
    <View
      style={[{ flex: 1, alignItems: "center" }, t.bgRed300, t.wFull, t.hFull]}
    >
      <Animated.View
        style={[
          {
            position: "absolute",
            top: 90,
            left: 20,
            width: 20,
            height: 20,
            backgroundColor: "blue",
          },
          animatedStyles,
        ]}
      />

      <Animated.ScrollView
        scrollEventThrottle={1}
        style={StyleSheet.absoluteFill}
        onScroll={scrollHandler}
      >
        {[0, 1, 2, 3, 43, 4, 45, 45, 45, 46, 432, 22, 333].map(() => (
          <View style={[t.h48]} />
        ))}
      </Animated.ScrollView>
    </View>
  );
}

其中关键是

 const scale = interpolate(scrollY.value, [-100, 0], [3, 1], {
      extrapolateRight: Extrapolate.CLAMP,
    });

这段代码我看了好久才搞明白,插值依赖的是scrollY.value,所以inputrange: [-100, 0],outputrange:[3,1]对应的就是当scrollY.value的值为-100的时候,scale的值就是3,当0的时候scale就是1,效果如下:

Nov-12-2021 21-48-43.gif
可以清晰看到,y的值在0到-100之间,scale的值都是1,当y<-100时候,scale变为成y/100=scale/3,这样算出来的scale

相关文章

  • react-native-renimated2 插值interp

    今天用了一天的react-native-renimated2,这玩意确实强大,对插值一直没详细研究过,今天用到一个...

  • Matlab中插值函数

    MATLAB中的插值函数为interp1,其调用格式为: yi= interp1(x,y,xi,'method'...

  • 线性插值 np.interp

    计算得出0-299这300个数的线性插值 ok, 300个数字线性插值完成!

  • 【Android 动画】动画详解之插值器(二)

    大家好,在上一篇中,我们介绍了Android 的补间动画,这一篇我们来说说动画的另外一个公共属性插值器Interp...

  • 缺失值处理-拉格朗日插值

    常用的插值法有:一维插值法:拉格朗日插值、牛顿插值、分段低次插值、埃尔米特插值、样条插值。二维插值法:双线性插值、...

  • 28. 图像缩放

    插值方法 四种插值,最近邻域插值 双线性插值 像素关系重采样 立方插值其中最近邻域插值、双线性插值原理如下: 1)...

  • 数值分析之插值

    插值 一.基本概念 1.1插值需要研究的问题 插值函数是否存在? 如何构造插值函数? 如何评估误差? 1.2插值法...

  • Less_变量插值

    选择器名插值 属性名插值 URL插值 import插值 媒体查询插值 less的作用域,就近原则,如果自己有这个变...

  • vue.js权威指南第2章——数据绑定

    一: 插值(Mustache标签)1: {{}},插值为数据;变体{{*text}},2: {{{}}} , 插值...

  • 【图像缩放算法】双立方(三次)插值

    当我们进行图像缩放的时候,我们就需要用到插值算法。常见的插值有: 最邻近插值 双线性插值 双立方(三次)插值在这三...

网友评论

      本文标题:react-native-renimated2 插值interp

      本文链接:https://www.haomeiwen.com/subject/gpaozltx.html