弹幕简单实现

最近一个项目要仿照好奇心日报的弹幕做一个差不多一样的,首先想到的是动画,结果做了一个老是跑着跑着就跪了,发现报内存溢出了。
ok,这就尴尬了。发现创建弹幕是出现一条弹幕就创建一条,结果来了一堆,就挂了,后面找了好多都是一条弹幕就创建一条弹幕,这样都会有内存溢出。
于是,想起动画不是可以重复的吗,我把重复次数改成Integer.MAX_VALUE,外面用索引拿数据就OK了。下面是关键代码

 private void createBarrage(String content) {
        int leftMargin = this.getRight() - this.getLeft()
                - this.getPaddingLeft();
        final TextView item = getItemView(content);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        // 计算本条弹幕的topMargin(随机值,但是与屏幕中已有的不重复)
        int verticalMargin = getRandomTopMargin();
        item.setTag(verticalMargin);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.topMargin = verticalMargin;
        ValueAnimator anim = AnimationHelper.createTranslateAnim(item, (Activity) getContext(),
                leftMargin, -this.getMeasuredWidth());
        item.setLayoutParams(params);
//        if (isRepeat)
        anim.setRepeatCount(Integer.MAX_VALUE);
//        else {
//            if (mbarrageInterface != null) {
//                if (mbarrageInterface.getBarrageCount() % linesCount == 0) {
//                    anim.setRepeatCount(mbarrageInterface.getBarrageCount() / linesCount);
//                } else {
//                    anim.setRepeatCount(mbarrageInterface.getBarrageCount() / linesCount + 1);
//                }
//            }
//        }
        anim.setInterpolator(new LinearInterpolator());
        anim.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator arg0) {

            }

            @Override
            public void onAnimationRepeat(Animator arg0) {
//                if (isRepeat) {
                index++;
                if (mbarrageInterface.getBarrageCount() == 0 || index == mbarrageInterface.getBarrageCount() || index > mbarrageInterface.getBarrageCount()) {
                    index = 0;
                }
//                } else {
//                    if (index == mbarrageInterface.getBarrageCount()) {
//                        return;
//                    }
//                }
                if (mbarrageInterface.getBarrageText(index) != null) {
                    item.setText(mbarrageInterface.getBarrageText(index));
                }
            }

            @Override
            public void onAnimationEnd(Animator arg0) {

            }

            @Override
            public void onAnimationCancel(Animator arg0) {

            }
        });
        list.add(anim);
        anim.start();
        this.addView(item);
    }
OK,内存溢出问题解决了。

下面发现又有个问题,好奇心日报的弹幕可以点击的,而我的弹幕点击只能在固定位置点击,他们的哪个位置都可以点。

后面发现,这个平移动画只是文字在移动,属性没跟着一起移动,额,来个属性动画,应该可以的,结果真的就OK了。下面的是属性平移动画关键代码:

 /**
     * 创建平移动画
     */
    public static ValueAnimator createTranslateAnim(View view, Activity context,
                                                    int fromX, int toX) {
        context.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        long duration = (long) (Math.abs(toX - fromX) * 1.0f
                / width * 3000);
        PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder
                .ofFloat("translationX", fromX, toX);
        ValueAnimator valueAnimator = ObjectAnimator.ofPropertyValuesHolder(
                view, propertyValuesHolder).setDuration(duration);
        return valueAnimator;
    }
OK,暂时这样吧!这是demo传送门:https://github.com/xfhomedream/BarrageDemo 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值