33
33
import com .xujiaji .dmlib2 .callback .OnDMAddListener ;
34
34
import com .xujiaji .dmlib2 .entity .BaseDmEntity ;
35
35
36
+ import java .util .ArrayList ;
37
+ import java .util .Collections ;
38
+ import java .util .HashSet ;
39
+ import java .util .Iterator ;
40
+ import java .util .LinkedHashSet ;
41
+ import java .util .List ;
36
42
import java .util .PriorityQueue ;
37
43
import java .util .Random ;
44
+ import java .util .Set ;
38
45
import java .util .Timer ;
39
46
import java .util .TimerTask ;
40
47
import java .util .concurrent .TimeUnit ;
@@ -48,6 +55,7 @@ public class Controller
48
55
private int mDuration = 3000 ;
49
56
private boolean isRun = false ;
50
57
private PriorityQueue <BaseDmEntity > mQueue = new PriorityQueue <>();
58
+ private Set <BaseDmEntity > mSpliceList = new LinkedHashSet <>();
51
59
private SurfaceProxy mSurfaceProxy ;
52
60
private boolean isActive ;//是否是活跃状态
53
61
private Bitmap mOneBitmap ;
@@ -57,8 +65,8 @@ public class Controller
57
65
private Canvas mOneCanvas ;
58
66
private Canvas mTwoCanvas ;
59
67
private Canvas mDrawDMCanvas ;
60
- private int mUseWidth ;//绘制了的弹幕占用的宽
61
- private int mUseHeight ;//绘制了的弹幕占用的高
68
+ private float mUseWidth ;//绘制了的弹幕占用的宽
69
+ private float mUseHeight ;//绘制了的弹幕占用的高
62
70
63
71
private int mWidth , mHeight ;
64
72
private ValueAnimator mValueAnim ;
@@ -215,9 +223,34 @@ private void animRepeat()
215
223
}
216
224
217
225
mDrawDMCanvas .drawColor (Color .TRANSPARENT , PorterDuff .Mode .CLEAR );//清理后台绘制弹幕的画布
226
+ // n ++;
227
+ // if (n % 2 == 0)
228
+ // {
229
+ // mDrawDMCanvas.drawColor(Color.BLUE);
230
+ // } else
231
+ // {
232
+ // mDrawDMCanvas.drawColor(Color.GREEN);
233
+ // }
234
+
235
+ Set <BaseDmEntity > set = new LinkedHashSet <>(mSpliceList );
236
+ for (Iterator <BaseDmEntity > it = set .iterator (); it .hasNext ();)
237
+ {
238
+ BaseDmEntity dm = it .next ();
239
+ add (dm );
240
+ if (dm .isSplice ()) // 去掉有效弹幕
241
+ {
242
+ it .remove ();
243
+ }
244
+ }
245
+
246
+ //清楚无效弹幕
247
+ mSpliceList .removeAll (set );
248
+
218
249
addFromQueue (mQueue );
219
250
}
220
251
252
+ // int n = 1;
253
+
221
254
/**
222
255
* 交换mOneBitmap和mDrawDMBitmap的引用
223
256
*/
@@ -298,9 +331,14 @@ private void startRun(int value)
298
331
private void drawUpDown (BaseDmEntity entity )
299
332
{
300
333
Bitmap bitmap = entity .getBitmap ();
301
- final int left ;
302
- final int top ;
303
- if (mUseWidth == 0 && mUseHeight == 0 )//第一次添加的情况
334
+ final float left ;
335
+ final float top ;
336
+ if (entity .isSplice ())
337
+ {
338
+ final float diff = mHeight - bitmap .getHeight ();
339
+ top = diff < 0 ? 0 : diff ;
340
+ left = entity .getLeft ();
341
+ } else if (mUseWidth == 0 && mUseHeight == 0 )//第一次添加的情况
304
342
{
305
343
top = mHeight - bitmap .getHeight () - random .nextInt (100 );
306
344
left = 0 ;
@@ -319,9 +357,26 @@ private void drawUpDown(BaseDmEntity entity)
319
357
}
320
358
321
359
mUseWidth = left + bitmap .getWidth ();
322
- mUseHeight = mHeight - top + random . nextInt ( 100 ) ;
360
+ mUseHeight = mHeight - top ;
323
361
mDrawDMCanvas .drawBitmap (bitmap , left , top , null );
362
+
363
+ if (top < 0 )
364
+ {
365
+ final int newBitmapHeight = (int ) (Math .abs (top ));
366
+ entity .setSplice (true );
367
+ Bitmap newBitmap = Bitmap .createBitmap (bitmap .getWidth (), newBitmapHeight , Bitmap .Config .ARGB_8888 );
368
+ Canvas canvas = new Canvas (newBitmap );
369
+ canvas .drawBitmap (bitmap , 0 , 0 , null );
370
+ entity .setBitmap (newBitmap );
371
+ entity .setLeft (left );
372
+ mSpliceList .add (entity );
373
+ } else
374
+ {
375
+ entity .setSplice (false );
376
+ }
377
+
324
378
dmAdded (entity );
379
+ bitmap .recycle ();
325
380
}
326
381
327
382
/**
@@ -331,9 +386,14 @@ private void drawUpDown(BaseDmEntity entity)
331
386
private void drawDownUp (BaseDmEntity entity )
332
387
{
333
388
Bitmap bitmap = entity .getBitmap ();
334
- final int left ;
335
- final int top ;
336
- if (mUseWidth == 0 && mUseHeight == 0 )//第一次添加的情况
389
+ final float left ;
390
+ final float top ;
391
+ if (entity .isSplice ())
392
+ {
393
+ top = 0 ;
394
+ left = entity .getLeft ();
395
+ }
396
+ else if (mUseWidth == 0 && mUseHeight == 0 )//第一次添加的情况
337
397
{
338
398
top = random .nextInt (100 );
339
399
left = 0 ;
@@ -352,9 +412,26 @@ private void drawDownUp(BaseDmEntity entity)
352
412
}
353
413
354
414
mUseWidth = left + bitmap .getWidth ();
355
- mUseHeight = top + bitmap .getHeight () + random . nextInt ( 100 ) ;
415
+ mUseHeight = top + bitmap .getHeight ();
356
416
mDrawDMCanvas .drawBitmap (bitmap , left , top , null );
417
+
418
+ if (mUseHeight > mHeight )
419
+ {
420
+ final int newBitmapHeight = (int ) (bitmap .getHeight () - mHeight + top );
421
+ entity .setSplice (true );
422
+ Bitmap newBitmap = Bitmap .createBitmap (bitmap .getWidth (), newBitmapHeight , Bitmap .Config .ARGB_8888 );
423
+ Canvas canvas = new Canvas (newBitmap );
424
+ canvas .drawBitmap (bitmap , 0 , -mHeight + top , null );
425
+ entity .setBitmap (newBitmap );
426
+ entity .setLeft (left );
427
+ mSpliceList .add (entity );
428
+ } else
429
+ {
430
+ entity .setSplice (false );
431
+ }
432
+
357
433
dmAdded (entity );
434
+ bitmap .recycle ();
358
435
}
359
436
360
437
/**
@@ -364,9 +441,14 @@ private void drawDownUp(BaseDmEntity entity)
364
441
private void drawRightLeft (BaseDmEntity entity )
365
442
{
366
443
Bitmap bitmap = entity .getBitmap ();
367
- final int left ;
368
- final int top ;
369
- if (mUseWidth == 0 && mUseHeight == 0 )//第一次添加的情况
444
+ final float left ;
445
+ final float top ;
446
+ if (entity .isSplice ())
447
+ {
448
+ top = entity .getTop ();
449
+ left = 0 ;
450
+ }
451
+ else if (mUseWidth == 0 && mUseHeight == 0 )//第一次添加的情况
370
452
{
371
453
left = random .nextInt (100 );
372
454
top = 0 ;
@@ -385,10 +467,28 @@ private void drawRightLeft(BaseDmEntity entity)
385
467
return ;
386
468
}
387
469
388
- mUseWidth = left + bitmap .getWidth () + random . nextInt ( 100 ) ;
470
+ mUseWidth = left + bitmap .getWidth ();
389
471
mUseHeight = top + bitmap .getHeight ();
390
472
mDrawDMCanvas .drawBitmap (bitmap , left , top , null );
473
+
474
+
475
+ if (mUseWidth > mWidth )
476
+ {
477
+ final int newBitmapWidth = (int ) (bitmap .getWidth () - mWidth + left );
478
+ entity .setSplice (true );
479
+ Bitmap newBitmap = Bitmap .createBitmap (newBitmapWidth , bitmap .getHeight (), Bitmap .Config .ARGB_8888 );
480
+ Canvas canvas = new Canvas (newBitmap );
481
+ canvas .drawBitmap (bitmap , -mWidth + left , 0 , null );
482
+ entity .setBitmap (newBitmap );
483
+ entity .setTop (top );
484
+ mSpliceList .add (entity );
485
+ } else
486
+ {
487
+ entity .setSplice (false );
488
+ }
489
+
391
490
dmAdded (entity );
491
+ bitmap .recycle ();
392
492
}
393
493
394
494
/**
@@ -398,9 +498,14 @@ private void drawRightLeft(BaseDmEntity entity)
398
498
private void drawLeftRight (BaseDmEntity entity )
399
499
{
400
500
Bitmap bitmap = entity .getBitmap ();
401
- final int left ;
402
- final int top ;
403
- if (mUseWidth == 0 && mUseHeight == 0 )//第一次添加的情况
501
+ final float left ;
502
+ final float top ;
503
+ if (entity .isSplice ())
504
+ {
505
+ final float diff = mWidth - bitmap .getWidth ();
506
+ left = diff < 0 ? 0 : diff ;
507
+ top = entity .getTop ();
508
+ } else if (mUseWidth == 0 && mUseHeight == 0 )//第一次添加的情况
404
509
{
405
510
left = mWidth - bitmap .getWidth () - random .nextInt (100 );
406
511
top = 0 ;
@@ -418,10 +523,27 @@ private void drawLeftRight(BaseDmEntity entity)
418
523
return ;
419
524
}
420
525
421
- mUseWidth = mWidth - left + random . nextInt ( 100 ) ;
526
+ mUseWidth = mWidth - left ;
422
527
mUseHeight = top + bitmap .getHeight ();
423
528
mDrawDMCanvas .drawBitmap (bitmap , left , top , null );
529
+
530
+ if (left < 0 )
531
+ {
532
+ final int newBitmapWidth = (int ) (Math .abs (left ));
533
+ entity .setSplice (true );
534
+ Bitmap newBitmap = Bitmap .createBitmap (newBitmapWidth , bitmap .getHeight (), Bitmap .Config .ARGB_8888 );
535
+ Canvas canvas = new Canvas (newBitmap );
536
+ canvas .drawBitmap (bitmap , 0 , 0 , null );
537
+ entity .setBitmap (newBitmap );
538
+ entity .setTop (top );
539
+ mSpliceList .add (entity );
540
+ } else
541
+ {
542
+ entity .setSplice (false );
543
+ }
544
+
424
545
dmAdded (entity );
546
+ bitmap .recycle ();
425
547
}
426
548
427
549
0 commit comments