Skip to content

Commit

Permalink
refactor: rename BaseCacheStuffer.Callback to BaseCacheStuffer.Proxy
Browse files Browse the repository at this point in the history
add relaseResource method to BaseCacheStuffer.Proxy
update Sample
  • Loading branch information
ctiao committed Nov 20, 2015
1 parent dfdb081 commit 6d032ae
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ protected void entryRemoved(boolean evicted, BaseDanmaku oldValue, BaseDanmaku n
if (oldValue.cache != null) {
IDrawingCache<?> cache = oldValue.cache;
long releasedSize = clearCache(oldValue);
if (oldValue.isTimeOut()) {
mContext.getDisplayer().getCacheStuffer().releaseResource(oldValue);
}
if (releasedSize <= 0) return;
mRealSize -= releasedSize;
mCachePool.release((DrawingCache) cache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
*/
public abstract class BaseCacheStuffer {

public static abstract class Callback {
public static abstract class Proxy {
/**
* 在弹幕显示前使用新的text,使用新的text
* @param danmaku
* @param fromWorkerThread 是否在工作(非UI)线程,在true的情况下可以做一些耗时操作(例如更新Span的drawblae或者其他IO操作)
* @return 如果不需重置,直接返回danmaku.text
*/
public abstract void onPrepareDrawing(BaseDanmaku danmaku, boolean fromWorkerThread);
public abstract void prepareDrawing(BaseDanmaku danmaku, boolean fromWorkerThread);

public abstract void releaseResource(BaseDanmaku danmaku);
}

protected Callback mCallback;
protected Proxy mProxy;

/**
* set paintWidth, paintHeight to danmaku
Expand Down Expand Up @@ -71,8 +73,14 @@ public void clearCache(BaseDanmaku danmaku) {

}

public void setAdapter(Callback adapter) {
mCallback = adapter;
public void setProxy(Proxy adapter) {
mProxy = adapter;
}

public void releaseResource(BaseDanmaku danmaku) {
if (mProxy != null) {
mProxy.releaseResource(danmaku);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,10 @@ public boolean isPreventOverlappingEnabled() {
* @param cacheStuffer
* @param cacheStufferAdapter
*/
public DanmakuContext setCacheStuffer(BaseCacheStuffer cacheStuffer, BaseCacheStuffer.Callback cacheStufferAdapter) {
public DanmakuContext setCacheStuffer(BaseCacheStuffer cacheStuffer, BaseCacheStuffer.Proxy cacheStufferAdapter) {
this.mCacheStuffer = cacheStuffer;
if (this.mCacheStuffer != null) {
this.mCacheStuffer.setAdapter(cacheStufferAdapter);
this.mCacheStuffer.setProxy(cacheStufferAdapter);
mDisplayer.setCacheStuffer(this.mCacheStuffer);
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ protected Float getCacheHeight(BaseDanmaku danmaku, Paint paint) {

@Override
public void measure(BaseDanmaku danmaku, TextPaint paint, boolean fromWorkerThread) {
if (mCallback != null) {
mCallback.onPrepareDrawing(danmaku, fromWorkerThread);
if (mProxy != null) {
mProxy.prepareDrawing(danmaku, fromWorkerThread);
}
float w = 0;
Float textHeight = 0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class SpannedCacheStuffer extends SimpleTextCacheStuffer {
@Override
public void measure(BaseDanmaku danmaku, TextPaint paint, boolean fromWorkerThread) {
if (danmaku.text instanceof Spanned) {
if (mCallback != null) {
mCallback.onPrepareDrawing(danmaku, fromWorkerThread);
if (mProxy != null) {
mProxy.prepareDrawing(danmaku, fromWorkerThread);
}
CharSequence text = createNewSpan(danmaku, danmaku.text);
CharSequence text = danmaku.text;
if (text != null) {
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();
Expand Down Expand Up @@ -59,10 +59,10 @@ public void drawText(BaseDanmaku danmaku, String lineText, Canvas canvas, float
if (requestInvalidate || staticLayout == null) {
if (requestInvalidate) {
danmaku.requestFlags &= ~BaseDanmaku.FLAG_REQUEST_INVALIDATE;
} else if (mCallback != null) {
mCallback.onPrepareDrawing(danmaku, fromWorkerThread);
} else if (mProxy != null) {
mProxy.prepareDrawing(danmaku, fromWorkerThread);
}
CharSequence text = createNewSpan(danmaku, danmaku.text);
CharSequence text = danmaku.text;
if (text != null) {
if (requestRemeasure) {
staticLayout = new StaticLayout(text, paint, (int) StaticLayout.getDesiredWidth(danmaku.text, paint), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
Expand Down Expand Up @@ -103,20 +103,9 @@ public void clearCache(BaseDanmaku danmaku) {
}
}

private CharSequence createNewSpan(BaseDanmaku danmaku, CharSequence text) {
if (text instanceof SpannableStringBuilder) {
text = new SpannableStringBuilder(text);
} else if (text instanceof Spannable) {
text = Spannable.Factory.getInstance().newSpannable(text);
} else if (text instanceof SpannedString) {
text = new SpannedString(text);
} else {
if (text != danmaku.text) {
DanmakuUtils.fillText(danmaku, text);
}
danmaku.obj = null;
return null;
}
return text;
@Override
public void releaseResource(BaseDanmaku danmaku) {
clearCache(danmaku);
super.releaseResource(danmaku);
}
}
9 changes: 7 additions & 2 deletions Sample/src/main/java/com/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public class MainActivity extends Activity implements View.OnClickListener {

private Button mBtnSendDanmakus;
private DanmakuContext mContext;
private BaseCacheStuffer.Callback mCacheStufferAdapter = new BaseCacheStuffer.Callback() {
private BaseCacheStuffer.Proxy mCacheStufferAdapter = new BaseCacheStuffer.Proxy() {

private Drawable mDrawable;

@Override
public void onPrepareDrawing(final BaseDanmaku danmaku, boolean fromWorkerThread) {
public void prepareDrawing(final BaseDanmaku danmaku, boolean fromWorkerThread) {
if (danmaku.text instanceof Spanned) { // 根据你的条件检查是否需要需要更新弹幕
// FIXME 这里只是简单启个线程来加载远程url图片,请使用你自己的异步线程池,最好加上你的缓存池
new Thread() {
Expand Down Expand Up @@ -119,6 +119,11 @@ public void run() {
}.start();
}
}

@Override
public void releaseResource(BaseDanmaku danmaku) {
// TODO 重要:清理含有ImageSpan的text中的一些占用内存的资源 例如drawable
}
};

/**
Expand Down

0 comments on commit 6d032ae

Please sign in to comment.