DamakuView.java
7.26 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.cnlive.goldenline.ui.widget;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.cnlive.goldenline.model.DamakuViewinfo;
import java.util.Random;
/**
* Created by Dean.Zhang on 2016-08-24.
*/
public class DamakuView extends RelativeLayout {
private Context mContext;
@NonNull
public BarrageHandler mHandler = new BarrageHandler();
@NonNull
private Random random = new Random(System.currentTimeMillis());
private static final long BARRAGE_GAP_MIN_DURATION = 2000;//两个弹幕的最小间隔时间
private static final long BARRAGE_GAP_MAX_DURATION = 4000;//两个弹幕的最大间隔时间
private int maxSpeed = 10000;//速度,ms
private int minSpeed = 5000;//速度,ms
private int maxSize = 20;//文字大小,dp
private int minSize = 10;//文字大小,dp
private int totalHeight = 0;
private int lineHeight = 0;//每一行弹幕的高度
private int totalLine = 0;//弹幕的行数
@NonNull
private String[] itemText = {"抢沙发咯!!!"};
private int textCount;
// private List<BarrageItem> itemList = new ArrayList<BarrageItem>();
public DamakuView(Context context) {
this(context, null);
}
public DamakuView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DamakuView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init();
}
private void init() {
textCount = itemText.length;
int duration = (int) ((BARRAGE_GAP_MAX_DURATION - BARRAGE_GAP_MIN_DURATION) * Math.random());
mHandler.sendEmptyMessageDelayed(0, duration);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
totalHeight = getMeasuredHeight();
lineHeight = getLineHeight();
totalLine = totalHeight / lineHeight;
}
/** 获取外部传入的数据 */
public void getData(@NonNull String txt, @Nullable String col, String sz){
Log.i("TSM","接收到的弹幕1"+"-----"+txt+"----"+col+"-----"+sz);
int size=Integer.parseInt(sz);
if (col==null || col.equals("0")){ generateItem(txt,size,255,255,255); }else {
int color=Integer.parseInt(col);
switch (color){
//case 0: generateItem(txt,size,255,255,255); break; //255.255.255
case 1: generateItem(txt,size,255,102,51); break; //255.102.51
case 2: generateItem(txt,size,255,153,1); break; // 255.153.1
case 3: generateItem(txt,size,255,255,0); break; //255.255.0
case 4: generateItem(txt,size,0,255,16); break; //0.255.16
case 5: generateItem(txt,size,0,255,255); break; //0.255.255
case 6: generateItem(txt,size,0,140,238); break; //0.140.238
case 7: generateItem(txt,size,134,0,255); break; //134.0.255
}
}
}
private void generateItem(@NonNull String txt, int size, int R, int G, int B) {
Log.i("TSM","接收到的弹幕2"+"-----"+txt+"----"+size+"-----"+R+"-----"+G+"--------"+B);
DamakuViewinfo item = new DamakuViewinfo();
item.textView = new TextView(mContext);
item.textView.setText(txt);
item.textView.setTextSize(size);
item.textView.setTextColor(Color.rgb(R,G,B));
item.textMeasuredWidth = (int) getTextWidth(item, txt, size);
item.moveSpeed = (int) (minSpeed + (maxSpeed - minSpeed) * Math.random());
if (totalLine == 0) {
totalHeight = getMeasuredHeight();
lineHeight = getLineHeight();
totalLine = totalHeight / lineHeight;
}
item.verticalPos = random.nextInt(totalLine) * lineHeight;
// itemList.add(item);
showBarrageItem(item);
}
private void showBarrageItem(@NonNull final DamakuViewinfo 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.verticalPos;
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();
DamakuView.this.removeView(item.textView);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
item.textView.startAnimation(anim);
}
@NonNull
private TranslateAnimation generateTranslateAnim(@NonNull DamakuViewinfo item, int leftMargin) {
TranslateAnimation anim = new TranslateAnimation(leftMargin, -item.textMeasuredWidth, 0, 0);
anim.setDuration(item.moveSpeed);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.setFillAfter(true);
return anim;
}
/**
* 计算TextView中字符串的长度
*
* @param text 要计算的字符串
* @param Size 字体大小
* @return TextView中字符串的长度
*/
public float getTextWidth(@NonNull DamakuViewinfo item, @NonNull String text, float Size) {
Rect bounds = new Rect();
TextPaint paint;
paint = item.textView.getPaint();
paint.getTextBounds(text, 0, text.length(), bounds);
return bounds.width();
}
/**
* 获得每一行弹幕的最大高度
*
* @return
*/
private int getLineHeight() {
DamakuViewinfo item = new DamakuViewinfo();
String tx = itemText[0];
item.textView = new TextView(mContext);
item.textView.setText(tx);
item.textView.setTextSize(maxSize);
Rect bounds = new Rect();
TextPaint paint;
paint = item.textView.getPaint();
paint.getTextBounds(tx, 0, tx.length(), bounds);
return bounds.height();
}
class BarrageHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//generateItem(itemText[(int) (Math.random() * textCount)],(int) (minSize + (maxSize - minSize) * Math.random()),255,255,255); ////random.nextInt(R), random.nextInt(G), random.nextInt(B))
//每个弹幕产生的间隔时间随机
int duration = (int) ((BARRAGE_GAP_MAX_DURATION - BARRAGE_GAP_MIN_DURATION) * Math.random());
this.sendEmptyMessageDelayed(0, duration);
}
}
}