日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

添加弹幕

發布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 添加弹幕 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果圖:

                    

?

1、? 添加依賴

implementation 'com.github.ctiao:DanmakuFlameMaster:0.5.3'// bilibili 開源彈幕庫

2、編寫布局?? DanmakuView? 覆蓋在 VideoView

<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns: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:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:orientation="vertical"><RelativeLayoutandroid:layout_width="400dp"android:layout_height="400dp"><VideoViewandroid:id="@+id/video"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerInParent="true"android:background="@android:color/white"/><master.flame.danmaku.ui.widget.DanmakuViewandroid:id="@+id/DanmakuView"android:layout_width="match_parent"android:layout_height="match_parent" /><LinearLayoutandroid:layout_alignParentBottom="true"android:id="@+id/operation_layout"android:layout_width="match_parent"android:orientation="horizontal"android:layout_height="50dp"><EditTextandroid:id="@+id/et"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><ImageViewandroid:id="@+id/send"android:layout_width="wrap_content"android:layout_height="match_parent"app:srcCompat="@android:drawable/ic_menu_send" /></LinearLayout></RelativeLayout> </LinearLayout>

?

?

?

?

3、 編寫相關邏輯

public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";private boolean showDanmaku;private DanmakuView danmakuView;private DanmakuContext danmakuContext;private BaseDanmakuParser parser = new BaseDanmakuParser() {@Overrideprotected IDanmakus parse() {return new Danmakus();}};/*** 添加彈幕* @param content* @param withBorder*/private void addDanmaku(String content,boolean withBorder){BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_LR);danmaku.text = content;danmaku.padding = 5;danmaku.textSize = 50;//DensityUtil.px2dip(MainActivity.this,100);danmaku.textColor = Color.BLACK;danmaku.setTime(danmakuView.getCurrentTime());if (withBorder){danmaku.borderColor = Color.GREEN;}danmakuView.addDanmaku(danmaku);}/*** 隨機生成彈幕*/private void generateSomeDanmaku() {new Thread(new Runnable() {@Overridepublic void run() {while(showDanmaku){int time = new Random().nextInt(300);String content = ""+time+time;addDanmaku(content,false);try {Thread.sleep(time);} catch (InterruptedException e) {e.printStackTrace();}}}}).start();}/***暫停時 彈幕跟著暫停*/@Overrideprotected void onResume() {super.onResume();if (danmakuView!=null&&danmakuView.isPrepared()){danmakuView.resume();}}/*** 退出時銷毀彈幕*/@Overrideprotected void onDestroy() {super.onDestroy();showDanmaku = false;if (danmakuView!=null){danmakuView.release();// 釋放danmakuView = null;}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);VideoView videoView = findViewById(R.id.video);videoView.setVideoURI(Uri.parse("http://ips.ifeng.com/video.ifeng.com/video04/2011/03/24/480x360_offline20110324.mp4"));videoView.start();danmakuView=findViewById(R.id.DanmakuView);danmakuView.enableDanmakuDrawingCache(true);danmakuView.setCallback(new DrawHandler.Callback() {@Overridepublic void prepared() {showDanmaku = true;danmakuView.start();generateSomeDanmaku();}@Overridepublic void updateTimer(DanmakuTimer timer) { }@Overridepublic void danmakuShown(BaseDanmaku danmaku) { }@Overridepublic void drawingFinished() { }});danmakuContext = DanmakuContext.create();danmakuView.prepare(parser,danmakuContext);final LinearLayout operationLayout = findViewById(R.id.operation_layout);ImageView send = findViewById(R.id.send);final EditText editText = findViewById(R.id.et);danmakuView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (operationLayout.getVisibility()==View.GONE){operationLayout.setVisibility(View.VISIBLE);}else {operationLayout.setVisibility(View.GONE);}}});send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String content = editText.getText().toString();if (!TextUtils.isEmpty(content)){addDanmaku(content,true);editText.setText("");}}});} }

?

轉載于:https://www.cnblogs.com/the-wang/p/9059969.html

總結

以上是生活随笔為你收集整理的添加弹幕的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。