preface

In the past few weeks, I have been working on a Barrage library every day after work except watching videos. The idea came from last year when I saw a preview of pictures supporting Barrage in QZone, and I wanted to implement one on a random impulse. Finally, I finished muti-Barrage.

1. Single view barrage

2. Multi-view barrage

PhotoPagerView

A, functionality,

Muti-barrage supports the following features:

  • Customize multiple views
  • Setting the Sending Interval
  • Collision detection (problems with multiple views)
  • Touch event handling
  • Supports full screen and upper, middle and lower display
  • Supports setting playback times and loops

Second, the use of

1. Add dependencies

From build.gradle(Project: XXX) add:

Maven () {url 'https://jitpack.io'}}Copy the code

Add to build. Gradle (Module:app) dependencies:

dependencies { ... */ implementation 'com.jieWang:Muti-Barrage:1.0.4'}Copy the code

2. Use

The use method is a bit similar to RecyclerView, but free customization also caused the rise of the use cost, there is no way ~ the first step: add into the layout file

<com.orient.tea.barragephoto.ui.BarrageView
            android:id="@+id/barrage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
Copy the code

Step 2: Build your own implementation of the barrage data typeDataSourceinterface

public class BarrageData implements DataSource {
    		...
    		@Override
        public int getType() {
            return type; } // If required, which is usually not required, @override public long is later removedgetShowTime() {
            return0; }}Copy the code

Step 3: GetBarrageViewAnd initialize the parameters

private BarrageView barrageView; . barrageView = findViewById(xxx); Barrageview.options Options = new barrageview.options ().setgravity (barrageview.gravity_top) // Set the projectile position. SetInterval (50) SetSpeed (200,29) // set the speed and fluctuation values. SetModel (barrageview.model_collision_detection) // set the random generation of the barrage mode or collision detection .setrepeat (-1) // Loop playback defaults to 1. -1 is an infinite loop.false); // Set barrageView.setoptions (options);Copy the code

Step 4: CreateViewHolderTo realizeBarrageViewHolderInterface (after many need to add the template)

/ / in the multiple view barrage themselves need to build multiple types ViewHolder class ViewHolder extends BarrageAdapter. BarrageViewHolder < BarrageData > {public ViewHolder(View itemView) { super(itemView); } @Override protected void onBind(BarrageData data) { ... }}}}}}}}}Copy the code

Step 5: Set up the adapter To create the adapter, you need to add the stereotype (the data type implemented in Part 2)

private BarrageAdapter<BarrageData> mAdapter;
Copy the code

A single view

barrageView.setAdapter(mAdapter = new BarrageAdapter<BarrageData>(null, this) {
                @Override
                public BarrageViewHolder<BarrageData> onCreateViewHolder(View root, int type) {
                    returnnew SingleBarrageActivity.ViewHolder(root); } @override public int getItemLayout(BarrageData BarrageData) {returnR.layout.barrage_item_normal; // Return your own layout file}});Copy the code

Multiple views are a little trickier (see the sample code)

Barrageview. setAdapter(mAdapter = new BarrageAdapter<BarrageData>(null, this) { @Override public BarrageViewHolder<BarrageData> onCreateViewHolder(View root, inttype) {
                    switch (type) {// heretypeRefers to the child layout file we set, and then set ViewHolder... }} @override public int getItemLayout(BarrageData BarrageData) {switch (barragedata.gettype ()) {// according to the barrageDatatypeSet up the sub-layout file... // Different barrage types return different layout files}}});Copy the code

Barrageview. Options must be clickable in step 3 if you need to set the barrage touch event

/ / set the listener mAdapter. SetAdapterListener (new AdapterListener < BarrageData > () {@ Override public void onItemClick(BarrageAdapter.BarrageViewHolder<BarrageData> holder, BarrageData item) { ... }});Copy the code

Three, update,

If you are interested in how the barrage works:

Teach you to write a barrage library, sure do not understand?

If you find any problems, please comment:

Muti-Barrage:github.com/mCyp/Muti-B…

If you feel good, welcome Star, if you find any problems, welcome to submit issues.