使用React-Native-ART實(shí)現(xiàn)聲波、水波紋幔摸、波浪線動(dòng)畫效果(附源碼)

背景

筆者最近有個(gè)RN項(xiàng)目摸柄,需要在與設(shè)備通話的時(shí)候,App側(cè)展示聲波紋UI效果既忆,以筆者的尿性又去折騰整理了一番驱负,同樣也非常適用想了解react-native art繪圖入門基礎(chǔ)的同學(xué)。

在React Native中有個(gè)ART庫(kù)患雇,它可以繪制各種線(實(shí)線跃脊、虛線、曲線)苛吱、圖形(矩形酪术、圓、扇形)組合翠储,讓非郴嫜悖酷炫的繪圖及動(dòng)畫變成了可能。

至于水波紋援所、波浪動(dòng)畫庐舟、聲波音頻這些常用的效果,一般都是貝塞爾曲線加持畫出來(lái)的任斋。

水波紋.jpg

一继阻、UI效果展示

以下是我是整理和改進(jìn)UI效果,基本能滿足項(xiàng)目需要

水波紋波浪曲線

水波紋.gif
水波紋電量.gif

聲音聲波曲線

聲波1.gif

聲波2.gif

下載速率曲線

下載速率.gif

二废酷、源碼下載

源碼有需要的就拿去吧瘟檩,代碼實(shí)現(xiàn)就不一一貼出來(lái)解析了。然后針對(duì)ART這個(gè)知識(shí)點(diǎn)做一下梳理澈蟆,做了一個(gè)演示DEMO墨辛,這樣會(huì)有一個(gè)大概的了解。

=====> React-Native使用ART實(shí)現(xiàn)水波紋動(dòng)畫趴俘、波浪線睹簇、聲波音頻波動(dòng)效果(附源碼)

三奏赘、知識(shí)點(diǎn)梳理

添加依賴

Android默認(rèn)就包含ART庫(kù),IOS需要單獨(dú)添加依賴庫(kù)太惠。

右鍵點(diǎn)擊項(xiàng)目 -> ‘Add Files to ProjectName -> 選擇 node_modules/react-native/React/Libraries/ART/ART.xcodeproj’
將 libART.a 添加到 Linked Frameworks and Libraries

導(dǎo)入ART庫(kù)

import {
    ART
} from 'react-native';
 
const {
   Surface, Shape, Path, Group, Text
} = ART;

基礎(chǔ)組件

?Surface:一個(gè)矩形可渲染的區(qū)域磨淌,是其他元素的容器
width:渲染區(qū)域的寬
height:定義渲染區(qū)域的高

? Group:可容納多個(gè)形狀、文本和其他的分組

? Shape:形狀定義凿渊,可填充
d:定義繪制路徑
stroke:描邊顏色
strokeWidth:描邊寬度
strokeDash:定義虛線
fill:填充顏色

? Text:文本形狀定義
font:字體樣式梁只,定義字體、大小埃脏、粗細(xì)等搪锣,如bold 35px Heiti SC

? Path:Shape的屬性值 表示繪制的路徑
moveTo(x,y):移動(dòng)到坐標(biāo)(x,y)
lineTo(x,y):連線到坐標(biāo)(x,y)
line(x,y): 相對(duì)于原來(lái)的偏移
arc():繪制弧線
close():封閉空間

Path().moveTo(20, 20).lineTo(10, 20) 得到的是一條(20, 20) 到(10, 20) 的線。
Path().moveTo(20, 20).line(10, 20) 得到的是一條(20, 20) 到(30, 40) 的線彩掐。
Path().moveTo(150,50).arc(50, 100,50) 繪制一半圓弧(150,50)起點(diǎn)构舟,(50, 100)偏移量(終點(diǎn)) 50 半徑。

四堵幽、DEMO示例

Demo圖效

效果如下狗超,代碼不復(fù)雜的,聰明如你看看就明白谐檀,基本組件的操作下面也舉例說(shuō)明了

基礎(chǔ)元素

1.jpg

圓形抡谐、圓環(huán)、扇形桐猬、橢圓

2.jpg

三角形、半圓

3.jpg

多邊形

4.jpg

直線

了解Path的moveTo和LineTo的使用

import React from 'react'
import {
    View,
    ART
} from 'react-native'

export default class Line extends React.Component{

    render(){

        const path = ART.Path();
        path.moveTo(1,1); //將起始點(diǎn)移動(dòng)到(1,1) 默認(rèn)(0,0)
        path.lineTo(300,1); //連線到目標(biāo)點(diǎn)(300,1)

        return(
            <View style={this.props.style}>
                <ART.Surface width={300} height={2}>
                    <ART.Shape d={path} stroke="#000000" strokeWidth={1} />
                </ART.Surface>
            </View>
        )
    }
}

虛線

了解strokeDash的參數(shù)刽肠,
[10,5] : 表示繪10像素實(shí)線在繪5像素空白溃肪,如此循環(huán)
[10,5,20,5] : 表示繪10像素實(shí)線在繪制5像素空白在繪20像素實(shí)線及5像素空白

import React from 'react'
import {
    View,
    ART
} from 'react-native'

export default class Line extends React.Component{

    render(){

        const path = ART.Path();
        path.moveTo(1,1); //將起始點(diǎn)移動(dòng)到(1,1) 默認(rèn)(0,0)
        path.lineTo(300,1); //連線到目標(biāo)點(diǎn)(300,1)

        return(
            <View style={this.props.style}>
                <ART.Surface width={300} height={2}>
                    <ART.Shape d={path} stroke="#000000" strokeWidth={2} strokeDash={[10,10]} />
                </ART.Surface>
            </View>
        )
    }
}

折線

import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {width,height} = Dimensions.get('window');

export default class Line extends React.Component{

    render(){

        // 構(gòu)建折線路徑
        const brokenLine = Path()
            .move(10,60)
            .line(80, 23)
            .line(60, -43)
            .line(70, 20);
            
        return(
            <View style={this.props.style}>
                  <Surface width={width} height={100}>
                        <Shape d={brokenLine} stroke="#000000" strokeWidth={2}  />
                   </Surface>
            </View>
        )
    }
}

曲線

了解curve的用法

import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {width,height} = Dimensions.get('window');

export default class Line extends React.Component{

    render(){

        // 構(gòu)建貝斯路徑
        const curveLine = Path()
            .move(10,60)
            .curve(50, 23, 100, -23, 200, 0);
            
        return(
            <View style={this.props.style}>
                    <Surface width={width} height={100}>
                        <Shape d={curveLine} stroke="#000000" strokeWidth={2} />
                    </Surface>
            </View>
        )
    }
}

矩形

了解close()的使用,close的意思是創(chuàng)建一個(gè)密閉的路徑音五。首先通過(guò)lineTo繪制三條邊惫撰,在使用close鏈接第四條邊。fill做顏色填充

import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {Surface, Shape, Path} = ART;

export default class Rect extends React.Component{

    render(){

        const path = new Path()
            .moveTo(1,1)
            .lineTo(1,99)
            .lineTo(99,99)
            .lineTo(99,1)
            .close();

        return(
            <View style={this.props.style}>
                <Surface width={100} height={100}>
                    <Shape d={path} stroke="#000000" fill="#892265" strokeWidth={1} />
                </Surface>
            </View>
        )
    }
}

了解arc(x,y,radius)的使用, 終點(diǎn)坐標(biāo)距離起點(diǎn)坐標(biāo)的相對(duì)距離

import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {Surface, Shape, Path} = ART;

export default class Circle extends React.Component{

    render(){

        const path = new Path()
            .moveTo(50,1)
            .arc(0,99,25)
            .arc(0,-99,25)
            .close();


        return(
            <View style={this.props.style}>
                <Surface width={100} height={100}>
                    <Shape d={path} stroke="#000000" strokeWidth={1}/>
                </Surface>
            </View>
        )
    }
}

文本

了解font屬性的使用躺涝,規(guī)則是“粗細(xì) 字號(hào) 字體”

import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {Surface, Text, Path} = ART;

export default class ArtText extends  React.Component{


    render(){

        return(
            <View style={this.props.style}>
                <Surface width={200} height={100}>
                    <Text strokeWidth={1} stroke="#000" font="bold 35px Heiti SC" path={new Path().moveTo(40,40).lineTo(99,10)} >Hello World</Text>
                </Surface>
            </View>
        )
    }

}

扇形

注意這里封裝了一個(gè)Wedge組件厨钻,內(nèi)部還是使用arc做路徑繪制,感興趣的同學(xué)可以閱讀一下代碼

import React from 'react'
import {
    View,
    ART
} from  'react-native'

const {Surface} = ART;
import Wedge from './Wedge'

export default class Fan extends  React.Component{

    render(){

        return(
            <View style={this.props.style}>
                <Surface width={100} height={100}>
                    <Wedge
                     outerRadius={50}
                     startAngle={0}
                     endAngle={60}
                     originX={50}
                     originY={50}
                     fill="blue"/>

                </Surface>
            </View>
        )
    }
}

圖層

了解Group的使用

import React from 'react'
import {
    View,
    ART
} from 'react-native'

const {Surface, Shape,Text, Path,Group} = ART;

export default class GroupTest extends React.Component{

    render(){

        const pathRect = new Path()
            .moveTo(1,1)
            .lineTo(1,99)
            .lineTo(99,99)
            .lineTo(99,1)
            .close();

        const pathCircle = new Path()
            .moveTo(50,1)
            .arc(0,99,25)
            .arc(0,-99,25)
            .close();

        const pathText = new Path()
            .moveTo(40,5)
            .lineTo(40,99);


        return(
            <View>
                <Surface width={100} height={100}>
                    <Group>
                        <Shape d={pathRect} stroke="#000000" fill="#000000" strokeWidth={1}/>
                        <Shape d={pathCircle} stroke="#FFFFFF" fill="#FFFFFF" strokeWidth={1}/>
                        <Text strokeWidth={1} strokeDash={[2,1,2,1]} stroke="#000" font="bold 30px Heiti SC" path={pathText} >Swipe</Text>
                    </Group>
                </Surface>
            </View>
        )
    }
}

以上整理坚嗜,希望能幫助到有需要的同學(xué)夯膀,加油共勉!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末苍蔬,一起剝皮案震驚了整個(gè)濱河市诱建,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌碟绑,老刑警劉巖俺猿,帶你破解...
    沈念sama閱讀 216,744評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件茎匠,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡押袍,警方通過(guò)查閱死者的電腦和手機(jī)诵冒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)谊惭,“玉大人汽馋,你說(shuō)我怎么就攤上這事∥绲眩” “怎么了惭蟋?”我有些...
    開封第一講書人閱讀 163,105評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)药磺。 經(jīng)常有香客問我告组,道長(zhǎng),這世上最難降的妖魔是什么癌佩? 我笑而不...
    開封第一講書人閱讀 58,242評(píng)論 1 292
  • 正文 為了忘掉前任木缝,我火速辦了婚禮,結(jié)果婚禮上围辙,老公的妹妹穿的比我還像新娘我碟。我一直安慰自己,他們只是感情好姚建,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評(píng)論 6 389
  • 文/花漫 我一把揭開白布矫俺。 她就那樣靜靜地躺著,像睡著了一般掸冤。 火紅的嫁衣襯著肌膚如雪厘托。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評(píng)論 1 299
  • 那天稿湿,我揣著相機(jī)與錄音铅匹,去河邊找鬼。 笑死饺藤,一個(gè)胖子當(dāng)著我的面吹牛包斑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播涕俗,決...
    沈念sama閱讀 40,096評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼罗丰,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了咽袜?” 一聲冷哼從身側(cè)響起丸卷,我...
    開封第一講書人閱讀 38,939評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎询刹,沒想到半個(gè)月后谜嫉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體萎坷,經(jīng)...
    沈念sama閱讀 45,354評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評(píng)論 2 333
  • 正文 我和宋清朗相戀三年沐兰,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了哆档。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,745評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡住闯,死狀恐怖瓜浸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情比原,我是刑警寧澤插佛,帶...
    沈念sama閱讀 35,448評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站量窘,受9級(jí)特大地震影響雇寇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蚌铜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評(píng)論 3 327
  • 文/蒙蒙 一锨侯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧冬殃,春花似錦囚痴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至涣觉,卻和暖如春成箫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背旨枯。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留混驰,地道東北人攀隔。 一個(gè)月前我還...
    沈念sama閱讀 47,776評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像栖榨,于是被迫代替她去往敵國(guó)和親昆汹。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容