GithubHelp home page GithubHelp logo

danmukulight's Introduction

DanmukuLight

Android上专为视频直播打造的轻量级弹幕库(100多kb)

demo运行后的效果如下:

下载示例Apk

目前支持以下需求

  • 继承自View实现的DanMuView(直播间的弹幕,推荐使用这种)
  • 继承自SurfaceView实现的DanMuView
  • 支持显示富文本内容
  • 支持弹幕单击事件的处理

使用:

在Module中的build.gradle文件里,添加以下依赖:

 allprojects {
    repositories {
        jcenter()

        maven {
            url 'https://dl.bintray.com/hpdx/maven/'
        }
    }
 }

 compile 'com.anbetter:danmukulight:1.0.0'

在xml中添加

        <com.anbetter.danmuku.DanMuView
            android:id="@+id/danmaku_container_broadcast"
            android:layout_width="match_parent"
            android:layout_height="80dp"/>

注:每条弹道的高度目前为40dp

启动弹幕引擎

        mDanMuContainerBroadcast = (DanMuView) findViewById(R.id.danmaku_container_broadcast);
        mDanMuContainerBroadcast.prepare();

创建弹幕对象

        DanMuModel danMuView = new DanMuModel();
        danMuView.setDisplayType(DanMuModel.RIGHT_TO_LEFT);
        danMuView.setPriority(DanMuModel.NORMAL);
        danMuView.marginLeft = DimensionUtil.dpToPx(mContext, 30);

        // 显示的文本内容
        danMuView.textSize = DimensionUtil.spToPx(mContext, 14);
        danMuView.textColor = ContextCompat.getColor(mContext, R.color.light_green);
        danMuView.textMarginLeft = DimensionUtil.dpToPx(mContext, 5);

        if (entity.getRichText() != null) {
            danMuView.text = RichTextParse.parse(mContext, entity.getRichText(), DimensionUtil.spToPx(mContext, 18), false);
        } else {
            danMuView.text = entity.getText();
        }

        // 弹幕文本背景
        danMuView.textBackground = ContextCompat.getDrawable(mContext, R.drawable.corners_danmu);
        danMuView.textBackgroundMarginLeft = DimensionUtil.dpToPx(mContext, 15);
        danMuView.textBackgroundPaddingTop = DimensionUtil.dpToPx(mContext, 3);
        danMuView.textBackgroundPaddingBottom = DimensionUtil.dpToPx(mContext, 3);
        danMuView.textBackgroundPaddingRight = DimensionUtil.dpToPx(mContext, 15);

将弹幕添加到弹道上

mDanMuContainerBroadcast.add(danMuView);

OK了,就这么简单,一条弹幕就发送成功了

对了,若要弹幕是能响应单击事件,需要添加如下处理:

在xml中添加DanMuParentView

 <com.anbetter.danmuku.DanMuParentView
        android:id="@+id/dpv_broadcast"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <com.anbetter.danmuku.DanMuView
            android:id="@+id/danmaku_container_broadcast"
            android:layout_width="match_parent"
            android:layout_height="80dp"/>

    </com.anbetter.danmuku.DanMuParentView>

在构建弹幕对象的代码中添加

        danMuView.enableTouch(true);
        danMuView.setOnTouchCallBackListener(new OnDanMuTouchCallBackListener() {
             @Override
             public void callBack(DanMuModel danMuView) {

             }
        });

在使用过程中有遇到bug,欢迎提issuse ! 若你觉得还不错,请点Star, 谢谢!

danmukulight's People

Contributors

hpdx avatar

Watchers

James Cloos avatar alex.xin avatar

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.