React Native实现一个渐变进度条

用ReactNative做一个渐变的进度条,可以用RN自带的ART画一个,也可以用SVG库来实现。今天,用自带的ART库实现一个渐变进度条。ART的用法就不细讲了,可以参考这个链接ARTDOC

1.新建一个React Native的测试工程

react-native init LineProgressBar

2.导入ART库(以iOS为例)

将 node_modules/react-native/Libraries/ART/ART.xcodeproj
添加到工程Libraries下 并连接libART.a

  1. 导入ART到js文件
import {
    StyleSheet,
    View,
    Text,
    TouchableOpacity,
    ART
} from 'react-native';

const {
    Surface,
    Group,
    Shape,
    LinearGradient,
    Path,
    Transform,
    ClippingRectangle
} = ART;

4.进度条需要个底色和进度的颜色咱们可以用ART的Group让他们整合在一起

  <Surface 
      width={this.props.style.width} height={this.props.style.height}
  >
    <Group>
        <Shape
            d={this.state.backgroundPath}
            fill={'#F2F2F2'}
        />
        <Shape
            d={this.state.abovePath}
            fill={this.state.barColor}
            strokeWidth={this.props.strokeWidth}
            stroke={this.props.strokeColor}
            strokeCap={'butt'}
            strokeJoin={'bevel'}
        />

    </Group>
 </Surface>

5.下面画一个背景色

new Path()
    .moveTo(height / 2 + strokeWidth,  strokeWidth)
    .lineTo(width - height / 2 - strokeWidth, strokeWidth)
    .arcTo(width - strokeWidth, height/ 2 + strokeWidth)
    .arcTo(width - strokeWidth - height / 2, height + strokeWidth)
    .lineTo(height / 2 + strokeWidth, height + strokeWidth)
    .arcTo(strokeWidth, height / 2 + strokeWidth)
    .arcTo(height / 2 + strokeWidth, strokeWidth)
    .close()

6.实现一个渐变色

 new LinearGradient({
                '0': startColor,
                '1': endColor
            },
            '', "", this.props.style.width,  ''
        )

7.接下来实现一个动画。 运用RN的Animated来创建个动画组件。

 const AnimatedLineProgressBars = Animated.createAnimatedComponent(LineProgressBar);

8.用RN的Animated.timing 来实现动画效果

 Animated.timing(this.state.progressNumber, {
        toValue: progress,
        duration: 1000
    }).start();

实现动画的代码

componentDidMount() {
    this.startAnimate(this.props.progressNumber)
}

componentWillReceiveProps(nextProps) {
    if (nextProps != this.props && nextProps.progressNumber != this.props.progressNumber) {
        this.startAnimate(nextProps.progressNumber)
    }
}

startAnimate(progress) {
    this.state.progressNumber.setValue(0);
    Animated.timing(this.state.progressNumber, {
        toValue: progress,
        duration: 1000
    }).start();
}

render() {

    const {progressNumber, ...other} = this.props;

    return (
        <AnimatedLineProgressBars
            {...other}
            progressNumber = {this.state.progressNumber}
        />
    )
}

调用代码

  <AnimatedLineProgressBar
            style={{
                      marginTop: 10,
                      height: 25,
                      width: width
                   }}
            progressTotal={100}
            progressNumber={90}
            strokeWidth={0}
            barStartColor={'orange'}
            barEndColor={'purple'}
        />

完整项目可查看:https://github.com/RoarRain/react-native-lineProgressBar-example

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • React Native优秀博客,以及优秀的Github库列表(很多英文资料源自于[awesome-react-n...
    董董董董董董董董董大笨蛋阅读 10,982评论 4 162
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,868评论 25 709
  • 今天我们的蚕卵又孵化出了三条,现在一共五条蚕愉快得啃着桑叶了,我和悠爸非常开心,貌似我们俩比悠悠更开心,悠悠...
    枝枝Rena阅读 1,466评论 6 2
  • 为什么写作?原来还真没有认真思考过! 从爱好起,从青春年少时开始了写作的尝试。那时读的书少,了解的信息也很少,更没...
    白丰阁阅读 376评论 1 3
  • 高中的班主任,很抱歉我一时想不起来他的名字了。可我一直都记得我们给他起的外号~狒狒,这里没有贬义。我们大家都很喜欢...
    天空之鱼阅读 362评论 0 0

友情链接更多精彩内容