Use b Station Open Source Bullet Curtain Engine to Achieve Cool Bullet Curtain Effect

Keywords: github xml Android JSON

Reprinted please indicate the source
Author: AboutJoke( http://blog.csdn.net/u013200308 )
Links to the original text: http://blog.csdn.net/u013200308/article/details/57123100

Now the major video websites have a bullet screen function, but the best display effect is the B station. If you want to have the same cool barrage effect as station b, then follow me step by step.

First put the address

https://github.com/Bilibili/DanmakuFlameMaster

It's easy to use.

repositories {
    jcenter()
}

dependencies {
    compile 'com.github.ctiao:DanmakuFlameMaster:0.6.4'
    compile 'com.github.ctiao:ndkbitmap-armv7a:0.6.4'

    # Other ABIs: optional
    compile 'com.github.ctiao:ndkbitmap-armv5:0.6.4'
    compile 'com.github.ctiao:ndkbitmap-x86:0.6.4'
}

At this point, we import the library into as, and then the real use.

<master.flame.danmaku.ui.widget.DanmakuView
        android:id="@+id/danmaku_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

The layout used to carry the screen display can be implemented with the layout. Some methods are also annotated.

    //Initialization of Barrage Layer
    public void initDanmaku() {
        mDanmakuContext = DanmakuContext.create();
        HashMap<Integer, Integer> maxLine = new HashMap<>();
        maxLine.put(BaseDanmaku.TYPE_SCROLL_RL, 3);// Rolling Barrage Maximum Display 3 Lines
        // Set whether overlap is prohibited
        HashMap<Integer, Boolean> overlappingEnablePair = new HashMap<Integer, Boolean>();
        overlappingEnablePair.put(BaseDanmaku.TYPE_SCROLL_RL, true);
        overlappingEnablePair.put(BaseDanmaku.TYPE_FIX_TOP, true);
        mDanmakuContext.setDanmakuStyle(IDisplayer.DANMAKU_STYLE_STROKEN, 3)//Set up the stroke style
                .setDuplicateMergingEnabled(false)//Whether to Enable Merged Duplicate Barrage
                .setScrollSpeedFactor(1.2f) //Setting the rolling velocity coefficient of the barrage is only effective for the rolling barrage.
                .setScaleTextSize(1.2f)//Setting font zooming
                .setMaximumLines(maxLine)//Set the maximum number of display rows
                .preventOverlapping(overlappingEnablePair);//Overlapping of Bulletproof Curtain
        if (mDanmakuView != null) {
            mDanmakuView.setCallback(new DrawHandler.Callback() {
                @Override
                public void prepared() {
                    //Start playing the screen
                    mDanmakuView.start();
                }

                @Override
                public void updateTimer(DanmakuTimer timer) {

                }

                @Override
                public void danmakuShown(BaseDanmaku danmaku) {

                }

                @Override
                public void drawingFinished() {

                }
            });

            //Barrage parser, if you don't want to use xml format, you can define or default
//            mParser = new BaseDanmakuParser() {
//                @Override
//                protected IDanmakus parse() {
//                    return new Danmakus();
//                }
//            };

            mParser = createParser(this.getResources().openRawResource(R.raw.comments));
            mDanmakuView.showFPS(true);//Display fps
            mDanmakuView.enableDanmakuDrawingCache(true);//Show Bullet Curtain Drawing Buffer
            mDanmakuView.prepare(mParser, mDanmakuContext);
        }
    }

The barrage of station B is saved in xml format, while the barrage of station a is saved in json format. If you are ready to use xml format, you can use the parsing method of station b, or if you don't use either format, you can use the default barrage parser.

mParser = new BaseDanmakuParser() {
                @Override
                protected IDanmakus parse() {
                    return new Danmakus();
                }
            };

Next you need to create a barrage parser to parse the different formats of the barrage. As mentioned earlier, if your barrage format is xml or json, you need this step.

//Create parser objects and parse input streams
    private BaseDanmakuParser createParser(InputStream stream) {

        if (stream == null) {
            return new BaseDanmakuParser() {

                @Override
                protected Danmakus parse() {
                    return new Danmakus();
                }
            };
        }

        //Danmaku LoaderFactory. create (Danmaku LoaderFactory. TAG_BILI) XML parsing
        //Danmaku LoaderFactory. create (Danmaku LoaderFactory. TAG_ACFUN) JSON file format parsing

        ILoader loader = DanmakuLoaderFactory.create(DanmakuLoaderFactory.TAG_BILI);

        try {
            loader.load(stream);
        } catch (IllegalDataException e) {
            e.printStackTrace();
        }
        BaseDanmakuParser parser = new BiliDanmukuParser();
        IDataSource<?> dataSource = loader.getDataSource();
        parser.load(dataSource);
        return parser;

    }

The main function of the barrage parser is to accept the barrage files from the server and display them one by one after parsing. Then we need a separate method to send out the barrage. In this step, you can customize the sent barrage individually.

//Add barrage
    public void addDanmaku(boolean islive, String msg, boolean isUs) {
        BaseDanmaku danmaku = mDanmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        if (danmaku == null || mDanmakuView == null) {
            return;
        }
        danmaku.text = msg;//Barrage content
        danmaku.padding = 5;
        danmaku.priority = 1;//0 means that it may be filtered by various filters and hide display 1 means that it will be displayed, generally used for local sending of bullet curtain
        danmaku.isLive = islive; //Is it a live barrage?
        danmaku.setTime(mDanmakuView.getCurrentTime() + 1200); //Display time
        danmaku.textSize = 18f * (mParser.getDisplayer().getDensity() - 0.6f); //font size
        danmaku.textColor = Color.WHITE;
        danmaku.textShadowColor = Color.parseColor("#333333");// Shadow color to prevent white fonts from being invisible against a white background
        if (isUs)
            danmaku.borderColor = Color.YELLOW; //For the bullet curtain sent by oneself, it can be displayed in a box, 0 means no border.
        mDanmakuView.addDanmaku(danmaku);
    }

With the method of sending the barrage, of course, we have to have the layout of receiving the barrage. Here I use Popup Windows to achieve, you can use the way you are accustomed to. Let's look at the layout we want to achieve.

From the picture, you can see that the right side has a number of signs, that is because I have to achieve a limit on the number of captions, if you send too many captions at a time, it will inevitably affect the viewing. The word limit is also very simple, you can use EditText's TextWatcher to achieve, if not, you can go to the source code to see. So we can play and send the bullet curtain. Let's see the effect quickly, but before we see the effect, we have to write a few more lines of code, mDanmakuView.release(); don't forget to release resources in onDestroy() and onBackPressed(). Okay, let's see the effect.

The recording effect looks a little bad, but the real effect is great. Attach the source address:

https://github.com/AboutJoke/DanmuDemo

Posted by crees on Fri, 05 Apr 2019 10:30:32 -0700