Skip to content

Commit 338bfea

Browse files
committed
fix: #1
1 parent be4166a commit 338bfea

File tree

7 files changed

+221
-27
lines changed

7 files changed

+221
-27
lines changed

.idea/gradle.xml

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# DMView2
2-
[![GitHub release](https://img.shields.io/badge/bintray-0.0.3-brightgreen.svg)](https://bintray.com/xujiaji/maven/dmlib2/0.0.3)
2+
[![GitHub release](https://img.shields.io/badge/bintray-0.0.4-brightgreen.svg)](https://bintray.com/xujiaji/maven/dmlib2/0.0.4)
3+
4+
> 更新
5+
- 0.0.4: [修复弹幕过长后的不完整](https://github.com/xujiaji/DMView2/issues/1)
36

47
**目前还不是稳定版,请使用的朋友自行斟酌!(〃'▽'〃)**
58

@@ -36,7 +39,7 @@
3639
> build.gradle文件中添加依赖
3740
3841
```
39-
implementation 'com.github.xujiaji:dmlib2:0.0.3'
42+
implementation 'com.github.xujiaji:dmlib2:0.0.4'
4043
```
4144

4245
## 步骤1

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ext {
4141
siteUrl = 'https://github.com/xujiaji/DMView2'
4242
gitUrl = 'https://github.com/xujiaji/DMView2.git'
4343

44-
libraryVersion = '0.0.3'
44+
libraryVersion = '0.0.4'
4545

4646
developerId = 'xujiaji'
4747
developerName = 'xujiaji'

dmlib2/src/main/java/com/xujiaji/dmlib2/entity/BaseDmEntity.java

+70
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,88 @@
2727
public class BaseDmEntity implements Comparable<BaseDmEntity>
2828
{
2929
private Bitmap bitmap;
30+
private boolean isSplice;//是否需要拼接
31+
private float left;
32+
private float top;
33+
private float right;
34+
private float bottom;
3035
private int priority;
3136

3237
public BaseDmEntity(View itemView)
3338
{
3439
bitmap = Util.convertViewToBitmap(itemView);
40+
isSplice = false;
3541
}
3642

3743
public Bitmap getBitmap()
3844
{
3945
return bitmap;
4046
}
4147

48+
public void setBitmap(Bitmap bitmap)
49+
{
50+
this.bitmap = bitmap;
51+
}
52+
53+
public boolean isSplice()
54+
{
55+
return isSplice;
56+
}
57+
58+
public void setSplice(boolean splice)
59+
{
60+
isSplice = splice;
61+
}
62+
63+
public float getLeft()
64+
{
65+
return left;
66+
}
67+
68+
public void setLeft(float left)
69+
{
70+
this.left = left;
71+
}
72+
73+
public float getTop()
74+
{
75+
return top;
76+
}
77+
78+
public void setTop(float top)
79+
{
80+
this.top = top;
81+
}
82+
83+
public float getRight()
84+
{
85+
return right;
86+
}
87+
88+
public void setRight(float right)
89+
{
90+
this.right = right;
91+
}
92+
93+
public float getBottom()
94+
{
95+
return bottom;
96+
}
97+
98+
public void setBottom(float bottom)
99+
{
100+
this.bottom = bottom;
101+
}
102+
103+
public int getPriority()
104+
{
105+
return priority;
106+
}
107+
108+
public void setPriority(int priority)
109+
{
110+
this.priority = priority;
111+
}
42112

43113
@Override
44114
public int compareTo(BaseDmEntity o)

dmlib2/src/main/java/com/xujiaji/dmlib2/widget/Controller.java

+140-18
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@
3333
import com.xujiaji.dmlib2.callback.OnDMAddListener;
3434
import com.xujiaji.dmlib2.entity.BaseDmEntity;
3535

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;
3642
import java.util.PriorityQueue;
3743
import java.util.Random;
44+
import java.util.Set;
3845
import java.util.Timer;
3946
import java.util.TimerTask;
4047
import java.util.concurrent.TimeUnit;
@@ -48,6 +55,7 @@ public class Controller
4855
private int mDuration = 3000;
4956
private boolean isRun = false;
5057
private PriorityQueue<BaseDmEntity> mQueue = new PriorityQueue<>();
58+
private Set<BaseDmEntity> mSpliceList = new LinkedHashSet<>();
5159
private SurfaceProxy mSurfaceProxy;
5260
private boolean isActive;//是否是活跃状态
5361
private Bitmap mOneBitmap;
@@ -57,8 +65,8 @@ public class Controller
5765
private Canvas mOneCanvas;
5866
private Canvas mTwoCanvas;
5967
private Canvas mDrawDMCanvas;
60-
private int mUseWidth;//绘制了的弹幕占用的宽
61-
private int mUseHeight;//绘制了的弹幕占用的高
68+
private float mUseWidth;//绘制了的弹幕占用的宽
69+
private float mUseHeight;//绘制了的弹幕占用的高
6270

6371
private int mWidth, mHeight;
6472
private ValueAnimator mValueAnim;
@@ -215,9 +223,34 @@ private void animRepeat()
215223
}
216224

217225
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+
218249
addFromQueue(mQueue);
219250
}
220251

252+
// int n = 1;
253+
221254
/**
222255
* 交换mOneBitmap和mDrawDMBitmap的引用
223256
*/
@@ -298,9 +331,14 @@ private void startRun(int value)
298331
private void drawUpDown(BaseDmEntity entity)
299332
{
300333
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)//第一次添加的情况
304342
{
305343
top = mHeight - bitmap.getHeight() - random.nextInt(100);
306344
left = 0;
@@ -319,9 +357,26 @@ private void drawUpDown(BaseDmEntity entity)
319357
}
320358

321359
mUseWidth = left + bitmap.getWidth();
322-
mUseHeight = mHeight - top + random.nextInt(100);
360+
mUseHeight = mHeight - top;
323361
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+
324378
dmAdded(entity);
379+
bitmap.recycle();
325380
}
326381

327382
/**
@@ -331,9 +386,14 @@ private void drawUpDown(BaseDmEntity entity)
331386
private void drawDownUp(BaseDmEntity entity)
332387
{
333388
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)//第一次添加的情况
337397
{
338398
top = random.nextInt(100);
339399
left = 0;
@@ -352,9 +412,26 @@ private void drawDownUp(BaseDmEntity entity)
352412
}
353413

354414
mUseWidth = left + bitmap.getWidth();
355-
mUseHeight = top + bitmap.getHeight() + random.nextInt(100);
415+
mUseHeight = top + bitmap.getHeight();
356416
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+
357433
dmAdded(entity);
434+
bitmap.recycle();
358435
}
359436

360437
/**
@@ -364,9 +441,14 @@ private void drawDownUp(BaseDmEntity entity)
364441
private void drawRightLeft(BaseDmEntity entity)
365442
{
366443
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)//第一次添加的情况
370452
{
371453
left = random.nextInt(100);
372454
top = 0;
@@ -385,10 +467,28 @@ private void drawRightLeft(BaseDmEntity entity)
385467
return;
386468
}
387469

388-
mUseWidth = left + bitmap.getWidth() + random.nextInt(100);
470+
mUseWidth = left + bitmap.getWidth();
389471
mUseHeight = top + bitmap.getHeight();
390472
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+
391490
dmAdded(entity);
491+
bitmap.recycle();
392492
}
393493

394494
/**
@@ -398,9 +498,14 @@ private void drawRightLeft(BaseDmEntity entity)
398498
private void drawLeftRight(BaseDmEntity entity)
399499
{
400500
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)//第一次添加的情况
404509
{
405510
left = mWidth - bitmap.getWidth() - random.nextInt(100);
406511
top = 0;
@@ -418,10 +523,27 @@ private void drawLeftRight(BaseDmEntity entity)
418523
return;
419524
}
420525

421-
mUseWidth = mWidth - left + random.nextInt(100);
526+
mUseWidth = mWidth - left;
422527
mUseHeight = top + bitmap.getHeight();
423528
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+
424545
dmAdded(entity);
546+
bitmap.recycle();
425547
}
426548

427549

0 commit comments

Comments
 (0)