1. 程式人生 > >Android 彈幕的實現DanmakuFlameMaster

Android 彈幕的實現DanmakuFlameMaster

 //手動隱藏標題欄
        if(getSupportActionBar() != null){
            getSupportActionBar().hide();
        }


1、新增彈幕依賴

compile'com.github.ctiao:DanmakuFlameMaster:0.7.3'
2、新增許可權,這裡我用了本地的視訊,如果要獲取網路上的視訊,要新增聯網許可權

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

3、在values資料夾下,建立dimens檔案

<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

4、在res下面建立raw資料夾,然後將視訊放進去,到這步,一切準備就緒,下面開始正式上程式碼了
5、activity_main.xml 
RelativeLayout佔全屏
<VideoView
    android:id="@+id/diveo_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_layout" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="#fff" android:visibility="gone" > <EditText android:id="@+id/edit_text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="send" /> </LinearLayout>
6.在MainActivity裡面的程式碼
private VideoView diveo_view;
private DanmakuView danmaku_view;
private LinearLayout operation_layout;
private EditText edit_text;
private Button send;

private boolean showDanmaku;
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);
    //手動隱藏標題
    if(getSupportActionBar() != null){
        getSupportActionBar().hide();
    }

    initView();//控制元件初始化

}

private void initView() {
    //控制元件
    diveo_view = findViewById(R.id.diveo_view);
    danmaku_view = findViewById(R.id.danmaku_view);
    operation_layout = findViewById(R.id.operation_layout);
    edit_text = findViewById(R.id.edit_text);
    send = findViewById(R.id.send);

    //視訊的地址
    diveo_view.setVideoURI(Uri.parse("android.resource://" + getPackageName()
            + "/" + R.raw.mian));
    diveo_view.start();//視訊開始

    //彈幕
    danmaku_view.enableDanmakuDrawingCache(true);
    danmaku_view.setCallback(new DrawHandler.Callback() {
        @Override
        public void prepared() {
            //準備階段
            showDanmaku = true;
            danmaku_view.start();
            generateSomeDanmaku();
        }

        @Override
        public void updateTimer(DanmakuTimer timer) {

        }

        @Override
        public void danmakuShown(BaseDanmaku danmaku) {

        }

        @Override
        public void drawingFinished() {

        }
    });
    danmakuContext = DanmakuContext.create();
    danmaku_view.prepare(parser,danmakuContext);

    //點選螢幕,顯示彈幕輸入框
    //再次點選,隱藏輸入框
    danmaku_view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(operation_layout.getVisibility() == View.GONE){
                operation_layout.setVisibility(View.VISIBLE);
            }else if(operation_layout.getVisibility() == View.VISIBLE){
                operation_layout.setVisibility(View.GONE);
            }
        }
    });

    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //傳送訊息
            addDanmaku(edit_text.getText().toString(),true);
        }
    });

}

/**
 * 隨機生成一些內容測試
 */
private void generateSomeDanmaku() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (showDanmaku){
                int time = new Random().nextInt(300);
                String content = "我是---" + time;
                addDanmaku(content, false);
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

//新增
private void addDanmaku(String content, boolean b) {
    BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
    danmaku.text = content;
    danmaku.padding = 5;
    danmaku.textSize = sp2px(20);
    danmaku.textColor = Color.WHITE;
    danmaku.setTime(danmaku_view.getCurrentTime());
    if (b) {
        danmaku.borderColor = Color.GREEN;
    }
    danmaku_view.addDanmaku(danmaku);
}


//文字的大小
private float sp2px(int value) {
    final float fontScale = getResources().getDisplayMetrics().scaledDensity;
    return (int) (value * fontScale + 0.5f);
}

//如果不加這個方法,彈幕可能會從中間顯示
@Override
protected void onPause() {
    super.onPause();
    if (danmaku_view != null && danmaku_view.isPrepared()) {
        danmaku_view.pause();
    }
}

//如果不加這個方法,彈幕可能會從中間顯示
@Override
protected void onResume() {
    super.onResume();
    if (danmaku_view != null && danmaku_view.isPrepared() && danmaku_view.isPaused()) {
        danmaku_view.resume();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    //釋放彈幕資源
    showDanmaku = false;
    if (danmaku_view != null) {
        danmaku_view.release();
        danmaku_view = null;
    }
}

//Activity一載入完畢,就觸發
@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_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
}

=======end=========
到這裡全部結束