Giter Site home page Giter Site logo

xdanmuku's Introduction

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:33a063b46e'
}

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

1. 添加控件

在布局xml中添加控件

<com.orzangleli.xdanmuku.DanmuContainerView
    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绑定到弹幕子视图上(类似于BaseAdaptergetView方法的作用)。

DanmuConverter danmuConverter = new DanmuConverter<DanmuEntity>() {
    @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<DanmuEntity>() {
    @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实现弹幕控件,不过最终这种方案失败了,更多细节讨论欢迎发送邮件([email protected])给我。

欢迎Star,提交Issues。

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

Watchers

James Cloos avatar SuperChao 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.