Giter Site home page Giter Site logo

xdanmuku's Introduction

XDanmuku V1.1版本

license

更新内容 V1.1

  1. 移动View线程数修改为1
  2. 加入View缓存,并能自动调整缓存空间大小
  3. 修改Entity绑定View的方式

使用方法

XDanmuku

一种支持多种弹幕样式的弹幕视图控件(点击可以查看添加库的方法)。

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

效果

  • 常规样式

  • 点击事件

  • 多种弹幕样式

  • 分区域显示

  • GIF效果图

使用

0. 添加依赖

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等请自行到点我查看。

1. 添加控件

在布局xml中添加控件

<com.orzangleli.xdanmuku.DanmuContainerView
    android:id="@+id/danmuContainerView"
    android:layout_width="match_parent"
    android:layout_height="240dp"
    />

2. 添加自定义弹幕Entity(需要继承自Model)

类似于DanmuEntity.java

3. 继承XAdapter

类似于ListView的BaseAdapter的结构,具体参照 DanmuAdapter.java

4. 添加弹幕

DanmuEntity danmuEntity = new DanmuEntity();
danmuEntity.setContent(SEED[random.nextInt(5)]);
danmuEntity.setType(0);
danmuEntity.setTime("23:20:11");
danmuContainerView.addDanmu(danmuEntity);

5. 弹幕点击事件监听

//弹幕点击事件
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();
    }
});

6. 设置弹幕移动速度

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);

7. 弹幕显示区域

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

//只在上方和中间区域显示弹幕
danmuContainerView.setGravity(DanmuContainerView.GRAVITY_TOP | DanmuContainerView.GRAVITY_CENTER);

致谢

感谢以下用户的建议和反馈:

附录

有几篇开发本库时的记录和心得,欢迎大家阅读点赞:

MIT License

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.

xdanmuku's People

Contributors

hust201010701 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xdanmuku's Issues

一进入页面就网络获取弹幕数据,然后加入到控件中,报数组越界

Process: com.orzangleli.recyclerviewdanmu, PID: 4134
                                                                            java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
                                                                                at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                                at java.util.ArrayList.get(ArrayList.java:308)
                                                                                at com.orzangleli.xdanmuku.DanmuContainerView.getBestLine(DanmuContainerView.java:228)
                                                                                at com.orzangleli.xdanmuku.DanmuContainerView.addTypeView(DanmuContainerView.java:180)
                                                                                at com.orzangleli.xdanmuku.DanmuContainerView.addDanmu(DanmuContainerView.java:158)

我在MainActivity的OnCreate里执行网络请求,获取到了20条弹幕数据。
然后直接调用addDanmu()方法,循环加入。直接就崩溃了

弹幕不显示

我刚刚使用了线程循环调用显示弹幕,发现刚开始有显示,但是之后又没有显示了。在没有显示的时候加入另一个弹幕时,之前不显示的弹幕又显示出来了。
我刚刚使用了每1秒加入一个1dp的view,暂时没有发现有不显示的view了。

源码有内存泄漏

看了下源码,开了一个线程,线程中跑了一个handler,建议作者在MyRunnable中加一个break开关,释放掉MyRunnable线程,并且使用handler.removeCallbacksAndMessages(null);移除掉所有消息,加一个release方法来在onDestory()中来释放,防止泄漏

请问怎么做视频的弹幕呢?

我们公司要做一款视频播放器,在播放器打开时可以获取到所有弹幕信息(包括弹幕position,弹幕position==视频当前播放进度),,这个怎么实现在弹幕position=视频position时再显示某个弹幕条目?

Todo

  1. 支持类似bilibili等网站弹幕按照时间保存的文件的输入和显示。

Thread new 出来就不管了?

大哥,恕小弟之言,你这 DanmuContainerView 里直接 new 了个 Thread就不管了?他可是一直在循环呢,一直到大于 7500,而且持有外部类的引用会造成内存泄露,而且里边一直用 handler 发送消息,handler 也会造成内存泄露,里边的 Handler 也是直接 new 出来了,啥也不管,一个界面可以,很多个界面不卡才怪呢,好的地方是可以一直循环,不好的地方就是内存泄露,卡,

这个控件如果放到 Adapter 上是用不 了的

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.orzangleli.xdanmuku.DanmuContainerView.getBestLine(DanmuContainerView.java:198)
at com.orzangleli.xdanmuku.DanmuContainerView.addTypeView(DanmuContainerView.java:150)
at com.orzangleli.xdanmuku.DanmuContainerView.addDanmu(DanmuContainerView.java:128)

关于设置danmuContainerView.setSpeed不起作用的问题

Demo中设置弹幕的滚动速度时,除了HIGH_SPEED有效以外,其他两项不起作用。
danmuContainerView.setSpeed(DanmuContainerView.HIGH_SPEED);

----无效属性:
danmuContainerView.setSpeed(DanmuContainerView.LOW_SPEED);
danmuContainerView.setSpeed(DanmuContainerView.NORMAL_SPEED);

弹幕覆盖

建议可自定义弹幕entity优先级,根据优先级排序既有完整性又有实时性

弹幕不显示

使用过程中发现2个问题,
1:在播放大布局弹幕时的过程中,会有几率第一张图片不显示
2:在播放过程中发现当再添加新的弹幕时,之前显示又出来了

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.