Flutter 實(shí)現(xiàn)虎牙/斗魚 彈幕功能
用Flutter實(shí)現(xiàn)彈幕功能,輕松實(shí)現(xiàn)虎牙、斗魚的彈幕效果。
先來一張效果圖:
實(shí)現(xiàn)原理
彈幕的實(shí)現(xiàn)原理非常簡(jiǎn)單,即將一條彈幕從左側(cè)平移到右側(cè),當(dāng)然我們要計(jì)算彈幕垂直方向上的偏移,不然所有的彈幕都會(huì)在一條直線上,相互覆蓋。平移代碼如下:
@override void initState() { _animationController = AnimationController(duration: widget.duration, vsync: this) ..addStatusListener((status){ if(status == AnimationStatus.completed){ widget.onComplete(''); } }); var begin = Offset(-1.0, .0); var end = Offset(1.0, .0); _animation = Tween(begin: begin, end: end).animate(_animationController); //開始動(dòng)畫 _animationController.forward(); super.initState(); } @override Widget build(BuildContext context) { return SlideTransition( position: _animation, child: widget.child, ); }
計(jì)算垂直方向的偏移:
_computeTop(int index, double perRowHeight) { //第幾輪彈幕 int num = (index / widget.showCount).floor(); var top; top = (index % widget.showCount) * perRowHeight + widget.padding; if (num % 2 == 1 && index % widget.showCount != widget.showCount - 1) { //第二輪在第一輪2行彈幕中間 top += perRowHeight / 2; } if (widget.randomOffset != 0 && top > widget.randomOffset) { top += _random.nextInt(widget.randomOffset) * 2 - widget.randomOffset; } return top; }
這些準(zhǔn)備好后,就是創(chuàng)建一條彈幕了,現(xiàn)創(chuàng)建一條最簡(jiǎn)單的文字彈幕:
Text( text, style: TextStyle(color: Colors.white), );
效果如下:
創(chuàng)建一條VIP用戶的彈幕,其實(shí)就是字體變下顏色:
Text( text, style: TextStyle(color: Color(0xFFE9A33A)), )
效果如下:
給文字加個(gè)圓角背景:
return Center( child: Container( padding: EdgeInsets.only(left: 10, right: 10, top: 3, bottom: 3), decoration: BoxDecoration( color: Colors.red.withOpacity(.8), borderRadius: BorderRadius.circular(50)), child: Text( text, style: TextStyle(color: Colors.white), ), ), );
效果如下:
創(chuàng)建一個(gè)送火箭的彈幕:
return Center( child: Container( padding: EdgeInsets.only(left: 10, right: 10, top: 3, bottom: 3), decoration: BoxDecoration( color: Colors.red.withOpacity(.8), borderRadius: BorderRadius.circular(50)), child: Row( mainAxisSize: MainAxisSize.min, children: <Widget>[ Text( text, style: TextStyle(color: Colors.white), ), Image.asset('assets/images/timg.jpeg',height: 30,width: 30,), Text( 'x $count', style: TextStyle(color: Colors.white, fontSize: 18), ), ], ), ), );
效果如下:
火箭有點(diǎn)丑了,不過這不是重點(diǎn)。
其實(shí)實(shí)現(xiàn)彈幕效果沒有我開始想的那么簡(jiǎn)單,過程中也遇到了一些問題,不過好在最終都解決了,獻(xiàn)上Github地址:https://github.com/781238222/flutter-do/tree/master/flutter_barrage_sample
如果覺得還不錯(cuò),給個(gè)小小的贊。
交流
Github地址:https://github.com/781238222/flutter-do
170+組件詳細(xì)用法:http://laomengit.com
總結(jié)
到此這篇關(guān)于Flutter 實(shí)現(xiàn)虎牙/斗魚 彈幕功能的文章就介紹到這了,更多相關(guān)Flutter 實(shí)現(xiàn)虎牙斗魚 彈幕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 狀態(tài)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 狀態(tài)欄虛擬導(dǎo)航鍵透明效果的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Android如何實(shí)現(xiàn)設(shè)備的異顯功能詳解
這篇文章主要給大家介紹了關(guān)于Android如何實(shí)現(xiàn)設(shè)備的異顯功能的相關(guān)資料,這篇文章通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02Android使用ViewPager實(shí)現(xiàn)滾動(dòng)廣告
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)滾動(dòng)廣告,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android編程實(shí)現(xiàn)在Activity中操作刷新另外一個(gè)Activity數(shù)據(jù)列表的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)在Activity中操作刷新另外一個(gè)Activity數(shù)據(jù)列表的方法,結(jié)合具體實(shí)例形式分析了2種常用的Activity交互實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06利用Fiddler對(duì)手機(jī)進(jìn)行抓包的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄肍iddler對(duì)手機(jī)進(jìn)行抓包的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06淺談Android Studio導(dǎo)出javadoc文檔操作及問題的解決
這篇文章主要介紹了淺談Android Studio導(dǎo)出javadoc文檔操作及問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03