Skip to content

Commit

Permalink
Merge pull request bilibili#145 from xyczero/master
Browse files Browse the repository at this point in the history
avoid repeated measuring when static layout is recycled by gc
  • Loading branch information
ctiao committed Nov 10, 2015
2 parents 937cf0b + e7487ba commit 8f669ad
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public void measure(BaseDanmaku danmaku, TextPaint paint) {
text = new SpannedString(danmaku.text);
}
if (text != null) {
createStaticLayout(danmaku);
StaticLayout staticLayout = new StaticLayout(text, paint, (int) StaticLayout.getDesiredWidth(danmaku.text, paint), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
danmaku.paintWidth = staticLayout.getWidth();
danmaku.paintHeight = staticLayout.getHeight();
danmaku.obj = new SoftReference<StaticLayout>(staticLayout);
return;
}
}
Expand All @@ -54,7 +57,8 @@ public void drawText(BaseDanmaku danmaku, String lineText, Canvas canvas, float
SoftReference<StaticLayout> reference = (SoftReference<StaticLayout>) danmaku.obj;
StaticLayout staticLayout = reference.get();
if (staticLayout == null) {
createStaticLayout(danmaku);
StaticLayout staticLayout = new StaticLayout(text, paint, (int) StaticLayout.getDesiredWidth(danmaku.text, paint), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
danmaku.obj = new SoftReference<StaticLayout>(staticLayout);
}
boolean needRestore = false;
if (left != 0 && top != 0) {
Expand All @@ -73,11 +77,5 @@ public void clearCaches() {
super.clearCaches();
System.gc();
}

private void createAndMeasureStaticLayout(BaseDanmaku danmaku){
StaticLayout staticLayout = new StaticLayout(text, paint, (int) StaticLayout.getDesiredWidth(danmaku.text, paint), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
danmaku.paintWidth = staticLayout.getWidth();
danmaku.paintHeight = staticLayout.getHeight();
danmaku.obj = new SoftReference<StaticLayout>(staticLayout);
}

}

0 comments on commit 8f669ad

Please sign in to comment.