详细介绍 React Native 的动画系统。主要包括 Animated 组件的各种用法:

1.基础动画值的创建:

javascript 复制代码
import { Animated, Easing } from 'react-native';

// 创建动画值
const fadeAnim = new Animated.Value(0);    // 透明度动画值,初始值为0
const scaleAnim = new Animated.Value(1);   // 缩放动画值,初始值为1
const moveAnim = new Animated.ValueXY({    // 位移动画值,初始位置为 {x: 0, y: 0}
  x: 0,
  y: 0
});

2.基本动画类型:

javascript 复制代码
function AnimationExample() {
  // 创建动画值
  const [fadeAnim] = useState(new Animated.Value(0));
  
  // 淡入动画
  const fadeIn = () => {
    Animated.timing(fadeAnim, {
      toValue: 1,           // 目标值
      duration: 1000,       // 动画持续时间(毫秒)
      easing: Easing.ease,  // 缓动函数
      useNativeDriver: true // 使用原生动画驱动
    }).start();            // 开始动画
  };
  
  // 淡出动画
  const fadeOut = () => {
    Animated.timing(fadeAnim, {
      toValue: 0,
      duration: 1000,
      useNativeDriver: true
    }).start();
  };
  
  return (
    <Animated.View
      style={
  
  {
        opacity: fadeAnim, // 绑定动画值到样式
      }}>
      <Text>淡入淡出的文本</Text>
    </Animated.View>
  );
}

3.复杂动画组合:

javascript 复制代码
function ComplexAnimation() {
  // 创建多个动画值
  const moveX = new Animated.Value(0);
  const moveY = new Animated.Value(0);
  const scale = new Animated.Value(1);
  
  // 组合动画
  const startComplexAnimation = () => {
    // 并行动画:同时执行多个动画
    Animated.parallel([
      // X轴移动
      Animated.timing(moveX, {
        toValue: 100,
        duration: 1000,
        useNativeDriver: true
      }),
      // Y轴移动
      Animated.timing(moveY, {
        toValue: 100,
        duration: 1000,
        useNativeDriver: true
      }),
      // 缩放
      Animated.timing(scale, {
        toValue: 2,
        duration: 1000,
        useNativeDriver: true
      })
    ]).start();
  };
  
  // 序列动画:按顺序执行动画
  const startSequenceAnimation = () => {
    Animated.sequence([
      // 先移动
      Animated.timing(moveX, {
        toValue: 100,
        duration: 1000,
        useNativeDriver: true
      }),
      // 再缩放
      Animated.timing(scale, {
        toValue: 2,
        duration: 1000,
        useNativeDriver: true
      })
    ]).start();
  };
  
  return (
    <Animated.View
      style={
  
  {
        transform: [
          { translateX: moveX },
          { translateY: moveY },
          { scale: scale }
        ]
      }}>
      <Text>复杂动画示例</Text>
    </Animated.View>
  );
}

4.弹性动画:

javascript 复制代码
function SpringAnimation() {
  const springAnim = new Animated.Value(0);
  
  const startSpring = () => {
    Animated.spring(springAnim, {
      toValue: 1,           // 目标值
      friction: 3,          // 摩擦力,越小弹性越大
      tension: 40,          // 张力,越大速度越快
      useNativeDriver: true
    }).start();
  };
  
  return (
    <Animated.View
      style={
  
  {
        transform: [{
          scale: springAnim.interpolate({
            inputRange: [0, 1],
            outputRange: [1, 2]
          })
        }]
      }}>
      <Text>弹性动画</Text>
    </Animated.View>
  );
}

5.插值动画:

javascript 复制代码
function InterpolateAnimation() {
  const animValue = new Animated.Value(0);
  
  // 开始动画
  const startAnimation = () => {
    Animated.timing(animValue, {
      toValue: 1,
      duration: 1000,
      useNativeDriver: true
    }).start();
  };
  
  return (
    <Animated.View
      style={
  
  {
        opacity: animValue,  // 透明度变化
        transform: [{
          scale: animValue.interpolate({  // 缩放插值
            inputRange: [0, 0.5, 1],      // 输入范围
            outputRange: [1, 2, 1]        // 输出范围
          })
        }],
        backgroundColor: animValue.interpolate({  // 颜色插值
          inputRange: [0, 1],
          outputRange: ['red', 'blue']
        })
      }}>
      <Text>插值动画示例</Text>
    </Animated.View>
  );
}

这些示例涵盖了 React Native 动画的基础用法。每个动画都可以根据需要进行组合和调整,创建更复杂的动画效果。记住要合理使用 useNativeDriver 来提高动画性能。

相关推荐
星栈独行1 小时前
翻完 Pi 源码:它和 Codex、Claude Code 有何不同
开发语言·javascript·人工智能·程序人生
落落Plus2 小时前
浏览器缓存机制详解
javascript·缓存·浏览器缓存
Darling噜啦啦2 小时前
React 组件化实战:从 TodoList 吃透父子通信与状态管理
react.js
浅水壁虎4 小时前
vue基础(第二章 )
前端·javascript·vue.js
界面开发小八哥4 小时前
界面控件DevExtreme v26.1新版亮点——支持Angular 22
前端·javascript·angular.js·devexpress·ui开发·devextreme
mONESY6 小时前
React useState 核心原理与最佳实践:从异步更新到性能优化全解
javascript
腻害兔6 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:CRM 客户关系模块深度解析——从线索到回款,一套完整的 B2B 销售闭环是怎么搭的?
java·前端·javascript·vue.js·产品经理·ai编程
gis开发之家7 小时前
《Vue3 从入门到大神37篇》Vue3 源码详解(七):watch 与 watchEffect 源码对比——副作用是如何被追踪的?
javascript·前端框架·vue3·vue3源码
bloglin999998 小时前
langchain 和 langgraph 和 react
javascript·react.js·langchain
触底反弹8 小时前
🚀 删了数据刷新又回来?3 组件 × 4 回调 × 3 坑讲透 React 父子通信
前端·javascript·react.js