React Native實(shí)現(xiàn)一個(gè)漸變進(jìn)度條

用ReactNative做一個(gè)漸變的進(jìn)度條,可以用RN自帶的ART畫一個(gè),也可以用SVG庫(kù)來(lái)實(shí)現(xiàn)。今天,用自帶的ART庫(kù)實(shí)現(xiàn)一個(gè)漸變進(jìn)度條。ART的用法就不細(xì)講了,可以參考這個(gè)鏈接ARTDOC

1.新建一個(gè)React Native的測(cè)試工程

react-native init LineProgressBar

2.導(dǎo)入ART庫(kù)(以iOS為例)

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

  1. 導(dǎo)入ART到j(luò)s文件
import {
    StyleSheet,
    View,
    Text,
    TouchableOpacity,
    ART
} from 'react-native';

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

4.進(jìn)度條需要個(gè)底色和進(jìn)度的顏色咱們可以用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.下面畫一個(gè)背景色

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.實(shí)現(xiàn)一個(gè)漸變色

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

7.接下來(lái)實(shí)現(xiàn)一個(gè)動(dòng)畫。 運(yùn)用RN的Animated來(lái)創(chuàng)建個(gè)動(dòng)畫組件。

 const AnimatedLineProgressBars = Animated.createAnimatedComponent(LineProgressBar);

8.用RN的Animated.timing 來(lái)實(shí)現(xiàn)動(dòng)畫效果

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

實(shí)現(xiàn)動(dòng)畫的代碼

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}
        />
    )
}

調(diào)用代碼

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

完整項(xiàng)目可查看:https://github.com/RoarRain/react-native-lineProgressBar-example

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • React Native優(yōu)秀博客,以及優(yōu)秀的Github庫(kù)列表(很多英文資料源自于[awesome-react-n...
    董董董董董董董董董大笨蛋閱讀 11,013評(píng)論 4 162
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,058評(píng)論 25 709
  • 今天我們的蠶卵又孵化出了三條,現(xiàn)在一共五條蠶愉快得啃著桑葉了,我和悠爸非常開心,貌似我們倆比悠悠更開心,悠悠...
    枝枝Rena閱讀 1,490評(píng)論 6 2
  • 為什么寫作?原來(lái)還真沒有認(rèn)真思考過! 從愛好起,從青春年少時(shí)開始了寫作的嘗試。那時(shí)讀的書少,了解的信息也很少,更沒...
    白豐閣閱讀 386評(píng)論 1 3
  • 高中的班主任,很抱歉我一時(shí)想不起來(lái)他的名字了??晌乙恢倍加浀梦覀兘o他起的外號(hào)~狒狒,這里沒有貶義。我們大家都很喜歡...
    天空之魚閱讀 366評(píng)論 0 0

友情鏈接更多精彩內(nèi)容