轻量级弹幕,三步即可展示弹幕;也可作为公告的动态展示用途
建议下载主分支类库dmlib2导入项目,由于项目目前并不稳定,并伴随经常修改,短时间不会继续发布远程仓库版本
该项目是为了弃用DMView,因为当时刚刚进入实习用RecyclerView动画实现的,性能很差。后来我离职后又再也没管了,不过有很多朋友又使用了DMView
,本着写出来就要负责的态度,于是写了这个续集。
DMView2
不是单纯的只有文字的弹幕,可图文一起;DMView2
目前通过两种方式实现,SurfaceView
和TextureView
。大家在使用的时候可以自行选择;DMView2
只是展示您设置好的View模板,模板可通过写xml布局,并且可以同时添加多种模板;xml参数 | 值 | 作用 |
---|---|---|
dm_direction |
right_left 、left_right up_down 、down_up |
down_up :弹幕从下往上跑;up_down :弹幕从上往下跑;left_right :弹幕从左往右跑;right_left :弹幕从右往左跑 |
dm_span |
dp |
刷新一次界面弹幕所移动的跨度 |
dm_span_time |
int |
每个跨度需要的时间,单位:ms(毫秒) |
dm_sleep |
int |
|
dm_v_space |
dp |
弹幕之间的垂直方向间距 |
dm_h_space |
dp |
弹幕之间的横向间距 |
注意: dm_span
和dm_sleep
可配合使用,调整弹幕运动速度
build.gradle文件中添加依赖
implementation 'com.github.xujiaji:dmlib2:0.0.6'
在布局xml文件中添加(或使用
DMSurfaceView
):
<com.xujiaji.dmlib2.widget.DMTextureView
android:id="@+id/dmView"
android:layout_width="match_parent"
android:layout_height="200dp"
app:dm_direction="right_left"
app:dm_span="2dp"
app:dm_v_space="8dp"
app:dm_h_space="16dp"
app:dm_span_time="16" />
创建弹幕模板布局,可参考
barrage.xml
announcement_image_text.xml
barrage_down_up.xml
等
获取DMSurfaceView实例,通过add方法添加弹幕,弹幕为步骤2设置好数据的模板。
View view = LayoutInflater.from(this).inflate(R.layout.announcement_text, null);
mDMSurfaceView.getController().add(view);
或者子线程中添加
dmSurfaceView.getController().add(new ViewCreator() {
@Override
public View build() {
return LayoutInflater.from(MainActivity.this).inflate(R.layout.barrage_other, null);
}
});
最好在Activity或Fragment的onPause周期中调用下暂停,如下:
@Override
protected void onPause() {
super.onPause();
dmSurfaceView.getController().pause();
}
该代码只展示了DMSurfaceView,查看DMTexutureView使用请参考 TestTextureActivity.java
Copyright 2018 XuJiaji
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.