Flutter 实现虎牙/斗鱼 弹幕功能
作者:老孟程序员 时间:2023-07-11 04:11:11
用Flutter实现弹幕功能,轻松实现虎牙、斗鱼的弹幕效果。
先来一张效果图:
实现原理
弹幕的实现原理非常简单,即将一条弹幕从左侧平移到右侧,当然我们要计算弹幕垂直方向上的偏移,不然所有的弹幕都会在一条直线上,相互覆盖。平移代码如下:
@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);
//开始动画
_animationController.forward();
super.initState();
}
@override
Widget build(BuildContext context) {
return SlideTransition(
position: _animation,
child: widget.child,
);
}
计算垂直方向的偏移:
_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;
}
这些准备好后,就是创建一条弹幕了,现创建一条最简单的文字弹幕:
Text(
text,
style: TextStyle(color: Colors.white),
);
效果如下:
创建一条VIP用户的弹幕,其实就是字体变下颜色:
Text(
text,
style: TextStyle(color: Color(0xFFE9A33A)),
)
效果如下:
给文字加个圆角背景:
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),
),
),
);
效果如下:
创建一个送火箭的弹幕:
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),
),
],
),
),
);
效果如下:
火箭有点丑了,不过这不是重点。
其实实现弹幕效果没有我开始想的那么简单,过程中也遇到了一些问题,不过好在最终都解决了,献上Github地址:https://github.com/781238222/flutter-do/tree/master/flutter_barrage_sample
如果觉得还不错,给个小小的赞。
交流
Github地址:https://github.com/781238222/flutter-do
170+组件详细用法:http://laomengit.com
来源:https://www.cnblogs.com/mengqd/archive/2020/04/13/12694159.html
标签:Flutter,弹幕
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android自定义RadioGroupX实现多行多列布局
2023-03-27 17:19:47
![](https://img.aspxhome.com/file/2023/9/128989_0s.jpg)
SpringBoot实现模块日志入库的项目实践
2022-06-15 10:32:49
![](https://img.aspxhome.com/file/2023/6/91136_0s.png)
java中使用数组进行模拟加密的方法
2023-11-18 15:37:39
C#用递归算法实现:一列数的规则如下: 1、1、2、3、5、8、13、21、34,求第30位数是多少
2023-12-23 01:22:51
Java之int和string类型转换详解
2023-11-06 04:10:06
C#网络爬虫代码分享 C#简单的爬取工具
2023-10-12 15:03:41
![](https://img.aspxhome.com/file/2023/5/72475_0s.png)
递归法求最大公约数和最小公倍数的实现代码
2023-10-07 02:11:34
Java中this,static,final,const用法详解
2022-01-23 17:21:41
关于Hibernate的一些学习心得总结
2022-04-23 23:51:37
详解Java分布式事务的 6 种解决方案
2022-06-19 13:33:49
springboot集成redis并使用redis生成全局唯一索引ID
2023-11-28 05:42:39
![](https://img.aspxhome.com/file/2023/0/59020_0s.jpg)
response文件流输出文件名中文不显示的解决
2023-02-06 19:41:02
java实现读取、删除文件夹下的文件
2021-12-06 20:07:48
C#中ToString数据类型格式大全(千分符)
2023-10-03 05:08:48
详解Java执行groovy脚本的两种方式
2021-05-28 09:23:27
![](https://img.aspxhome.com/file/2023/8/132018_0s.png)
Groovy动态语言使用教程简介
2022-04-28 15:05:54
java实现简单学生成绩管理系统
2023-08-15 18:38:57
![](https://img.aspxhome.com/file/2023/8/98708_0s.jpg)
Android编程使用Fragment界面向下跳转并一级级返回的实现方法
2021-08-20 19:56:51
![](https://img.aspxhome.com/file/2023/0/138790_0s.png)
JAVA中实现链式操作(方法链)的简单例子
2022-12-16 00:54:50
flyway实现java 自动升级SQL脚本的问题及解决方法
2021-10-14 00:23:19
![](https://img.aspxhome.com/file/2023/9/60399_0s.jpg)