React Native 带动画效果的进度条

17 Mar 2025 | 阅读 2 分钟

在之前的 ProgressBarAndroid 教程中,我们学习了如何显示默认的进度条。 但是,我们也可以使用 Animated 类自定义进度条。

React Native 带动画效果的进度条示例

要创建动画进度条,我们需要导入 Animated 类。 在 View 中添加 Animated.ViewAnimated.Text 组件。 初始化变量 progress status 为 0 并调用 onAnimate() 方法。 当屏幕完全加载时,将调用此方法(componentDidMount() 调用)。 在 onAnimate() 方法上添加一个侦听器,该侦听器更新 progress status。

App.js

进度条的 progress status 以每 0.5 秒(50000 微秒)的间隔更新。 同时,进度条的宽度由 progress status 确定,其 status 设置到 Animated.Text 组件中。

输出

React Native ProgressBar With Animated React Native ProgressBar With Animated
下一个主题React Native 动画