温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Android中实现弹幕效果

发布时间:2021-05-24 18:22:26 来源:亿速云 阅读:173 作者:Leah 栏目:移动开发

怎么在Android中实现弹幕效果?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

首先分析一下,他是由三层布局来共同完成的,第一层视频布局,第二层字幕布局,第三层输入框布局,要想让这三个布局在同一页面上,必须用相对布局或帧布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/activity_main"
  tools:context="com.bwie.danmustudy.MainActivity">
 
  <VideoView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    />
  <master.flame.danmaku.ui.widget.DanmakuView
    android:id="@+id/danmaku_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
  <LinearLayout
    android:id="@+id/operation_text"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:visibility="gone"
    android:background="#fff"
    android:orientation="horizontal"
    >
    <EditText
      android:id="@+id/edit_text"
      android:layout_weight="1"
      android:layout_width="0dp"
      android:layout_height="match_parent" />
    <Button
      android:id="@+id/send"
      android:text="send"
      android:layout_width="wrap_content"
      android:layout_height="match_parent" />
  </LinearLayout>
 
</RelativeLayout>

创建一个弹幕的解析器

public class MainActivity extends AppCompatActivity {
 
  private boolean showDanmaku;
  private DanmakuView danmakuView;
  private DanmakuContext danmakuContext;
  //创建一个弹幕的解析器
  private BaseDanmakuParser parser=new BaseDanmakuParser() {
    @Override
    protected IDanmakus parse() {
      return new Danmakus();
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //播放视频
    VideoView video_view= (VideoView) findViewById(R.id.video_view);
    Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.minion_08);
    video_view.setVideoURI(uri);
    video_view.start();
 
    danmakuView= (DanmakuView) findViewById(R.id.danmaku_view);
    //调用了enableDanmakuDrawingCache()方法来提升绘制效率,也就是绘制速度
    // 又调用了setCallback()方法来设置回调函数。
    danmakuView.enableDanmakuDrawingCache(true);
    danmakuView.setCallback(new DrawHandler.Callback() {
      @Override
      public void prepared() {
        showDanmaku=true;
        danmakuView.start();
      }
 
      @Override
      public void updateTimer(DanmakuTimer timer) {
 
      }
 
      @Override
      public void danmakuShown(BaseDanmaku danmaku) {
 
      }
 
      @Override
      public void drawingFinished() {
 
      }
    });
    danmakuContext=danmakuContext.create();
    //第一个参数是弹幕的解析器
    //调用DanmakuView的prepare()方法来进行准备,准备完成后会自动调用刚才设置的回调函数中的prepared()方法
    danmakuView.prepare(parser,danmakuContext);
    final LinearLayout operationLayout= (LinearLayout) findViewById(R.id.operation_text);
    final Button send= (Button) findViewById(R.id.send);
    final EditText edit_text= (EditText) findViewById(R.id.edit_text);
    danmakuView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        if (operationLayout.getVisibility()==View.GONE){
          operationLayout.setVisibility(View.VISIBLE);
        }else{
          operationLayout.setVisibility(View.GONE);
        }
      }
    });
    send.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        String content=edit_text.getText().toString();
        if (!TextUtils.isEmpty(content)){
          addDanmaku(content,true);
          edit_text.setText("");
        }
      }
    });
  }
 
  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus&& Build.VERSION.SDK_INT>=19){
      View decorView=getWindow().getDecorView();
      decorView.setSystemUiVisibility(
          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=danmakuContext.mDanmakuFactory
        .createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
    danmaku.text=content;
    danmaku.padding=5;
    danmaku.textSize=50;
 
    danmaku.setTime(danmakuView.getCurrentTime());
    if (withBorder){
      danmakuView.addDanmaku(danmaku);
    }
  }

最后使页面横屏展示:

<activity android:name=".MainActivity"
  只需要加这一行代码就可以
   android:screenOrientation="landscape"
   >
  <intent-filter>
     <action android:name="android.intent.action.MAIN" />
 
     <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI