From e7487ba8ab77390bb6b09386f5d7b1321112b6c3 Mon Sep 17 00:00:00 2001 From: xyczero Date: Tue, 10 Nov 2015 17:25:16 +0800 Subject: [PATCH] avoid repeated measuring when static layout is recycled by gc --- .../model/android/SpannedCacheStuffer.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/SpannedCacheStuffer.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/SpannedCacheStuffer.java index 4c0fc3b6..18142af1 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/SpannedCacheStuffer.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/SpannedCacheStuffer.java @@ -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); return; } } @@ -54,7 +57,8 @@ public void drawText(BaseDanmaku danmaku, String lineText, Canvas canvas, float SoftReference reference = (SoftReference) 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); } boolean needRestore = false; if (left != 0 && top != 0) { @@ -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); - } + }