public class MainActivity extends AppCompatActivity {
private Button btn;
private DanmakuView danmaku;
private DanmakuContext danmakuContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
danmaku = (DanmakuView) findViewById(R.id.danmaku);
danmakuContext = DanmakuContext.create();
danmaku.prepare(new BaseDanmakuParser() {
@Override
protected IDanmakus parse() {
return new Danmakus();
}
},danmakuContext);
danmaku.setCallback(new DrawHandler.Callback() {
@Override
public void prepared() {
danmaku.start();
}
@Override
public void updateTimer(DanmakuTimer timer) {
}
@Override
public void danmakuShown(BaseDanmaku danmaku) {
}
@Override
public void drawingFinished() {
}
});
}
public void send(View view) {
BaseDanmaku baseDanmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
baseDanmaku.text = "这是一条弹幕"+System.currentTimeMillis() ;
baseDanmaku.padding = 5;
baseDanmaku.priority = 1; // 可能会被各种过滤器过滤并隐藏显示
baseDanmaku.isLive = true;
baseDanmaku.textSize = 25f;
baseDanmaku.setTime(danmaku.getCurrentTime()+1200);
baseDanmaku.textColor = Color.RED;
baseDanmaku.textShadowColor = Color.WHITE;
baseDanmaku.underlineColor = Color.GREEN;
baseDanmaku.borderColor = Color.GREEN;
danmaku.addDanmaku(baseDanmaku);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:id="@+id/btn"
android:text="send"
android:onClick="send"
android:layout_height="wrap_content"></Button>
<master.flame.danmaku.ui.widget.DanmakuView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/danmaku"
></master.flame.danmaku.ui.widget.DanmakuView>
</LinearLayout>
附上依赖
implementation 'com.github.ctiao:DanmakuFlameMaster:0.9.25'
另一个build
maven { url 'https://jitpack.io' }
maven { url 'https://repo1.maven.org/maven2/' }