服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android自定义View实现弹幕效果

Android自定义View实现弹幕效果

2022-11-09 15:32BrcLi Android

这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在很多视频直播中都有弹幕功能,而安卓上没有简单好用的弹幕控件,本文介绍一个自定义弹幕view的demo。

效果图:

Android自定义View实现弹幕效果

思路:

1、自定义Textitem类表示弹幕的信息
2、自定义view继承view,使用ArrayList保存每条Textitem
3、随机生成坐标点绘制每条TextItem,不断变换Text的横坐标实现弹幕的滚动

首先创建弹幕类,弹幕包括坐标,颜色,滚动速度,以及文字内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Textitem {
 private String content;
 private float fx;
 private float fy;
 private float perstep;
 private int textcolor;
 
 public Textitem(String content,float fx,float fy,float perstep,int textcolor){
  this.content = content;
  this.fx = fx;
  this.fy = fy;
  this.perstep = perstep;
  this.textcolor = textcolor;
 }
 
 public String getContent(){
  return content;
 }
 
 public void setContent(String content){
  this.content = content;
 }
 
 public int getTextcolor(){
  return textcolor;
 }
 
 public void setTextcolor(int textcolor){
  this.textcolor = textcolor;
 }
 
 public float getFx(){
   return fx;
 }
 
 public void setFx(float fx){
  this.fx = fx;
 }
 
 public float getFy(){
  return fy;
 }
 
 public void setFy(float fy){
  this.fy = fy;
 }
 
 public float getPerstep(){
  return perstep;
 }
 
 public void setPerstep(){
  fx -= perstep;
 }
}

接下来自定义View,弹幕横坐标不断变换,需要实现定时刷新界面,重新绘制text。所以实现了Runable接口,在构造方法中开启线程,不断循环,每600毫秒刷新界面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class barrageview extends View implements Runnable{
 
 private List<Textitem> items = new ArrayList<>();
 Random random = new Random();
 private Paint paint;
 
 public barrageview(Context context) {
  super(context);
  initpaint();
  new Thread(this).start();
 }
 
 public barrageview(Context context, AttributeSet attrs) {
  super(context, attrs);
  initpaint();
  new Thread(this).start();
 }
 
 public barrageview(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  initpaint();
  new Thread(this).start();
 }
 
 
 public void addTextitem(String content){
  float x = random.nextFloat()*getWidth();
  float y = Math.abs(random.nextFloat()*(getHeight()-50))+40;
  float step = random.nextFloat()*50;
  int r = random.nextInt(255);
  int g = random.nextInt(255);
  int b = random.nextInt(255);
  Textitem item = new Textitem(content,x,y,step, Color.rgb(r,g,b));
  items.add(item);
 }
 
 public void initpaint(){
  paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
  paint.setColor(Color.RED);
  paint.setTextSize(30);
 }
 
 @Override
 public void draw(Canvas canvas) {
  super.draw(canvas);
  for(Textitem item:items){
   paint.setColor(item.getTextcolor());
   canvas.drawText(item.getContent(),item.getFx(),item.getFy(),paint);
  }
 }
 
 @Override
 public void run() {
  while(true){
   try{
    Thread.sleep(600);
    for(Textitem item:items){
     item.setPerstep();
    }
    postInvalidate();
   } catch (InterruptedException e){
    e.printStackTrace();
   }
  }
 }
}

弹幕VIew就是不断从ArrayList中获取弹幕进行绘制,由于在其他线程进行刷新,所以使用postInvalidate进行重绘。

由于只是实现demo,很多问题没有考虑,存在问题:

弹幕离开屏幕后没有进行清除,使得ArrayList不断扩大,可以进行一个判断,若Textitem的绘制区域不在屏幕内则删掉此item
弹幕若没有交互需求,可以使用Surfaceview进行绘制,SurfaceView可以在子线程更新UI,多缓存机制也可以避免画面跳动
另外注意下自定义View的构造函数的调用时机:

public View(Context context)是在java代码创建视图直接通过new方法创建的时候被调用,
public View(Context context, Attributeset attrs)是在xml创建但是没有指定style的时候被调用
public View(Context Context,AttributeSet attrs, int defStyle)给View提供一个基本的style,没有对View设置属性就使用style中的属性

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/BrcLi/article/details/81414184

延伸 · 阅读

精彩推荐