This project is an open source bullet-screen control library, which can support a variety of styles of bullet-screen, bullet-screen click monitoring, bullet-screen display by area, custom movement speed and other functions. The principle of the project is to customize ViewGroup. Probably the most powerful lightweight bullet-screen control yet.

Github project address: github.com/hust2010107… , hope you can Star or submit Issues.

The effect

  • General style

  • Click on the event

  • Multiple barrage styles

  • Segmented display

  • GIF rendering

use

0. Add dependencies

1. The importxdanmukuThe source code

You can directly download the xdanmuku module of this project, import the project directory, and add dependencies compile project(‘:xdanmuku’).

2. Gradle

Add the JitPack repository to the Project root build.gradle (Project) file.

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io'}}}Copy the code

Then add dependencies to your project

dependencies {
        compile 'com.github.hust201010701:XDanmuku:33a063b46e'
}Copy the code

Other methods to add dependencies, such as Maven, please go to my view.

1. Add controls

Add controls to the layout XML

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

2. Customize DanmuEntity

The bullet screen entity class can be customized according to requirements, including all the attributes and types of the bullet screen.

public class DanmuEntity {
    public String content;
    public int textColor;
    public int backgroundColor;
    public int type;
    public String time;
}Copy the code

3. Add DanmuConverter

There are two abstract methods to be implemented in DanmuConverter. GetSingleLineHeight is to return the maximum height of all barrage styles as the height of the channel of barrage. Convert is responsible for binding the DanmuEntity class to the subview of the barrage (similar to the getView method of BaseAdapter).

DanmuConverter danmuConverter = new DanmuConverter<DanmuEntity>() {
    @Override
    public int getSingleLineHeight() {
        // Take out the layout of all types of barrage and find the maximum height as the ballistic height
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_danmu, null);
        // Specify the row height
        view.measure(0.0);

        View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_super_danmu, null);
        // Specify the row height
        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());
        }
        returnview; }};Copy the code

4. Add a barrage

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();
}Copy the code

5. Monitoring of bullet screen click events

// Barrage click event
danmuContainerView.setOnItemClickListener(new DanmuContainerView.OnItemClickListener<DanmuEntity>() {
    @Override
    public voidonItemClick(DanmuEntity danmuEntity) { Toast.makeText(MainActivity.this,danmuEntity.content,Toast.LENGTH_SHORT).show(); }});Copy the code

6. Set the movement speed of the barrage

DanmuContainerView has three preset bullet movement speeds:

public final static int LOW_SPEED = 1;
public final static int NORMAL_SPEED = 3;
public final static int HIGH_SPEED = 5;Copy the code

SetSpeed by setSpeed method:

danmuContainerView.setSpeed(DanmuContainerView.HIGH_SPEED);Copy the code

You can also pass specific integer speeds:

danmuContainerView.setSpeed(4);Copy the code

7. Barrage display area

I will danmu control according to the vertical are divided into three parts, respectively GRAVITY_TOP,GRAVITY_CENTER,GRAVITY_BOTTOM. The user is free to combine display areas, GRAVITY_FULL by default. Set to display area by setGravity method, | parameters can be used for connection.

// Only the top and middle areas are displayed
danmuContainerView.setGravity(DanmuContainerView.GRAVITY_TOP | DanmuContainerView.GRAVITY_CENTER);Copy the code

Afterword.

You probably already know how this control works using custom ViewGroups. But BEFORE I spent a lot of events trying to make RecyclerView realize the bullet-screen control through custom LayoutManager, but finally this scheme failed. Please send me email ([email protected]) for more details.

Welcome Star, submit Issues.