Amndroid 弹幕Barrageview

xiaoxiao2021-02-28  65

 一个实现得弹幕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;

}

转载请注明原文地址: https://www.6miu.com/read-82454.html

最新回复(0)