简单的弹幕效果
一、依赖
implementation 'com.github.ctiao:DanmakuFlameMaster:0.7.3'//弹幕库依赖
二、使用步骤
1.xml
代码如下(示例):
<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:background="#DDCACA"
tools:context=".DanMuActivity">
<master.flame.danmaku.ui.widget.DanmakuView
android:id="@+id/danmu_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="#ffffff"
android:visibility="gone">
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:hint="评论" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="发送" />
</LinearLayout>
</RelativeLayout>
2.Ui
代码如下(示例):
public class DanMuActivity extends AppCompatActivity {
private DanmakuView mDanmakuView;
private LinearLayout mOperationLayout;
private EditText mText;
private Button send;
private DanmakuContext danmakuContext;
private final BaseDanmakuParser parser = new BaseDanmakuParser() {
@Override
protected IDanmakus parse() {
return new Danmakus();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dan_mu);
initView();
initDanmaku();
}
private void initView() {
mDanmakuView = (DanmakuView) findViewById(R.id.danmu_view);
mOperationLayout = (LinearLayout) findViewById(R.id.operation_layout);
mText = (EditText) findViewById(R.id.edit_text);
send = (Button) findViewById(R.id.send);
mDanmakuView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOperationLayout.getVisibility() == View.GONE) {
mOperationLayout.setVisibility(View.VISIBLE);
} else {
mOperationLayout.setVisibility(View.GONE);
}
}
});
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = mText.getText().toString();
if (!TextUtils.isEmpty(data)) {
addDanmaku(data);
mText.setText("");
}
}
});
}
/**
* 初始化弹幕组件
*/
private void initDanmaku() {
//给弹幕视图设置回调,在准备阶段获取弹幕信息并开始
mDanmakuView.setCallback(new DrawHandler.Callback() {
@Override
public void prepared() {
mDanmakuView.start();
}
@Override
public void updateTimer(DanmakuTimer timer) {
}
@Override
public void danmakuShown(BaseDanmaku danmaku) {
}
@Override
public void drawingFinished() {
}
});
//缓存,提升绘制效率
mDanmakuView.enableDanmakuDrawingCache(true);
//DanmakuContext主要用于弹幕样式的设置
danmakuContext = DanmakuContext.create();
// danmakuContext.setDanmakuStyle(IDisplayer.DANMAKU_STYLE_STROKEN, 3);//描边
// danmakuContext.setDuplicateMergingEnabled(true);//重复合并
// danmakuContext.setScrollSpeedFactor(1.2f);//弹幕滚动速度
// //让弹幕进入准备状态,传入弹幕解析器和样式设置
mDanmakuView.prepare(parser, danmakuContext);
// //显示fps、时间等调试信息
// mDanmakuView.showFPS(true);
}
/**
* 向弹幕View中添加一条弹幕
*
* @param content 弹幕的具体内容
*/
private void addDanmaku(String content) {
//弹幕实例BaseDanmaku,传入参数是弹幕方向
BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
danmaku.text = content;
danmaku.padding = 5;
danmaku.textSize = 50;
danmaku.textColor = Color.WHITE;
danmaku.setTime(mDanmakuView.getCurrentTime());
//加边框
danmaku.borderColor = Color.GREEN;
mDanmakuView.addDanmaku(danmaku);
}
}