一种支持多种弹幕样式的弹幕视图控件(点击可以查看添加库的方法)。
本项目是一个开源的弹幕控件库,能够支持多种样式弹幕,弹幕点击监听,弹幕分区域显示,自定义移动速度等功能,项目原理是通过自定义ViewGroup。可能是目前轻量级弹幕库中功能最强大的一款了。
1. 导入xdanmuku
源码
你可以直接下载本项目xdanmuku
模块,并导入项目目录,并添加依赖compile project(':xdanmuku')
2. Gradle
先把jitpack仓库添加到项目根 build.gradle(Project)
文件中,
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
然后在你的项目中添加依赖
dependencies {
compile 'com.github.hust201010701:XDanmuku:-SNAPSHOT'
}
其他添加依赖的方式,如maven等请自行到点我查看。
在布局xml中添加控件
<com.orzangleli.xdanmuku.DanmuContainerView
android:id="@+id/danmuContainerView"
android:layout_width="match_parent"
android:layout_height="240dp"
/>
类似于ListView的BaseAdapter的结构,具体参照 DanmuAdapter.java
DanmuEntity danmuEntity = new DanmuEntity();
danmuEntity.setContent(SEED[random.nextInt(5)]);
danmuEntity.setType(0);
danmuEntity.setTime("23:20:11");
danmuContainerView.addDanmu(danmuEntity);
//弹幕点击事件
danmuContainerView.setOnItemClickListener(new DanmuContainerView.OnItemClickListener() {
@Override
public void onItemClick(Model model) {
DanmuEntity danmuEntity = (DanmuEntity) model;
Toast.makeText(MainActivity.this, danmuEntity.content, Toast.LENGTH_SHORT).show();
}
});
DanmuContainerView
中预设了三种弹幕移动速度:
public final static int LOW_SPEED = 1;
public final static int NORMAL_SPEED = 4;
public final static int HIGH_SPEED = 8;
设置速度通过setSpeed
方法:
danmuContainerView.setSpeed(DanmuContainerView.HIGH_SPEED);
同时你可以传递具体的int
型速度(建议速度值在1-8之间,数值越大速度越快):
danmuContainerView.setSpeed(5);
本人将弹幕控件按照竖向均分为3份,分别为GRAVITY_TOP
,GRAVITY_CENTER
,GRAVITY_BOTTOM
。用户可以自由组合显示区域,默认情况下全区域(GRAVITY_FULL
)显示。设置要显示的区域通过setGravity
方法实现,参数可以使用 |
进行连接。
//只在上方和中间区域显示弹幕
danmuContainerView.setGravity(DanmuContainerView.GRAVITY_TOP | DanmuContainerView.GRAVITY_CENTER);
感谢以下用户的建议和反馈:
有几篇开发本库时的记录和心得,欢迎大家阅读点赞:
Copyright (c) 2017 orzangleli
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。