-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextSurfaceView.java
More file actions
496 lines (440 loc) · 13.2 KB
/
TextSurfaceView.java
File metadata and controls
496 lines (440 loc) · 13.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/**
* Created by monkey on 17/1/12.
*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TextSurfaceView extends SurfaceView implements Callback {
/**
* 是否滚动
*/
private boolean isMove = true;
/**
* 移动方向
*/
private int orientation = 0;
/**
* 向左移动
*/
public final static int MOVE_LEFT = 0;
/**
* 向右移动
*/
public final static int MOVE_RIGHT = 1;
/**
* 向上移动
*/
public final static int MOVE_TOP = 2;
/**
* 向下移动
*/
public final static int MOVE_BOTTOM = 3;
/**
* static state
*/
private int staticOri = 0;
public final static int STATIC_LEFT = 1;
public final static int STATIC_CENTER = 0;
public final static int STATIC_RIGHT = 2;
/**
* 移动速度
*/
private long speed = 100;
/**
* 字幕内容
*/
private String content = "";
/**
* 字幕背景色
*/
private int bgColor = Color.BLACK;
/**
* 字幕透明度 默认:255
*/
private int bgalpha = 255;
/**
* 字体颜色 默认:白色 (#FFFFFF)
*/
private int fontColor = Color.RED;
/**
* 字体透明度 默认:不透明(255)
*/
private int fontAlpha = 255;
/**
* 字体大小 默认:30
*/
private float fontSize = 30f;
/**
* 容器
*/
private SurfaceHolder mSurfaceHolder;
/**
* 内容滚动位置起始坐标
*/
private float x = 0;
/**
* 内容滚动位置起始坐标
*/
private float y = 0;
/**
* 文字内容宽度
*/
private float textContentWidth = 0;
private float textHeigth = 0;
private float textFontHeight = 0;
private int repeatCount = 0;
/**
* 内容
*/
private ArrayList<String> stringLines = new ArrayList<>();
private ScheduledExecutorService scheduledExecutorService = null;
private float viewWidth = 0;
/**
* @param context <see>默认滚动</see>
*/
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint mCalcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public TextSurfaceView(Context context) {
super(context);
init();
}
public TextSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TextSurfaceView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
//设置画布背景不为黑色 继承Sureface时这样处理才能透明
setZOrderOnTop(true);
mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
//背景色
setBackgroundColor(bgColor);
//设置透明
// getBackground().setAlpha(bgalpha);
}
/**
* @param context
* @param move <see>是否滚动</see>
*/
public TextSurfaceView(Context context, boolean move) {
this(context);
this.isMove = move;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
draw();
}
public void surfaceCreated(SurfaceHolder holder) {
y = getHeight() / 2 - getFontHeight(this.fontSize) / 2 + 5;
viewWidth = getWidth();
if (!TextUtils.isEmpty(content)) {
getTextInfo();
}
// textContentWidth = paint.measureText(content);
// textHeigth = getFontHeight(this.fontSize);
if (isMove) {//滚动效果
// JLog.i("surfaceCreated:",textContentWidth+"");
if (orientation == MOVE_LEFT) {
x = viewWidth;
} else if (orientation == MOVE_RIGHT) {
x = -(content.length() * 10);
} else if (orientation == MOVE_TOP) {
x = viewWidth / 2 - (textContentWidth) / 2;
y = getHeight() / 2 - textHeigth / 2;
} else {
x = viewWidth / 2 - (textContentWidth) / 2;
y = getHeight() / 2 - textHeigth / 2;
}
beginSchedule();
} else {//不滚动只画一次
draw();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
closeSchedule();
}
/**
* 画图
*/
private void draw() {
//锁定画布
Canvas canvas = mSurfaceHolder.lockCanvas();
if (mSurfaceHolder == null || canvas == null) {
return;
}
//清屏
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
//字体
mPaint.setTypeface(Typeface.SANS_SERIF);
//字体大小
mPaint.setTextSize(fontSize);
//字体颜色
mPaint.setColor(fontColor);
//字体透明度
mPaint.setAlpha(fontAlpha);
viewWidth = getWidth();
//滚动效果
if (isMove) {
//内容所占像素
float conlen = textContentWidth;
//组件宽度
float w = viewWidth;
//方向
if (orientation == MOVE_LEFT) {//向左
if (x < -conlen) {
x = w - 5;
} else {
x -= 2;
}
} else if (orientation == MOVE_RIGHT) {//向右
if (x >= w - 5) {
x = -conlen;
} else {
x += 2;
}
} else if (orientation == MOVE_TOP) {//向上
x = w / 2 - (conlen) / 2;
if (y < -textHeigth) {
y = getHeight() - 5;
} else {
y -= 2;
}
} else if (orientation == MOVE_BOTTOM) {//向下
if (y >= textHeigth) {
y = -getHeight() - 5;
} else {
y += 2;
}
}
} else {
//内容所占像素
float conlen = textContentWidth;
//组件宽度
float w = viewWidth;
if (staticOri == STATIC_LEFT) {
x = 0;
} else if (staticOri == STATIC_CENTER) {
x = w / 2 - conlen / 2;
} else if (staticOri == STATIC_RIGHT) {
x = w - conlen;
}
}
//画文字
// canvas.drawText(content, x, y, paint);
for (int i = 0; i < stringLines.size(); i++) {
canvas.drawText(stringLines.get(i), x,
y + textFontHeight * i, mPaint);
}
//解锁显示
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
private void closeSchedule() {
if (scheduledExecutorService != null) {
scheduledExecutorService.shutdown();
scheduledExecutorService = null;
}
}
private void beginSchedule() {
if (scheduledExecutorService == null) {
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(new ScrollTextThread(), 100, speed, TimeUnit.MILLISECONDS);
}
}
class ScrollTextThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (mSurfaceHolder) {
draw();
}
}
}
/******************************
* set get method
***********************************/
private int getOrientation() {
return orientation;
}
/**
* @param orientation <li>可以选择类静态变量</li>
* <li>1.MOVE_RIGHT 向右 </li>
* <li>2.MOVE_LEFT 向左 (默认)</li>
*/
public void setOrientation(int orientation) {
@synchronized(this){
this.orientation = orientation;
}
}
/**
* @param staticOri <li>choose</li>
* <li> STATIC_LEFT STATIC_CENTER STATIC_RIGHT </li>
*/
public void setStaticOri(int staticOri) {
@synchronized(this){
this.staticOri = staticOri;
}
}
private long getSpeed() {
return speed;
}
/**
* @param speed <li>速度以毫秒计算两次移动之间的时间间隔</li>
* <li>1-10 </li>
*/
public void setSpeed(long speed) {
@synchronized(this){
if(speed <1 || speed >10){
return;
}
this.speed = 100 / speed;
}
}
public boolean isMove() {
return isMove;
}
/**
* @param isMove <see>默认滚动</see>
*/
public void setMove(boolean isMove) {
@synchronized(this){
if (this.isMove == isMove) {
return;
}
this.isMove = isMove;
if (isMove) {
beginSchedule();
} else {
closeSchedule();
}
}
}
public void tryDraw() {
draw();
}
public void setContent(String content) {
@synchronized(this){
this.content = content;
getTextInfo();
}
}
private void getTextInfo() {
char ch;
int istart = 0;
int w = 0;
String tmpStr = "";
if (viewWidth == 0) {
stringLines.add(content);
return;
}
float width = viewWidth;
textFontHeight = getFontHeight(fontSize);
mCalcPaint.setTextSize(fontSize);
int count = content.length();
stringLines.clear();
float maxWidth = 0;
float tmpWidth = 0;
for (int i = 0; i < count; i++) {
ch = content.charAt(i);
float[] widths = new float[1];
String str = String.valueOf(ch);
mCalcPaint.getTextWidths(str, widths);
if (ch == '\n') {
tmpStr = content.substring(istart, i);
stringLines.add(tmpStr);
tmpWidth = mCalcPaint.measureText(tmpStr);
maxWidth = maxWidth > tmpWidth ? maxWidth : tmpWidth;
istart = i + 1;
w = 0;
} else {
w += (int) Math.ceil(widths[0]);
if (w > (width - 60)) {
tmpStr = content.substring(istart, i);
stringLines.add(tmpStr);
tmpWidth = mCalcPaint.measureText(tmpStr);
maxWidth = maxWidth > tmpWidth ? maxWidth : tmpWidth;
istart = i;
w = 0;
} else if (i == count - 1) {
tmpStr = content.substring(istart, count);
stringLines.add(tmpStr);
tmpWidth = mCalcPaint.measureText(tmpStr);
maxWidth = maxWidth > tmpWidth ? maxWidth : tmpWidth;
}
}
}
textHeigth = textFontHeight * stringLines.size();
textContentWidth = maxWidth;
y = getHeight() / 2 - textHeigth / 2 + 5;
}
public void setBgColor(String bgColor) {
@synchronized(this){
this.bgColor = Color.parseColor(bgColor);
if (mSurfaceHolder != null) {
setZOrderOnTop(true);
// setZOrderMediaOverlay(true);
mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
//背景色
setBackgroundColor(this.bgColor);
}
}
}
public void setBgColor(int bgColor) {
@synchronized(this){
this.bgColor = bgColor;
if (mSurfaceHolder != null) {
setZOrderOnTop(true);
// setZOrderMediaOverlay(true);
mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
//背景色
setBackgroundColor(this.bgColor);
}
}
}
public void setBgalpha(int bgalpha) {
@synchronized(this){
this.bgalpha = bgalpha;
}
}
public void setFontColor(String fontColor) {
@synchronized(this){
this.fontColor = Color.parseColor(fontColor);
}
}
public void setFontColor(int fontColor) {
@synchronized(this){
this.fontColor = fontColor;
}
}
public void setFontAlpha(int fontAlpha) {
@synchronized(this){
this.fontAlpha = fontAlpha;
}
}
public void setFontSize(float fontSize) {
@synchronized(this){
this.fontSize = fontSize;
}
}
int getFontHeight(float fontSize) {
mCalcPaint.setTextSize(fontSize);
Paint.FontMetrics fm = mCalcPaint.getFontMetrics();
return (int) Math.ceil(fm.descent - fm.top) + 2;
}
}