Flutter 實(shí)現(xiàn)虎牙/斗魚 彈幕功能
用Flutter實(shí)現(xiàn)彈幕功能,輕松實(shí)現(xiàn)虎牙、斗魚的彈幕效果。
先來一張效果圖:

實(shí)現(xiàn)原理
彈幕的實(shí)現(xià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)建一條最簡單的文字彈幕:
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)彈幕效果沒有我開始想的那么簡單,過程中也遇到了一些問題,不過好在最終都解決了,獻(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Android SDK-在64位Linux中使用需要注意的問題
本篇文章是對(duì)Android SDK-在64位Linux中使用需要注意的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android貝塞爾曲線實(shí)現(xiàn)直播點(diǎn)贊效果
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線實(shí)現(xiàn)直播點(diǎn)贊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android布局自定義Shap圓形ImageView可以單獨(dú)設(shè)置背景與圖片
這篇文章主要介紹了Android布局自定義Shap圓形ImageView可以單獨(dú)設(shè)置背景與圖片 的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android 利用ViewPager實(shí)現(xiàn)圖片可以左右循環(huán)滑動(dòng)效果附代碼下載
本文通過一個(gè)小demo給大家展示一段代碼實(shí)現(xiàn)viewpage圖片左右循環(huán)滑動(dòng)效果,對(duì)viewgager循環(huán)滑動(dòng)相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-11-11
Android 性能優(yōu)化系列之bitmap圖片優(yōu)化
在日常開發(fā)的APP,大部分時(shí)候需要想用戶展示圖片信息,圖片最終對(duì)應(yīng)Android中的Bitmap對(duì)象。而對(duì)于APP端來說Bitmap又是一個(gè)比較麻煩的問題,主要表現(xiàn)在Bitmap是非常占用內(nèi)存的對(duì)象,處理不當(dāng)將導(dǎo)致APP運(yùn)行卡頓甚至出現(xiàn)OOM2021-11-11
Android消息機(jī)制Handler用法總結(jié)
這篇文章介紹了Android消息機(jī)制Handler用法總結(jié),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
Android ANR(Application Not Responding)的分析
這篇文章主要介紹了Android ANR(Application Not Responding)的分析的相關(guān)資料,這里說明什么原因出現(xiàn)應(yīng)用程序的強(qiáng)制關(guān)閉,并說明該如何避免,需要的朋友可以參考下2017-08-08
Android系統(tǒng)在shell中的df命令實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于Android系統(tǒng)在shell中的df命令實(shí)現(xiàn),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Flutter 系統(tǒng)是如何實(shí)現(xiàn)ExpansionPanelList的示例代碼
Flutter組件有一個(gè)很大的特色,那就是很多復(fù)雜的組件都是通過一個(gè)一個(gè)小組件拼裝而成的,今天就來說說系統(tǒng)的ExpansionPanelList是如何實(shí)現(xiàn)的,需要的朋友可以參考下2020-05-05

