RecyclerView实现的弹幕之中文DOC

简介

弹幕动画通过重写RecyclerView的默认动画”DefaultItemAnimator”来实现,更改默认动画过后的文件地址在DmItemAnimator,里面的作用我简单的注释了一下。

Github地址:https://github.com/xujiaji/DMView
如有使用过程中出现bug欢迎私信!
英语不好,英文文档有些地方让人不好理解,也欢迎纠正。

DMView

dmlib类库可以帮助你只需要简单的几步就可以添加展示弹幕,本类库由RecyclerView为基础完成,使用Glide加载头像。

版本

2.0.0

演示

《RecyclerView实现的弹幕之中文DOC》 弹幕GIF演示

安装

将类库添加到您的项目中,只需要在您的项目buil.gradle文件中的dependency{}中添加如下信息:

dependencies {compile 'com.github.xujiaji:dmlib:1.1.1'}

使用(总共四步哦!)

1. 需要在布局中添加RecyclerView(因为以RecyclerView为主体)

<android.support.v7.widget.RecyclerView        
  android:id="@+id/rvBarrage"       
  android:layout_width="match_parent"        
  android:layout_height="match_parent"        
  android:layout_marginTop="90dp"        
  android:layout_marginBottom="90dp"        
  android:overScrollMode="never" />

2.初始化 “DanMu” 在获取了RecyclerView之后

①初始化-方法一 : 使用“dmlib”默认的布局和行数
rvBarrage = (RecyclerView) findViewById(R.id.rvBarrage);
DanMu.init(rvBarrage);
②初始化-方法二: 配置您想要的布局
Config config = new Config(
        R.layout.item,
        R.id.tvName,
        R.id.tvMsg,
        R.id.imgHead);
        config.setRowNum(5);//设置您要设置的行数
        DanMu.init(rvBarrage, config);
  • 这是上面的运行结果,代码请转到sample项目。

    《RecyclerView实现的弹幕之中文DOC》 自定义的布局

  • 备注: 如果您不想要其中一个布局,比如说您不需要名字,那么对应设置成0。

       Config config = new Config( 
       R.layout.item,
        0,
        R.id.tvMsg,
        R.id.imgHead);
③初始化-方法三:设置弹幕的行数
        Config config = new Config();
        config.setRowNum(3);
        DanMu.init(rvBarrage, config);
④初始化-方法四:设置弹幕的展示时间
        Config config = new Config();
        config.setDuration(10000);
        DanMu.init(rvBarrage, config);

3.添加一个弹幕

DanMu.call()        
       .picUrl("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=150237755,4294706681&fm=116&gp=0.jpg") 
       .name("xujiaji") 
       .msg("Bullet screen massage show ...")
       .show();
  • 注意: 如果您使用的是默认布局,那么您也可以通过以下方式展示不同的效果。

No.1

DanMu.call()        
        .picUrl("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=150237755,4294706681&fm=116&gp=0.jpg")
        .msg("Bullet screen massage show ...")
        .show();

《RecyclerView实现的弹幕之中文DOC》 头像和消息

No.2

DanMu.call()
        .msg("Bullet screen massage show ...")
        .show();

《RecyclerView实现的弹幕之中文DOC》 只有消息.png

No.3

javaDanMu.call()
        .name("xujiaji")
        .msg("Bullet screen massage show ...")
        .show();

《RecyclerView实现的弹幕之中文DOC》 名字和消息.png

No.4

DanMu.call()        
        .picUrl("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=150237755,4294706681&fm=116&gp=0.jpg")
        .show();

《RecyclerView实现的弹幕之中文DOC》 只有头像

3. 销毁弹幕

    @Override
    protected void onDestroy() {
        DanMu.destroy();
        super.onDestroy();
    }

Activity中所有的代码

public class MainActivity extends AppCompatActivity {
    private RecyclerView rvBarrage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rvBarrage = (RecyclerView) findViewById(R.id.rvBarrage);
        //sample.1: default init
        DanMu.init(rvBarrage);

        //sample.2: config yourself layout
//        Config config = new Config(
//                R.layout.item,
//                R.id.tvName,
//                R.id.tvMsg,
//                R.id.imgHead);
//        config.setRowNum(5);  // setting bullet screen's rows
//        DanMu.init(rvBarrage, config);

        //sample.3: setting bullet screen's rows
//        Config config = new Config();
//        config.setRowNum(3);
//        DanMu.init(rvBarrage, config);

        //sample.4 setting bullet screen's animator duration
//        Config config = new Config();
//        config.setDuration(10000);
//        DanMu.init(rvBarrage, config);
    }

    public void onAddClick(View view) {
        DanMu.call()
                .picUrl("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=150237755,4294706681&fm=116&gp=0.jpg")
                .name("xujiaji")
                .msg("Bullet screen massage show ...")
                .show();
    }

    @Override
    protected void onDestroy() {
        DanMu.destroy();
        super.onDestroy();
    }
}

“dmlib”更新日志

v2.0.0

更改了动画库,原先是从github上面的一个RecyclerView动画类库里面拉下来的几个文件,如今已经删掉。现在的实现介绍在:本文顶部简介。
添加了设置动画展示时间的功能。
现在可以销毁动画。

v1.1.1

进行了大面积的更改。
修改优化了添加弹屏的模式,并且可以添加自定义布局。
可以设置RecyclerView行数,默认为6行。

v1.0.0

第一版本,仅仅实现了发送弹幕的功能

License

Copyright 2016 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.
    原文作者:灵魂奏响曲
    原文地址: https://www.jianshu.com/p/6649f5239aef
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞