android弹幕控件,可能是目前轻量级弹幕控件中功能最强大的一款

本项目是一个开源的弹幕控件库,能够支持多种样式弹幕,弹幕点击监听,弹幕分区域显示,自定义移动速度等功能,项目原理是通过自定义ViewGroup。可能是目前轻量级弹幕控件中功能最强大的一款了。

Github项目地址:github.com/hust2010107…,希望你能Star或者提交Issues.

效果

常规样式

ef6f3aec86eda8387b5a345476ce7556.png

点击事件

9ee9cf52aafd888090ba486de2a66501.png

多种弹幕样式

0b96707769552553e9168f8dd848bfcb.png

分区域显示

73df7069094a29902424cd95a810a40a.png

GIF效果图

7b3cfbcc0ecd0bbdcc28ef27dc91e612.gif

使用

0. 添加依赖

1. 导入xdanmuku源码

你可以直接下载本项目xdanmuku模块,并导入项目目录,并添加依赖compile project(':xdanmuku')

2. Gradle

1

先把jitpack仓库添加到项目根 build.gradle(Project)文件中,

allprojects {

repositories {

...

maven { url 'https://jitpack.io' }

}

}复制代码

然后在你的项目中添加依赖

dependencies {

compile 'com.github.hust201010701:XDanmuku:33a063b46e'

}复制代码

其他添加依赖的方式,如maven等请自行到点我查看。

1. 添加控件

在布局xml中添加控件

android:id="@+id/danmuContainerView"

android:layout_width="match_parent"

android:layout_height="240dp"

/>复制代码

2. 自定义弹幕实体类DanmuEntity

根据需求定制弹幕实体类,包含所有弹幕的属性和类型。

public class DanmuEntity{

public String content;

public int textColor;

public int backgroundColor;

public int type;

public String time;

}复制代码

3. 添加DanmuConverter(弹幕转换器)

DanmuConverter中有两个抽象方法需要实现,getSingleLineHeight是返回所有弹幕样式中高度最大值作为弹幕航道的高度;convert负责将弹幕实体类DanmuEntity绑定到弹幕子视图上(类似于BaseAdapter的getView方法的作用)。

DanmuConverter danmuConverter = new DanmuConverter() {

@Override

public int getSingleLineHeight() {

//将所有类型弹幕的布局拿出来,找到高度最大值,作为弹道高度

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_danmu, null);

//指定行高

view.measure(0, 0);

View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_super_danmu, null);

//指定行高

view2.measure(0, 0);

return Math.max(view.getMeasuredHeight(),view2.getMeasuredHeight());

}

@Override

public View convert(DanmuEntity model) {

View view = null;

if(model.getType() == 0) {

view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_danmu, null);

TextView content = (TextView) view.findViewById(R.id.content);

ImageView image = (ImageView) view.findViewById(R.id.image);

image.setImageResource(ICON_RESOURCES[random.nextInt(5)]);

content.setText(model.content);

content.setTextColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));

}

else if(model.getType() == 1) {

view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_super_danmu, null);

TextView content = (TextView) view.findViewById(R.id.content);

content.setText(model.content);

TextView time = (TextView) view.findViewById(R.id.time);

time.setText(model.getTime());

}

return view;

}

};复制代码

4. 添加弹幕

DanmuEntity danmuEntity = new DanmuEntity();

danmuEntity.setContent(doubleSeed.substring(index, index + 2 + random.nextInt(20)));

danmuEntity.setType(random.nextInt(2));

danmuEntity.setTime("23:20:11");

try {

danmuContainerView.addDanmu(danmuEntity);

} catch (Exception e) {

e.printStackTrace();

}复制代码

5. 弹幕点击事件监听

//弹幕点击事件

danmuContainerView.setOnItemClickListener(new DanmuContainerView.OnItemClickListener() {

@Override

public void onItemClick(DanmuEntity danmuEntity) {

Toast.makeText(MainActivity.this,danmuEntity.content,Toast.LENGTH_SHORT).show();

}

});复制代码

6. 设置弹幕移动速度

DanmuContainerView中预设了三种弹幕移动速度:

public final static int LOW_SPEED = 1;

public final static int NORMAL_SPEED = 3;

public final static int HIGH_SPEED = 5;复制代码

设置速度通过setSpeed方法:

danmuContainerView.setSpeed(DanmuContainerView.HIGH_SPEED);复制代码

同时你可以传递具体的整数型速度:

danmuContainerView.setSpeed(4);复制代码

7. 弹幕显示区域

本人将弹幕控件按照竖向均分为3份,分别为GRAVITY_TOP,GRAVITY_CENTER,GRAVITY_BOTTOM。用户可以自由组合显示区域,默认情况下全区域(GRAVITY_FULL)显示。设置要显示的区域通过setGravity方法实现,参数可以使用 | 进行连接。

//只在上方和中间区域显示弹幕

danmuContainerView.setGravity(DanmuContainerView.GRAVITY_TOP | DanmuContainerView.GRAVITY_CENTER);复制代码

后记

本控件的原理你可能已经知道了使用自定义ViewGroup实现。但是之前我花了很多事件尝试通过自定义LayoutManager让RecyclerView实现弹幕控件,不过最终这种方案失败了,更多细节讨论欢迎发送邮件(orzangleli@163.com)给我。

欢迎Star,提交Issues。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值