React Native - ProgressBar动画

React Native - ProgressBar动画 首页 / React Native入门教程 / React Native - ProgressBar动画

在此示例中,我们将实现水平的ProgressBarAndroid并在TouchableOpacity组件上执行操作。进度状态将在“Text"组件中最多显示3位数字。

ProgressBar动画示例

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

App.js

import React, {Component} from 'react';  
import {Platform, StyleSheet, Text, View, Animated} from 'react-native';  
  
export default class App extends Component {  
    state={  
        progressStatus: 0,  
    }  
    anim = new Animated.Value(0);  
    componentDidMount(){  
        this.onAnimate();  
    }  
    onAnimate = () =>{  
        this.anim.addListener(({value})=> {  
            this.setState({progressStatus: parseInt(value,10)});  
        });  
        Animated.timing(this.anim,{  
             toValue: 100,  
             duration: 50000,  
        }).start();  
    }  
  render() {  
    return (  
      <View style={styles.container}>  
            <Animated.View  
                style={[  
                    styles.inner,{width: this.state.progressStatus +"%"},  
                ]}  
            />  
            <Animated.Text style={styles.label}>  
                    {this.state.progressStatus }%  
            </Animated.Text>  
      </View>  
    );  
  }  
}  
const styles = StyleSheet.create({  
    container: {  
    width: "100%",  
    height: 40,  
    padding: 3,  
    borderColor: "#FAA",  
    borderWidth: 3,  
    borderRadius: 30,  
    marginTop: 200,  
    justifyContent: "center",  
  },  
  inner:{  
    width: "100%",  
    height: 30,  
    borderRadius: 15,  
    backgroundColor:"green",  
  },  
  label:{  
    fontSize:23,  
    color: "black",  
    position: "absolute",  
    zIndex: 1,  
    alignSelf: "center",  
  }  
});  

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

无涯教程网

输出:

React Native ProgressBar With AnimatedReact Native ProgressBar With Animated

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

人工智能基础课 -〔王天一〕

白话法律42讲 -〔周甲徳〕

即时消息技术剖析与实战 -〔袁武林〕

深入浅出云计算 -〔何恺铎〕

数据中台实战课 -〔郭忆〕

视觉笔记入门课 -〔高伟〕

流程型组织15讲 -〔蒋伟良〕

大厂广告产品心法 -〔郭谊〕

AI 应用实战课 -〔黄佳〕

好记忆不如烂笔头。留下您的足迹吧 :)