Amndroid 弹幕Barrageview

本文介绍了一个Android自定义视图BarrageView的实现,该视图用于展示弹幕效果。文章详细讲解了如何设置弹幕速度、数据处理以及屏幕适配等关键步骤,帮助开发者理解如何在自己的应用中实现弹幕展示功能。

 一个实现得弹幕VIew

布局文件Activity页面


页面一些参数配置弹幕view添加数据自己写的伪请求过五秒去生成一批假数据,实际项目中可以是请求后台接口,或者后台推送前端接收后进行数据处理##BarrageView##public class BarrageView extends RelativeLayout {
private Random random = new Random(System.currentTimeMillis());
private int MOVE_SPEED_LOW = 2000;//低速
private int MOVE_SPEED_HIGH = 500;//高速

private int DELAY_TIME = 500;

private int TOTAL_COUNT = 0;//弹幕总共个数

private Queue<String> msg_data;//barrageView.setData(queue, this)

private MSGHandler msgHander;

private int temp = 0;

private Context mContext;


private int totalHeight = 0;
private int lineHeight = 0;//每一行弹幕的高度
private int totalLine = 0;//弹幕的行数


public BarrageView(Context context) {
    super(context);
}

public BarrageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public BarrageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);


    init();
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public BarrageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

/**
 * 初始化
 */
private void init() {

// msgHander = new MSGHandler();

}

   //此处拿到数据就行处理
public void setData(Queue datas, Context context) {
if (msgHander == null) {
msgHander = new MSGHandler();
}
msgHander.removeCallbacksAndMessages(null);
msg_data = datas;
TOTAL_COUNT = datas.size();
mContext = context;
//显示弹幕
msgHander.sendEmptyMessageDelayed(0, DELAY_TIME);

}

/**
 * 显示一条消息   //队列存储 先进先出
 */
private void doMSG() {

    if (temp < msg_data.size()) {
        ItemData itemData = new ItemData();
        itemData.text = msg_data.poll();
        temp++;

        itemData.moveSpeed = MOVE_SPEED_LOW;
        TextView textView = new TextView(mContext);
        itemData.textView = textView;
        itemData.textView.setTextSize(30);
        itemData.textView.setTextColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        textView.setText(itemData.text);

        Rect rect = new Rect();
        TextPaint tp = textView.getPaint();
        tp.getTextBounds(itemData.text, 0, itemData.text.length(), rect);
        itemData.textWidth = rect.width();

        if (totalLine == 0) {
            totalHeight = getMeasuredHeight();
            lineHeight = getLineHeight();
            totalLine = totalHeight / lineHeight;
        }

        itemData.vertical = random.nextInt(totalLine) * lineHeight;

        showMsg(itemData);

    } else {

// msgHander.removeCallbacksAndMessages(null);
// return;
temp = 0;//循环

    }


}

private void showMsg(final ItemData item) {
    int leftMargin = this.getRight() - this.getLeft() - this.getPaddingLeft();

    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    params.topMargin = item.vertical;
    this.addView(item.textView, params);
    Animation anim = generateTranslateAnim(item, leftMargin);
    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            item.textView.clearAnimation();
            BarrageView.this.removeView(item.textView);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    item.textView.startAnimation(anim);
}

private TranslateAnimation generateTranslateAnim(ItemData item, int leftMargin) {
    TranslateAnimation anim = new TranslateAnimation(leftMargin, -item.textWidth, 0, 0);
    anim.setDuration(item.moveSpeed);
    anim.setInterpolator(new AccelerateDecelerateInterpolator());
    anim.setFillAfter(true);
    return anim;
}


@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {

//计算弹幕再屏幕最多显示多少行
super.onWindowFocusChanged(hasWindowFocus);
totalHeight = getMeasuredHeight();
lineHeight = getLineHeight();
totalLine = totalHeight / lineHeight;

}

private int getLineHeight() {

    TextView textView = new TextView(mContext);
    textView.setText("AAAAAAAAAAAAA");
    textView.setTextSize(30);

    Rect rect = new Rect();
    TextPaint tp = textView.getPaint();
    tp.getTextBounds("AAAAAAAAAAAAA", 0, msg_data.size(), rect);

    return rect.height();


}

class MSGHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        //显示一条消息
        doMSG();

        msgHander.sendEmptyMessageDelayed(0, DELAY_TIME);


    }
}涉及到得一个modelpublic class ItemData {

public TextView textView;
public int textSize;
public int textColor;
public int moveSpeed;
public int textWidth;
public String text;

public int vertical;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值