SoFunction
Updated on 2025-03-11

Android simply realizes barrage effect

This article shares the specific code for Android to realize the barrage effect for your reference. The specific content is as follows

First of all, let’s analyze it. It is done by three-layer layouts, including the first layer of video layout, the second layer of subtitle layout, and the third layer of input frame layout. If you want these three layouts to be on the same page, you must use relative layout or frame layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
  xmlns:app="/apk/res-auto"
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:
  tools:context="">
 
  <VideoView
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    />
  <
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
  <LinearLayout
    android:
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:visibility="gone"
    android:background="#fff"
    android:orientation="horizontal"
    >
    <EditText
      android:
      android:layout_weight="1"
      android:layout_width="0dp"
      android:layout_height="match_parent" />
    <Button
      android:
      android:text="send"
      android:layout_width="wrap_content"
      android:layout_height="match_parent" />
  </LinearLayout>
 
</RelativeLayout>

Create a barrage parser

public class MainActivity extends AppCompatActivity {
 
  private boolean showDanmaku;
  private DanmakuView danmakuView;
  private DanmakuContext danmakuContext;
  //Create a barrage parser  private BaseDanmakuParser parser=new BaseDanmakuParser() {
    @Override
    protected IDanmakus parse() {
      return new Danmakus();
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    //Play video    VideoView video_view= (VideoView) findViewById(.video_view);
    Uri uri=("://"+getPackageName()+"/"+.minion_08);
    video_view.setVideoURI(uri);
    video_view.start();
 
    danmakuView= (DanmakuView) findViewById(.danmaku_view);
    //The enableDanmakuDrawingCache() method is called to improve the drawing efficiency, that is, the drawing speed    // The setCallback() method is called again to set the callback function.    (true);
    (new () {
      @Override
      public void prepared() {
        showDanmaku=true;
        ();
      }
 
      @Override
      public void updateTimer(DanmakuTimer timer) {
 
      }
 
      @Override
      public void danmakuShown(BaseDanmaku danmaku) {
 
      }
 
      @Override
      public void drawingFinished() {
 
      }
    });
    danmakuContext=();
    //The first parameter is the parser of the barrage    //Call the prepare() method of DanmakuView to prepare. After the preparation is completed, the prepared() method in the callback function just set will be automatically called.    (parser,danmakuContext);
    final LinearLayout operationLayout= (LinearLayout) findViewById(.operation_text);
    final Button send= (Button) findViewById();
    final EditText edit_text= (EditText) findViewById(.edit_text);
    (new () {
      @Override
      public void onClick(View view) {
        if (()==){
          ();
        }else{
          ();
        }
      }
    });
    (new () {
      @Override
      public void onClick(View view) {
        String content=edit_text.getText().toString();
        if (!(content)){
          addDanmaku(content,true);
          edit_text.setText("");
        }
      }
    });
  }
 
  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
    (hasFocus);
    if (hasFocus&amp;&amp; .SDK_INT&gt;=19){
      View decorView=getWindow().getDecorView();
      (
          View.SYSTEM_UI_FLAG_LAYOUT_STABLE
              |View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
              |View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
              |View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
              |View.SYSTEM_UI_FLAG_FULLSCREEN
              |View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
      );
    }
  }
  private void addDanmaku(String content,boolean withBorder){
    BaseDanmaku danmaku=
        .createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
    =content;
    =5;
    =50;
 
    (());
    if (withBorder){
      (danmaku);
    }
  }

Finally, the page is displayed horizontally:

&lt;activity android:name=".MainActivity"
  Just add this line of code
   android:screenOrientation="landscape"
   &gt;
  &lt;intent-filter&gt;
     &lt;action android:name="" /&gt;
 
     &lt;category android:name="" /&gt;
   &lt;/intent-filter&gt;
&lt;/activity&gt;

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.