Android弹幕的实现

前言: 一直想写博客,但不知道写点什么,就先随便写写吧,然后最近学习了弹幕的实现方式,并且想把MVP模式再熟练的运用一下。所以通过MVP的模式写了个小DEMO,各位看官多多包涵(关键是里面一些问题请多多指教)。话不多说,进入正题吧。
**一:弹幕的实现**
 1、一个包涵弹幕信息的View
 2、将View动态的添加到当前页面布局里面
 3、将View动起来,当View移动到屏幕边缘的时候,从当前页面布局里面移除
 **二:怎么用MVP模式实现**
 1、新建一个弹幕View的实体(BarrageItemEntity),新建Modle接口类,其中包括一个添加数据接口(返回值为BarrageItemEntity类)。(疑问:本人对MVP理解比较浅,这里使用Modle来获取处理数据,不知道Modle是仅仅指实体类还是应该来获取数据,还望大神指教)
 2、新建一个View接口,包括显示弹幕方法和隐藏弹幕的方法
 3、新建一个Presenter,里面包括一个有参构造函数(为了将M和V对象注入进来,一个显示弹幕的方法和一个隐藏弹幕的方法)
 4、实现Modle和View接口

接下来展示一下具体的代码:

import android.view.View;

/**
 * Created by 275073 on 2017/2/13.
 */
public class BarrageItemEntity {
    //显示的view,
    private View view;
    //移动速度
    private int moveSpeed;
    //垂直方向显示的位置
    private int verticalPos;
    //view占据的宽度
    private int textMeasureWidth;
    .....
}

里面的几个属性,移动速度是到时候决定在屏幕上面的移动速度的,垂直方向显示的位置会生成一个随机数,决定弹幕在屏幕最左侧出现的具体位置,textMeasureWidth,用来判断当前弹幕是否已经移动到屏幕最右侧,好决定什么时候应该移除这条弹幕

public class BarrageModleIm implements BarrageModle {
    Context context;

    //构造方法
    public BarrageModleIm(Context context) {
        this.context = context;
    }

    //实现返回的数据的具体方法
    BarrageItemEntity entity;

    @Override
    public BarrageItemEntity addBarrageItemEntity() {
        //每一次获取的时候新建一个对象
        entity = new BarrageItemEntity();
//        entity.setView(context.findViewById(R.id.barrage_text_view));
        LayoutInflater inflater = LayoutInflater.from(context);
        //找到这个view对象,待会我们要显示的 滚动播放的
        View view = inflater.inflate(R.layout.barrage_itrm, null);
        TextView tx = (TextView)view.findViewById(R.id.barrage_text_view);
        tx.setText("这是第一个弹幕 哈哈哈哈");
        tx.setTextColor(Color.RED);
        entity.setView(view);
        entity.setMoveSpeed(5000);
        //view的宽度 决定系统判断什么时候 动画完成
        entity.setTextMeasureWidth(400);
        //弹幕出现的位置,先来个随机数吧
        int x = (int)(Math.random()*200);
        entity.setVerticalPos(x);
        return entity;
    }

这里当时为了偷懒将view的宽度写死了,并且只返回了一个entity,这里应该返回一个list集合的。至于view的样式可以自己决定。

View的实现类
 @Override
    public void showABarrage(BarrageItemEntity barrageItemEntity) {
        barrageItemEntity = modle.getBarrageItemEntity();
        final View view = barrageItemEntity.getView();
        //设置要显示的弹幕的一些属性
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.topMargin = barrageItemEntity.getVerticalPos();
        params.bottomMargin = 100;
        params.rightMargin = 0;
        //将弹幕添加到布局里面去
        this.addContentView(barrageItemEntity.getView(), params);
        //速度
        Animation anim = generateTranslateAnim(barrageItemEntity, 1000);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }
           //移动完毕的时候
            @Override
            public void onAnimationEnd(Animation animation) {
                view.clearAnimation();
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(anim);
    }
    private TranslateAnimation generateTranslateAnim(BarrageItemEntity barrageItemEntity, int leftMargin) {
        TranslateAnimation anim = new TranslateAnimation(leftMargin, -barrageItemEntity.getTextMeasureWidth(), barrageItemEntity.getVerticalPos(), barrageItemEntity.getVerticalPos());
        anim.setDuration(barrageItemEntity.getMoveSpeed());
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.setFillAfter(true);
        return anim;
    }

这里主要是动态添加view到当前布局,然后给这个弹幕设置了一个动画,在动画结束的时候移除弹幕view。
最后,在activity里面需要用到弹幕的地方调用presenter的presenter.showBarrage();方法就可以实现弹幕啦啦啦啦。

博客新人,还请各位大神多多提出意见,尤其是这个MVP的。我也只是自己学习,然后写了几个demo练练手,没有大牛带,还希望大牛可以多多提出意见

猜你喜欢

转载自blog.csdn.net/l13020227067/article/details/61918318