privateVideoView diveo_view;
privateDanmakuView danmaku_view;
privateLinearLayout operation_layout;
privateEditText edit_text;
privateButton send;
private booleanshowDanmaku;
privateDanmakuContext danmakuContext;
privateBaseDanmakuParser parser= newBaseDanmakuParser() {
@OverrideprotectedIDanmakus parse() {
return newDanmakus();
}
};
@Overrideprotected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//手动隐藏标题if(getSupportActionBar() != null){
getSupportActionBar().hide();
}
initView();//控件初始化}
private voidinitView() {
//控件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(newDrawHandler.Callback() {
@Overridepublic voidprepared() {
//准备阶段showDanmaku= true;
danmaku_view.start();
generateSomeDanmaku();
}
@Overridepublic voidupdateTimer(DanmakuTimer timer) {
}
@Overridepublic voiddanmakuShown(BaseDanmaku danmaku) {
}
@Overridepublic voiddrawingFinished() {
}
});
danmakuContext= DanmakuContext.create();
danmaku_view.prepare(parser,danmakuContext);
//点击屏幕,显示弹幕输入框//再次点击,隐藏输入框danmaku_view.setOnClickListener(newView.OnClickListener() {
@Overridepublic voidonClick(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(newView.OnClickListener() {
@Overridepublic voidonClick(View view) {
//发送消息addDanmaku(edit_text.getText().toString(),true);
}
});
}
/*** 随机生成一些内容测试*/private voidgenerateSomeDanmaku() {
newThread(newRunnable() {
@Overridepublic voidrun() {
while(showDanmaku){
inttime = newRandom().nextInt(300);
String content = "我是---"+ time;
addDanmaku(content, false);
try{
Thread.sleep(time);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
//添加private voidaddDanmaku(String content, booleanb) {
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 floatsp2px(intvalue) {
final floatfontScale = getResources().getDisplayMetrics().scaledDensity;
return(int) (value * fontScale + 0.5f);
}
//如果不加这个方法,弹幕可能会从中间显示@Overrideprotected voidonPause() {
super.onPause();
if(danmaku_view!= null&& danmaku_view.isPrepared()) {
danmaku_view.pause();
}
}
//如果不加这个方法,弹幕可能会从中间显示@Overrideprotected voidonResume() {
super.onResume();
if(danmaku_view!= null&& danmaku_view.isPrepared() && danmaku_view.isPaused()) {
danmaku_view.resume();
}
}
@Overrideprotected voidonDestroy() {
super.onDestroy();
//释放弹幕资源showDanmaku= false;
if(danmaku_view!= null) {
danmaku_view.release();
danmaku_view= null;
}
}
//Activity一加载完毕,就触发@Overridepublic voidonWindowFocusChanged(booleanhasFocus) {
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);
}
}