diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/CacheManagingDrawTask.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/CacheManagingDrawTask.java index e32f4799..fc5086e4 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/CacheManagingDrawTask.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/CacheManagingDrawTask.java @@ -27,8 +27,8 @@ import master.flame.danmaku.danmaku.model.IDanmakuIterator; import master.flame.danmaku.danmaku.model.IDanmakus; import master.flame.danmaku.danmaku.model.IDrawingCache; -import master.flame.danmaku.danmaku.model.android.DanmakuGlobalConfig; -import master.flame.danmaku.danmaku.model.android.DanmakuGlobalConfig.DanmakuConfigTag; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; +import master.flame.danmaku.danmaku.model.android.DanmakuContext.DanmakuConfigTag; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.model.android.DrawingCache; import master.flame.danmaku.danmaku.model.android.DrawingCachePoolManager; @@ -48,12 +48,11 @@ public class CacheManagingDrawTask extends DrawTask { private CacheManager mCacheManager; private DanmakuTimer mCacheTimer; - + private final Object mDrawingNotify = new Object(); - public CacheManagingDrawTask(DanmakuTimer timer, AbsDisplayer disp, - TaskListener taskListener, int maxCacheSize) { - super(timer, disp, taskListener); + public CacheManagingDrawTask(DanmakuTimer timer, DanmakuContext config, TaskListener taskListener, int maxCacheSize) { + super(timer, config, taskListener); NativeBitmapFactory.loadLibs(); mMaxCacheSize = maxCacheSize; if (NativeBitmapFactory.isInNativeAlloc()) { @@ -91,15 +90,15 @@ protected void onDanmakuRemoved(BaseDanmaku danmaku) { } @Override - public RenderingState draw(AbsDisplayer displayer) { + public RenderingState draw(AbsDisplayer displayer) { RenderingState result = super.draw(displayer); synchronized (mDrawingNotify) { mDrawingNotify.notify(); } - if(result != null && mCacheManager != null) { - if(result.incrementCount < -20) { + if (result != null && mCacheManager != null) { + if (result.incrementCount < -20) { mCacheManager.requestClearTimeout(); - mCacheManager.requestBuild(-DanmakuFactory.MAX_DANMAKU_DURATION); + mCacheManager.requestBuild(-mContext.mDanmakuFactory.MAX_DANMAKU_DURATION); } } return result; @@ -134,7 +133,7 @@ public void start() { public void quit() { super.quit(); reset(); - if(mCacheManager!=null){ + if (mCacheManager != null) { mCacheManager.end(); mCacheManager = null; } @@ -239,30 +238,30 @@ public void resume() { begin(); } } - - public float getPoolPercent(){ - if(mMaxSize == 0){ + + public float getPoolPercent() { + if (mMaxSize == 0) { return 0; } - return mRealSize/(float)mMaxSize; + return mRealSize / (float) mMaxSize; } - - public boolean isPoolFull(){ - return mRealSize + 5120 >= mMaxSize; + + public boolean isPoolFull() { + return mRealSize + 5120 >= mMaxSize; } private void evictAll() { if (mCaches != null) { IDanmakuIterator it = mCaches.iterator(); - while(it.hasNext()) { + while (it.hasNext()) { BaseDanmaku danmaku = it.next(); entryRemoved(true, danmaku, null); } mCaches.clear(); - } + } mRealSize = 0; } - + private void evictAllNotInScreen() { evictAllNotInScreen(false); } @@ -301,14 +300,14 @@ protected void entryRemoved(boolean evicted, BaseDanmaku oldValue, BaseDanmaku n return; } mRealSize -= sizeOf(oldValue); - oldValue.cache.destroy(); + oldValue.cache.destroy(); mCachePool.release((DrawingCache) oldValue.cache); oldValue.cache = null; } } protected int sizeOf(BaseDanmaku value) { - if (value.cache != null && !value.cache.hasReferences()) { + if (value.cache != null && !value.cache.hasReferences()) { return value.cache.size(); } return 0; @@ -362,9 +361,9 @@ private void clearTimeOutCaches(long time) { } } } - + private BaseDanmaku findReuseableCache(BaseDanmaku refDanmaku, - boolean strictMode) { + boolean strictMode) { IDanmakuIterator it = mCaches.iterator(); int slopPixel = 0; if (!strictMode) { @@ -394,15 +393,16 @@ private BaseDanmaku findReuseableCache(BaseDanmaku refDanmaku, } float widthGap = danmaku.cache.width() - refDanmaku.paintWidth; float heightGap = danmaku.cache.height() - refDanmaku.paintHeight; - if (widthGap >= 0 && widthGap <= slopPixel && - heightGap >= 0 && heightGap <= slopPixel) { + if (widthGap >= 0 && widthGap <= slopPixel && + heightGap >= 0 && heightGap <= slopPixel) { return danmaku; } } return null; } - + int danmakuAddedCount = 0; + public class CacheHandler extends Handler { private static final int PREPARE = 0x1; @@ -412,17 +412,17 @@ public class CacheHandler extends Handler { public static final int BUILD_CACHES = 0x3; public static final int CLEAR_TIMEOUT_CACHES = 0x4; - + public static final int SEEK = 0x5; - + public static final int QUIT = 0x6; public static final int CLEAR_ALL_CACHES = 0x7; - + public static final int CLEAR_OUTSIDE_CACHES = 0x8; - + public static final int CLEAR_OUTSIDE_CACHES_AND_RESET = 0x9; - + public static final int DISPATCH_ACTIONS = 0x10; private boolean mPause; @@ -452,7 +452,7 @@ public void handleMessage(Message msg) { //Log.e(TAG,"dispatch_actions:"+mCacheTimer.currMillisecond+":"+mTimer.currMillisecond); long delayed = dispatchAction(); if (delayed <= 0) { - delayed = DanmakuFactory.MAX_DANMAKU_DURATION / 2; + delayed = mContext.mDanmakuFactory.MAX_DANMAKU_DURATION / 2; } sendEmptyMessageDelayed(DISPATCH_ACTIONS, delayed); break; @@ -487,13 +487,13 @@ public void handleMessage(Message msg) { case QUIT: removeCallbacksAndMessages(null); mPause = true; - evictAll(); + evictAll(); clearCachePool(); this.getLooper().quit(); break; case CLEAR_ALL_CACHES: evictAll(); - mCacheTimer.update(mTimer.currMillisecond - DanmakuFactory.MAX_DANMAKU_DURATION); + mCacheTimer.update(mTimer.currMillisecond - mContext.mDanmakuFactory.MAX_DANMAKU_DURATION); mSeekedFlag = true; break; case CLEAR_OUTSIDE_CACHES: @@ -507,14 +507,14 @@ public void handleMessage(Message msg) { break; } } - + private long dispatchAction() { float level = getPoolPercent(); BaseDanmaku firstCache = mCaches.first(); //TODO 如果firstcache大于当前时间超过半屏并且水位在0.5f以下, long gapTime = firstCache != null ? firstCache.time - mTimer.currMillisecond : 0; - long doubleScreenDuration = DanmakuFactory.MAX_DANMAKU_DURATION * 2; - if (level < 0.6f && gapTime > DanmakuFactory.MAX_DANMAKU_DURATION) { + long doubleScreenDuration = mContext.mDanmakuFactory.MAX_DANMAKU_DURATION * 2; + if (level < 0.6f && gapTime > mContext.mDanmakuFactory.MAX_DANMAKU_DURATION) { mCacheTimer.update(mTimer.currMillisecond); removeMessages(BUILD_CACHES); sendEmptyMessage(BUILD_CACHES); @@ -525,7 +525,7 @@ private long dispatchAction() { sendEmptyMessage(CLEAR_TIMEOUT_CACHES); return 0; } - + if (level >= 0.9f) { return 0; } @@ -539,7 +539,7 @@ private long dispatchAction() { } else if (deltaTime > doubleScreenDuration) { return 0; } - + removeMessages(BUILD_CACHES); sendEmptyMessage(BUILD_CACHES); return 0; @@ -559,7 +559,7 @@ private void releaseDanmakuCache(BaseDanmaku item, DrawingCache cache) { private long prepareCaches(boolean repositioned) { long curr = mCacheTimer.currMillisecond; - long end = curr + DanmakuFactory.MAX_DANMAKU_DURATION * mScreenSize; + long end = curr + mContext.mDanmakuFactory.MAX_DANMAKU_DURATION * mScreenSize; if (end < mTimer.currMillisecond) { return 0; } @@ -586,12 +586,12 @@ private long prepareCaches(boolean repositioned) { return 0; } long deltaTime = first.time - mTimer.currMillisecond; - long sleepTime = 30 + 10 * deltaTime / DanmakuFactory.MAX_DANMAKU_DURATION; + long sleepTime = 30 + 10 * deltaTime / mContext.mDanmakuFactory.MAX_DANMAKU_DURATION; sleepTime = Math.min(100, sleepTime); if (repositioned) { sleepTime = 0; } - + IDanmakuIterator itr = danmakus.iterator(); BaseDanmaku item = null; long consumingTime = 0; @@ -601,45 +601,45 @@ private long prepareCaches(boolean repositioned) { // String message = ""; while (!mPause && !mCancelFlag) { boolean hasNext = itr.hasNext(); - if(!hasNext){ + if (!hasNext) { // message = "break at not hasNext"; break; } item = itr.next(); - + if (last.time < mTimer.currMillisecond) { // message = "break at last.time < mTimer.currMillisecond"; break; } - - if(item.hasDrawingCache()){ + + if (item.hasDrawingCache()) { continue; } - + if (repositioned == false && (item.isTimeOut() || !item.isOutside())) { continue; } if (!item.hasPassedFilter()) { - DanmakuFilters.getDefault().filter(item, orderInScreen, sizeInScreen, null, true); + mContext.mDanmakuFilters.filter(item, orderInScreen, sizeInScreen, null, true, mContext); } //Log.e("prepareCache", currScreenIndex+","+orderInScreen+"," + item.time+"skip:"+skip); if (item.priority == 0 && item.isFiltered()) { continue; } - - if(item.getType() == BaseDanmaku.TYPE_SCROLL_RL){ + + if (item.getType() == BaseDanmaku.TYPE_SCROLL_RL) { // 同屏弹幕密度只对滚动弹幕有效 - int screenIndex = (int) ((item.time - curr)/DanmakuFactory.MAX_DANMAKU_DURATION); - if(currScreenIndex == screenIndex) + int screenIndex = (int) ((item.time - curr) / mContext.mDanmakuFactory.MAX_DANMAKU_DURATION); + if (currScreenIndex == screenIndex) orderInScreen++; - else{ + else { orderInScreen = 0; currScreenIndex = screenIndex; } } - + if (!repositioned) { try { synchronized (mDrawingNotify) { @@ -664,13 +664,13 @@ private long prepareCaches(boolean repositioned) { break; } } - + } consumingTime = System.currentTimeMillis() - startTime; if (item != null) { mCacheTimer.update(item.time); //Log.i("cache","stop at :"+item.time+","+count+",size:"+danmakus.size()+","+message); - }else { + } else { mCacheTimer.update(end); } return consumingTime; @@ -705,7 +705,7 @@ public boolean createCache(BaseDanmaku item) { } private byte buildCache(BaseDanmaku item) { - + // measure if (!item.isMeasured()) { item.measure(mDisp); @@ -724,7 +724,7 @@ private byte buildCache(BaseDanmaku item) { mCacheManager.push(item, sizeOf(item)); return RESULT_SUCCESS; } - + // try to find reuseable cache from timeout && no-refrerence caches danmaku = findReuseableCache(item, false); if (danmaku != null) { @@ -775,7 +775,7 @@ private final void addDanmakuAndBuildCache(BaseDanmaku danmaku) { if (danmaku.priority == 0 && danmaku.isFiltered()) { return; } - if (!danmaku.isLive && danmaku.isLate() && danmaku.time > mCacheTimer.currMillisecond + DanmakuFactory.MAX_DANMAKU_DURATION) { + if (!danmaku.isLive && danmaku.isLate() && danmaku.time > mCacheTimer.currMillisecond + mContext.mDanmakuFactory.MAX_DANMAKU_DURATION) { return; } if (!danmaku.hasDrawingCache()) { @@ -783,13 +783,13 @@ private final void addDanmakuAndBuildCache(BaseDanmaku danmaku) { } if (danmaku.isLive) { mCacheTimer.update(mTimer.currMillisecond - + DanmakuFactory.MAX_DANMAKU_DURATION * mScreenSize); + + mContext.mDanmakuFactory.MAX_DANMAKU_DURATION * mScreenSize); } } public void begin() { sendEmptyMessage(PREPARE); - sendEmptyMessageDelayed(CLEAR_TIMEOUT_CACHES, DanmakuFactory.MAX_DANMAKU_DURATION); + sendEmptyMessageDelayed(CLEAR_TIMEOUT_CACHES, mContext.mDanmakuFactory.MAX_DANMAKU_DURATION); } public void pause() { @@ -803,7 +803,7 @@ public void resume() { mPause = false; removeMessages(DISPATCH_ACTIONS); sendEmptyMessage(DISPATCH_ACTIONS); - sendEmptyMessageDelayed(CLEAR_TIMEOUT_CACHES, DanmakuFactory.MAX_DANMAKU_DURATION); + sendEmptyMessageDelayed(CLEAR_TIMEOUT_CACHES, mContext.mDanmakuFactory.MAX_DANMAKU_DURATION); } public boolean isPause() { @@ -830,7 +830,7 @@ public long getFirstCacheTime() { } public void requestBuild(long correctionTime) { - if(mHandler != null) { + if (mHandler != null) { mHandler.requestBuildCacheAndDraw(correctionTime); } } @@ -869,14 +869,14 @@ public void post(Runnable runnable) { } } - + @Override - public boolean onDanmakuConfigChanged(DanmakuGlobalConfig config, DanmakuConfigTag tag, - Object... values) { + public boolean onDanmakuConfigChanged(DanmakuContext config, DanmakuConfigTag tag, + Object... values) { if (super.handleOnDanmakuConfigChanged(config, tag, values)) { // do nothing } else if (DanmakuConfigTag.SCROLL_SPEED_FACTOR.equals(tag)) { - mDisp.resetSlopPixel(DanmakuGlobalConfig.DEFAULT.scaleTextSize); + mDisp.resetSlopPixel(mContext.scaleTextSize); requestClear(); } else if (tag.isVisibilityRelatedTag()) { if (values != null && values.length > 0) { @@ -889,11 +889,11 @@ public boolean onDanmakuConfigChanged(DanmakuGlobalConfig config, DanmakuConfigT requestClear(); } else if (DanmakuConfigTag.TRANSPARENCY.equals(tag) || DanmakuConfigTag.SCALE_TEXTSIZE.equals(tag) || DanmakuConfigTag.DANMAKU_STYLE.equals(tag)) { if (DanmakuConfigTag.SCALE_TEXTSIZE.equals(tag)) { - mDisp.resetSlopPixel(DanmakuGlobalConfig.DEFAULT.scaleTextSize); + mDisp.resetSlopPixel(mContext.scaleTextSize); } if (mCacheManager != null) { mCacheManager.requestClearAll(); - mCacheManager.requestBuild(-DanmakuFactory.MAX_DANMAKU_DURATION); + mCacheManager.requestBuild(-mContext.mDanmakuFactory.MAX_DANMAKU_DURATION); } } else { if (mCacheManager != null) { diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DanmakuFilters.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DanmakuFilters.java index c1e3f2df..286c6462 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DanmakuFilters.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DanmakuFilters.java @@ -12,11 +12,10 @@ import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.DanmakuTimer; -import master.flame.danmaku.danmaku.model.GlobalFlagValues; import master.flame.danmaku.danmaku.model.IDanmakuIterator; import master.flame.danmaku.danmaku.model.IDanmakus; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.model.android.Danmakus; -import master.flame.danmaku.danmaku.parser.DanmakuFactory; public class DanmakuFilters { @@ -32,18 +31,18 @@ public class DanmakuFilters { public static final int FILTER_TYPE_OVERLAPPING = 512; - public static interface IDanmakuFilter { + public interface IDanmakuFilter { /* * 是否过滤 */ - public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, - DanmakuTimer timer, boolean fromCachingTask); + boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, + DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config); - public void setData(T data); + void setData(T data); - public void reset(); + void reset(); - public void clear(); + void clear(); } @@ -77,7 +76,7 @@ public void disableType(Integer type) { @Override public boolean filter(BaseDanmaku danmaku, int orderInScreen, int totalsizeInScreen, - DanmakuTimer timer, boolean fromCachingTask) { + DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) { boolean filtered = danmaku != null && mFilterTypes.contains(danmaku.getType()); if (filtered) { danmaku.mFilterParam |= FILTER_TYPE_TYPE; @@ -114,14 +113,14 @@ public static class QuantityDanmakuFilter extends BaseDanmakuFilter { protected BaseDanmaku mLastSkipped = null; private boolean needFilter(BaseDanmaku danmaku, int orderInScreen, - int totalSizeInScreen, DanmakuTimer timer, boolean fromCachingTask) { + int totalSizeInScreen, DanmakuTimer timer, boolean fromCachingTask, DanmakuContext context) { if (mMaximumSize <= 0 || danmaku.getType() != BaseDanmaku.TYPE_SCROLL_RL) { return false; } if (totalSizeInScreen < mMaximumSize || danmaku.isShown() - || (mLastSkipped != null && (danmaku.time - mLastSkipped.time > DanmakuFactory.MAX_DANMAKU_DURATION / 20))) { + || (mLastSkipped != null && (danmaku.time - mLastSkipped.time > context.mDanmakuFactory.MAX_DANMAKU_DURATION / 20))) { mLastSkipped = danmaku; return false; } @@ -135,8 +134,8 @@ private boolean needFilter(BaseDanmaku danmaku, int orderInScreen, @Override public synchronized boolean filter(BaseDanmaku danmaku, int orderInScreen, - int totalsizeInScreen, DanmakuTimer timer, boolean fromCachingTask) { - boolean filtered = needFilter(danmaku, orderInScreen, totalsizeInScreen, timer, fromCachingTask); + int totalsizeInScreen, DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) { + boolean filtered = needFilter(danmaku, orderInScreen, totalsizeInScreen, timer, fromCachingTask, config); if (filtered) { danmaku.mFilterParam |= FILYER_TYPE_QUANTITY; } @@ -188,7 +187,7 @@ private synchronized boolean needFilter(BaseDanmaku danmaku, int orderInScreen, @Override public boolean filter(BaseDanmaku danmaku, int orderInScreen, - int totalsizeInScreen, DanmakuTimer timer, boolean fromCachingTask) { + int totalsizeInScreen, DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) { boolean filtered = needFilter(danmaku, orderInScreen, totalsizeInScreen, timer, fromCachingTask); if (filtered) { danmaku.mFilterParam |= FILTER_TYPE_ELAPSED_TIME; @@ -230,7 +229,7 @@ private void addToWhiteList(Integer color) { @Override public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, - DanmakuTimer timer, boolean fromCachingTask) { + DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) { boolean filtered = danmaku != null && !mWhiteList.contains(danmaku.textColor); if (filtered) { danmaku.mFilterParam |= FILTER_TYPE_TEXTCOLOR; @@ -272,7 +271,7 @@ private void addToBlackList(T id) { @Override public abstract boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, - DanmakuTimer timer, boolean fromCachingTask); + DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config); @Override public void setData(List data) { @@ -300,7 +299,7 @@ public static class UserIdFilter extends UserFilter { @Override public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, - DanmakuTimer timer, boolean fromCachingTask) { + DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) { boolean filtered = danmaku != null && mBlackList.contains(danmaku.userId); if (filtered) { danmaku.mFilterParam |= FILTER_TYPE_USER_ID; @@ -319,7 +318,7 @@ public static class UserHashFilter extends UserFilter { @Override public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, - DanmakuTimer timer, boolean fromCachingTask) { + DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) { boolean filtered = danmaku != null && mBlackList.contains(danmaku.userHash); if (filtered) { danmaku.mFilterParam |= FILTER_TYPE_USER_HASH; @@ -340,7 +339,7 @@ public static class GuestFilter extends BaseDanmakuFilter { @Override public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, - DanmakuTimer timer, boolean fromCachingTask) { + DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) { boolean filtered = mBlock && danmaku.isGuest; if (filtered) { danmaku.mFilterParam |= FILTER_TYPE_USER_GUEST; @@ -434,7 +433,7 @@ public synchronized boolean needFilter(BaseDanmaku danmaku, int index, int total @Override public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, - DanmakuTimer timer, boolean fromCachingTask) { + DanmakuTimer timer, boolean fromCachingTask, DanmakuContext config) { boolean filtered = needFilter(danmaku, index, totalsizeInScreen, timer, fromCachingTask); if (filtered) { danmaku.mFilterParam |= FILTER_TYPE_DUPLICATE_MERGE; @@ -466,7 +465,7 @@ public static class MaximumLinesFilter extends BaseDanmakuFilter mMaximumLinesPairs; @Override - public boolean filter(BaseDanmaku danmaku, int lines, int totalsizeInScreen, DanmakuTimer timer, boolean willHit) { + public boolean filter(BaseDanmaku danmaku, int lines, int totalsizeInScreen, DanmakuTimer timer, boolean willHit, DanmakuContext config) { boolean filtered = false; if (mMaximumLinesPairs != null) { Integer maxLines = mMaximumLinesPairs.get(danmaku.getType()); @@ -494,7 +493,7 @@ public static class OverlappingFilter extends BaseDanmakuFilter mEnabledPairs; @Override - public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, DanmakuTimer timer, boolean willHit) { + public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, DanmakuTimer timer, boolean willHit, DanmakuContext config) { boolean filtered = false; if (mEnabledPairs != null) { Boolean enabledValue = mEnabledPairs.get(danmaku.getType()); @@ -529,24 +528,22 @@ public void reset() { public final static String TAG_USER_HASH_FILTER = "1015_Filter"; - public static final String TAG_GUEST_FILTER = "1016_Filter"; - - public static final String TAG_DUPLICATE_FILTER = "1017_Filter"; + public final static String TAG_GUEST_FILTER = "1016_Filter"; - public static final String TAG_MAXIMUN_LINES_FILTER = "1018_Filter"; + public final static String TAG_DUPLICATE_FILTER = "1017_Filter"; - public static final String TAG_OVERLAPPING_FILTER = "1019_Filter"; + public final static String TAG_MAXIMUN_LINES_FILTER = "1018_Filter"; - private static DanmakuFilters instance = null; + public final static String TAG_OVERLAPPING_FILTER = "1019_Filter"; public final Exception filterException = new Exception("not suuport this filter tag"); public void filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, - DanmakuTimer timer, boolean fromCachingTask) { + DanmakuTimer timer, boolean fromCachingTask, DanmakuContext context) { for (IDanmakuFilter f : mFilterArray) { if (f != null) { - boolean filtered = f.filter(danmaku, index, totalsizeInScreen, timer, fromCachingTask); - danmaku.filterResetFlag = GlobalFlagValues.FILTER_RESET_FLAG; + boolean filtered = f.filter(danmaku, index, totalsizeInScreen, timer, fromCachingTask, context); + danmaku.filterResetFlag = context.mGlobalFlagValues.FILTER_RESET_FLAG; if (filtered) { break; } @@ -555,11 +552,11 @@ public void filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, } public boolean filterSecondary(BaseDanmaku danmaku, int lines, int totalsizeInScreen, - DanmakuTimer timer, boolean willHit) { + DanmakuTimer timer, boolean willHit, DanmakuContext context) { for (IDanmakuFilter f : mFilterArraySecondary) { if (f != null) { - boolean filtered = f.filter(danmaku, lines, totalsizeInScreen, timer, willHit); - danmaku.filterResetFlag = GlobalFlagValues.FILTER_RESET_FLAG; + boolean filtered = f.filter(danmaku, lines, totalsizeInScreen, timer, willHit, context); + danmaku.filterResetFlag = context.mGlobalFlagValues.FILTER_RESET_FLAG; if (filtered) { return true; } @@ -568,9 +565,9 @@ public boolean filterSecondary(BaseDanmaku danmaku, int lines, int totalsizeInSc return false; } - private final static Map> filters = Collections + private final Map> filters = Collections .synchronizedSortedMap(new TreeMap>()); - private final static Map> filtersSecondary = Collections + private final Map> filtersSecondary = Collections .synchronizedSortedMap(new TreeMap>()); IDanmakuFilter[] mFilterArray = new IDanmakuFilter[0]; IDanmakuFilter[] mFilterArraySecondary = new IDanmakuFilter[0]; @@ -688,11 +685,4 @@ private void throwFilterException() { } } - public static DanmakuFilters getDefault() { - if (instance == null) { - instance = new DanmakuFilters(); - } - return instance; - } - } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawHandler.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawHandler.java index 247baf7c..44ea51ba 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawHandler.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawHandler.java @@ -30,19 +30,18 @@ import master.flame.danmaku.danmaku.model.AbsDisplayer; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.DanmakuTimer; -import master.flame.danmaku.danmaku.model.GlobalFlagValues; import master.flame.danmaku.danmaku.model.IDanmakus; import master.flame.danmaku.danmaku.model.IDisplayer; -import master.flame.danmaku.danmaku.model.android.AndroidDisplayer; -import master.flame.danmaku.danmaku.model.android.DanmakuGlobalConfig; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; -import master.flame.danmaku.danmaku.parser.DanmakuFactory; import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; import master.flame.danmaku.danmaku.util.AndroidUtils; import tv.cjump.jni.DeviceUtils; public class DrawHandler extends Handler { + private DanmakuContext mContext; + public interface Callback { public void prepared(); @@ -62,13 +61,13 @@ public interface Callback { private static final int QUIT = 6; private static final int PAUSE = 7; - + private static final int SHOW_DANMAKUS = 8; - + private static final int HIDE_DANMAKUS = 9; private static final int NOTIFY_DISP_SIZE_CHANGED = 10; - + private static final int NOTIFY_RENDERING = 11; private static final int UPDATE_WHEN_PAUSED = 12; @@ -97,7 +96,7 @@ public interface Callback { private boolean mDanmakusVisible = true; - private AbsDisplayer mDisp; + private AbsDisplayer mDisp; private final RenderingState mRenderingState = new RenderingState(); @@ -113,7 +112,7 @@ public interface Callback { private final boolean mUpdateInNewThread; private long mCordonTime = 30; - + @SuppressWarnings("unused") private long mCordonTime2 = 60; @@ -139,9 +138,9 @@ public DrawHandler(Looper looper, IDanmakuViewController view, boolean danmakuVi mUpdateInNewThread = (Runtime.getRuntime().availableProcessors() > 3); mIdleSleep = !DeviceUtils.isProblemBoxDevice(); bindView(view); - if(danmakuVisibile){ + if (danmakuVisibile) { showDanmakus(null); - }else{ + } else { hideDanmakus(false); } mDanmakusVisible = danmakuVisibile; @@ -151,6 +150,10 @@ private void bindView(IDanmakuViewController view) { this.mDanmakuView = view; } + public void setConfig(DanmakuContext config) { + mContext = config; + } + public void setParser(BaseDanmakuParser parser) { mParser = parser; } @@ -230,10 +233,10 @@ public void run() { } break; case NOTIFY_DISP_SIZE_CHANGED: - DanmakuFactory.notifyDispSizeChanged(mDisp); + mContext.mDanmakuFactory.notifyDispSizeChanged(mContext); Boolean updateFlag = (Boolean) msg.obj; - if(updateFlag != null && updateFlag){ - GlobalFlagValues.updateMeasureFlag(); + if (updateFlag != null && updateFlag) { + mContext.mGlobalFlagValues.updateMeasureFlag(); } break; case SHOW_DANMAKUS: @@ -251,7 +254,7 @@ public void run() { } mDanmakusVisible = true; if(quitFlag && mDanmakuView != null) { - mDanmakuView.drawDanmakus(); + mDanmakuView.drawDanmakus(); } notifyRendering(); break; @@ -436,13 +439,13 @@ private final long syncTimer(long startMS) { mInSyncAction = false; return d; } - + private void syncTimerIfNeeded() { if (mInWaitingState) { syncTimer(System.currentTimeMillis()); } } - + private void initRenderingConfigs() { long averageFrameConsumingTime = 16; mCordonTime = Math.max(33, (long) (averageFrameConsumingTime * 2.5f)); @@ -485,20 +488,21 @@ public boolean isPrepared() { return mReady; } - private IDrawTask createDrawTask(boolean useDrwaingCache, DanmakuTimer timer, Context context, - int width, int height, boolean isHardwareAccelerated, - IDrawTask.TaskListener taskListener) { - mDisp = new AndroidDisplayer(); + private IDrawTask createDrawTask(boolean useDrwaingCache, DanmakuTimer timer, + Context context, + int width, int height, + boolean isHardwareAccelerated, + IDrawTask.TaskListener taskListener) { + mDisp = mContext.getDisplayer(); mDisp.setSize(width, height); DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); mDisp.setDensities(displayMetrics.density, displayMetrics.densityDpi, displayMetrics.scaledDensity); - mDisp.resetSlopPixel(DanmakuGlobalConfig.DEFAULT.scaleTextSize); + mDisp.resetSlopPixel(mContext.scaleTextSize); mDisp.setHardwareAccelerated(isHardwareAccelerated); - - IDrawTask task = useDrwaingCache ? new CacheManagingDrawTask(timer, mDisp, - taskListener, 1024 * 1024 * AndroidUtils.getMemoryClass(context) / 3) - : new DrawTask(timer, mDisp, taskListener); + IDrawTask task = useDrwaingCache ? + new CacheManagingDrawTask(timer, mContext, taskListener, 1024 * 1024 * AndroidUtils.getMemoryClass(context) / 3) + : new DrawTask(timer, mContext, taskListener); task.setParser(mParser); task.prepare(); obtainMessage(NOTIFY_DISP_SIZE_CHANGED, false).sendToTarget(); @@ -515,6 +519,7 @@ public void seekTo(Long ms) { public void addDanmaku(BaseDanmaku item) { if (drawTask != null) { + item.flags = mContext.mGlobalFlagValues; item.setTimer(timer); drawTask.addDanmaku(item); obtainMessage(NOTIFY_RENDERING).sendToTarget(); @@ -563,7 +568,7 @@ public RenderingState draw(Canvas canvas) { recordRenderingTime(); return mRenderingState; } - + private void notifyRendering() { if (!mInWaitingState) { return; @@ -586,7 +591,7 @@ private void notifyRendering() { } mInWaitingState = false; } - + private void waitRendering(long dTime) { mRenderingState.sysTime = System.currentTimeMillis(); mInWaitingState = true; @@ -632,7 +637,7 @@ private synchronized void recordRenderingTime() { frames = MAX_RECORD_SIZE; } } - + public IDisplayer getDisplayer(){ return mDisp; } @@ -678,4 +683,8 @@ public void clearDanmakusOnScreen() { obtainMessage(CLEAR_DANMAKUS_ON_SCREEN).sendToTarget(); } + public DanmakuContext getConfig() { + return mContext; + } + } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawTask.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawTask.java index 4cabeb3d..8374bfcc 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawTask.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawTask.java @@ -21,23 +21,23 @@ import master.flame.danmaku.danmaku.model.AbsDisplayer; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.DanmakuTimer; -import master.flame.danmaku.danmaku.model.GlobalFlagValues; import master.flame.danmaku.danmaku.model.IDanmakuIterator; import master.flame.danmaku.danmaku.model.IDanmakus; -import master.flame.danmaku.danmaku.model.android.DanmakuGlobalConfig; -import master.flame.danmaku.danmaku.model.android.DanmakuGlobalConfig.ConfigChangedCallback; -import master.flame.danmaku.danmaku.model.android.DanmakuGlobalConfig.DanmakuConfigTag; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; +import master.flame.danmaku.danmaku.model.android.DanmakuContext.ConfigChangedCallback; +import master.flame.danmaku.danmaku.model.android.DanmakuContext.DanmakuConfigTag; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; -import master.flame.danmaku.danmaku.parser.DanmakuFactory; import master.flame.danmaku.danmaku.renderer.IRenderer; import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; import master.flame.danmaku.danmaku.renderer.android.DanmakuRenderer; import master.flame.danmaku.danmaku.renderer.android.DanmakusRetainer; public class DrawTask implements IDrawTask { + + protected final DanmakuContext mContext; - protected AbsDisplayer mDisp; + protected final AbsDisplayer mDisp; protected IDanmakus danmakuList; @@ -66,24 +66,28 @@ public class DrawTask implements IDrawTask { private boolean mIsHidden; private ConfigChangedCallback mConfigChangedCallback = new ConfigChangedCallback() { @Override - public boolean onDanmakuConfigChanged(DanmakuGlobalConfig config, DanmakuConfigTag tag, Object... values) { + public boolean onDanmakuConfigChanged(DanmakuContext config, DanmakuConfigTag tag, Object... values) { return DrawTask.this.onDanmakuConfigChanged(config, tag, values); } }; - public DrawTask(DanmakuTimer timer, AbsDisplayer disp, + public DrawTask(DanmakuTimer timer, DanmakuContext context, TaskListener taskListener) { + if (context == null) { + throw new IllegalArgumentException("context is null"); + } + mContext = context; + mDisp = context.getDisplayer(); mTaskListener = taskListener; - mRenderer = new DanmakuRenderer(); - mRenderer.setVerifierEnabled(DanmakuGlobalConfig.DEFAULT.isPreventOverlappingEnabled() || DanmakuGlobalConfig.DEFAULT.isMaxLinesLimited()); - mDisp = disp; + mRenderer = new DanmakuRenderer(context); + mRenderer.setVerifierEnabled(mContext.isPreventOverlappingEnabled() || mContext.isMaxLinesLimited()); initTimer(timer); - Boolean enable = DanmakuGlobalConfig.DEFAULT.isDuplicateMergingEnabled(); + Boolean enable = mContext.isDuplicateMergingEnabled(); if (enable != null) { if(enable) { - DanmakuFilters.getDefault().registerFilter(DanmakuFilters.TAG_DUPLICATE_FILTER); + mContext.mDanmakuFilters.registerFilter(DanmakuFilters.TAG_DUPLICATE_FILTER); } else { - DanmakuFilters.getDefault().unregisterFilter(DanmakuFilters.TAG_DUPLICATE_FILTER); + mContext.mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_DUPLICATE_FILTER); } } } @@ -163,8 +167,8 @@ protected synchronized void removeUnusedLiveDanmakusIn(int msec) { @Override public IDanmakus getVisibleDanmakusOnTime(long time) { - long beginMills = time - DanmakuFactory.MAX_DANMAKU_DURATION - 100; - long endMills = time + DanmakuFactory.MAX_DANMAKU_DURATION; + long beginMills = time - mContext.mDanmakuFactory.MAX_DANMAKU_DURATION - 100; + long endMills = time + mContext.mDanmakuFactory.MAX_DANMAKU_DURATION; IDanmakus subDanmakus = danmakuList.sub(beginMills, endMills); IDanmakus visibleDanmakus = new Danmakus(); if (null != subDanmakus && !subDanmakus.isEmpty()) { @@ -181,7 +185,7 @@ public IDanmakus getVisibleDanmakusOnTime(long time) { } @Override - public synchronized RenderingState draw(AbsDisplayer displayer) { + public synchronized RenderingState draw(AbsDisplayer displayer) { return drawDanmakus(displayer,mTimer); } @@ -197,25 +201,25 @@ public void reset() { public void seek(long mills) { reset(); // requestClear(); - GlobalFlagValues.updateVisibleFlag(); + mContext.mGlobalFlagValues.updateVisibleFlag(); mStartRenderTime = mills < 1000 ? 0 : mills; } @Override public void clearDanmakusOnScreen(long currMillis) { reset(); - GlobalFlagValues.updateVisibleFlag(); + mContext.mGlobalFlagValues.updateVisibleFlag(); mStartRenderTime = currMillis; } @Override public void start() { - DanmakuGlobalConfig.DEFAULT.registerConfigChangedCallback(mConfigChangedCallback); + mContext.registerConfigChangedCallback(mConfigChangedCallback); } @Override public void quit() { - DanmakuGlobalConfig.DEFAULT.unregisterAllConfigChangedCallbacks(); + mContext.unregisterAllConfigChangedCallbacks(); if (mRenderer != null) mRenderer.release(); } @@ -230,8 +234,19 @@ public void prepare() { } protected void loadDanmakus(BaseDanmakuParser parser) { - danmakuList = parser.setDisplayer(mDisp).setTimer(mTimer).getDanmakus(); - GlobalFlagValues.resetAll(); + danmakuList = parser.setConfig(mContext).setDisplayer(mDisp).setTimer(mTimer).getDanmakus(); + if (danmakuList != null && !danmakuList.isEmpty()) { + if (danmakuList.first().flags == null) { + IDanmakuIterator it = danmakuList.iterator(); + while (it.hasNext()) { + BaseDanmaku item = it.next(); + if (item != null) { + item.flags = mContext.mGlobalFlagValues; + } + } + } + } + mContext.mGlobalFlagValues.resetAll(); } public void setParser(BaseDanmakuParser parser) { @@ -239,7 +254,7 @@ public void setParser(BaseDanmakuParser parser) { mReadyState = false; } - protected RenderingState drawDanmakus(AbsDisplayer disp, DanmakuTimer timer) { + protected RenderingState drawDanmakus(AbsDisplayer disp, DanmakuTimer timer) { if (clearRetainerFlag) { DanmakusRetainer.clear(); clearRetainerFlag = false; @@ -250,8 +265,8 @@ protected RenderingState drawDanmakus(AbsDisplayer disp, DanmakuTimer timer) if (mIsHidden) { return mRenderingState; } - long beginMills = timer.currMillisecond - DanmakuFactory.MAX_DANMAKU_DURATION - 100; - long endMills = timer.currMillisecond + DanmakuFactory.MAX_DANMAKU_DURATION; + long beginMills = timer.currMillisecond - mContext.mDanmakuFactory.MAX_DANMAKU_DURATION - 100; + long endMills = timer.currMillisecond + mContext.mDanmakuFactory.MAX_DANMAKU_DURATION; if(mLastBeginMills > beginMills || timer.currMillisecond > mLastEndMills) { IDanmakus subDanmakus = danmakuList.sub(beginMills, endMills); if(subDanmakus != null) { @@ -295,7 +310,7 @@ public void requestClearRetainer() { clearRetainerFlag = true; } - public boolean onDanmakuConfigChanged(DanmakuGlobalConfig config, DanmakuConfigTag tag, + public boolean onDanmakuConfigChanged(DanmakuContext config, DanmakuConfigTag tag, Object... values) { boolean handled = handleOnDanmakuConfigChanged(config, tag, values); if (mTaskListener != null) { @@ -304,7 +319,7 @@ public boolean onDanmakuConfigChanged(DanmakuGlobalConfig config, DanmakuConfigT return handled; } - protected boolean handleOnDanmakuConfigChanged(DanmakuGlobalConfig config, DanmakuConfigTag tag, Object[] values) { + protected boolean handleOnDanmakuConfigChanged(DanmakuContext config, DanmakuConfigTag tag, Object[] values) { boolean handled = false; if (tag == null || DanmakuConfigTag.MAXIMUM_NUMS_IN_SCREEN.equals(tag)) { handled = true; @@ -312,9 +327,9 @@ protected boolean handleOnDanmakuConfigChanged(DanmakuGlobalConfig config, Danma Boolean enable = (Boolean) values[0]; if (enable != null) { if (enable) { - DanmakuFilters.getDefault().registerFilter(DanmakuFilters.TAG_DUPLICATE_FILTER); + mContext.mDanmakuFilters.registerFilter(DanmakuFilters.TAG_DUPLICATE_FILTER); } else { - DanmakuFilters.getDefault().unregisterFilter(DanmakuFilters.TAG_DUPLICATE_FILTER); + mContext.mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_DUPLICATE_FILTER); } handled = true; } @@ -323,7 +338,7 @@ protected boolean handleOnDanmakuConfigChanged(DanmakuGlobalConfig config, Danma handled = false; } else if (DanmakuConfigTag.MAXIMUN_LINES.equals(tag) || DanmakuConfigTag.OVERLAPPING_ENABLE.equals(tag)) { if (mRenderer != null) { - mRenderer.setVerifierEnabled(DanmakuGlobalConfig.DEFAULT.isPreventOverlappingEnabled() || DanmakuGlobalConfig.DEFAULT.isMaxLinesLimited()); + mRenderer.setVerifierEnabled(mContext.isPreventOverlappingEnabled() || mContext.isMaxLinesLimited()); } handled = true; } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDanmakuView.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDanmakuView.java index 279673f9..0f9750f0 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDanmakuView.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDanmakuView.java @@ -6,6 +6,7 @@ import master.flame.danmaku.controller.DrawHandler.Callback; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.IDanmakus; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; public interface IDanmakuView { @@ -52,7 +53,8 @@ public interface IDanmakuView { * @return */ public long getCurrentTime(); - + + public DanmakuContext getConfig(); // ------------- Android View方法 -------------------- @@ -69,7 +71,7 @@ public interface IDanmakuView { // ------------- 播放控制 ------------------- - public void prepare(BaseDanmakuParser parser); + public void prepare(BaseDanmakuParser parser, DanmakuContext config); public void seekTo(Long ms); diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDrawTask.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDrawTask.java index 2c360c9f..dac21c1d 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDrawTask.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDrawTask.java @@ -34,7 +34,7 @@ public interface IDrawTask { public IDanmakus getVisibleDanmakusOnTime(long time); - public RenderingState draw(AbsDisplayer displayer); + public RenderingState draw(AbsDisplayer displayer); public void reset(); diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/AbsDisplayer.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/AbsDisplayer.java index e19e4d28..98ad24f3 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/AbsDisplayer.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/AbsDisplayer.java @@ -1,6 +1,8 @@ package master.flame.danmaku.danmaku.model; -public abstract class AbsDisplayer implements IDisplayer { +import master.flame.danmaku.danmaku.model.android.BaseCacheStuffer; + +public abstract class AbsDisplayer implements IDisplayer { public abstract T getExtraData(); @@ -11,4 +13,17 @@ public boolean isHardwareAccelerated() { return false; } + public abstract void drawDanmaku(BaseDanmaku danmaku, T canvas, float left, float top, boolean quickly); + + public abstract void clearTextHeightCache(); + + public abstract void setTypeFace(F font); + + public abstract void setFakeBoldText(boolean bold); + + public abstract void setTransparency(int newTransparency); + + public abstract void setScaleTextSizeFactor(float factor); + + public abstract void setCacheStuffer(BaseCacheStuffer cacheStuffer); } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/BaseDanmaku.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/BaseDanmaku.java index ac2b34b2..0fc0bc1e 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/BaseDanmaku.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/BaseDanmaku.java @@ -175,6 +175,8 @@ public abstract class BaseDanmaku { public int filterResetFlag = -1; + public GlobalFlagValues flags = null; + public long getDuration() { return duration.value; } @@ -188,13 +190,13 @@ public int draw(IDisplayer displayer) { } public boolean isMeasured() { - return paintWidth >= 0 && paintHeight >= 0 - && measureResetFlag == GlobalFlagValues.MEASURE_RESET_FLAG; + return paintWidth > -1 && paintHeight > -1 + && measureResetFlag == flags.MEASURE_RESET_FLAG; } public void measure(IDisplayer displayer) { displayer.measure(this); - this.measureResetFlag = GlobalFlagValues.MEASURE_RESET_FLAG; + this.measureResetFlag = flags.MEASURE_RESET_FLAG; } public boolean hasDrawingCache() { @@ -203,7 +205,7 @@ public boolean hasDrawingCache() { public boolean isShown() { return this.visibility == VISIBLE - && visibleResetFlag == GlobalFlagValues.VISIBLE_RESET_FLAG; + && visibleResetFlag == flags.VISIBLE_RESET_FLAG; } public boolean isTimeOut() { @@ -228,7 +230,7 @@ public boolean isLate() { } public boolean hasPassedFilter() { - if (filterResetFlag != GlobalFlagValues.FILTER_RESET_FLAG) { + if (filterResetFlag != flags.FILTER_RESET_FLAG) { mFilterParam = 0; return false; } @@ -236,16 +238,16 @@ public boolean hasPassedFilter() { } public boolean isFiltered() { - return filterResetFlag == GlobalFlagValues.FILTER_RESET_FLAG && mFilterParam != 0; + return filterResetFlag == flags.FILTER_RESET_FLAG && mFilterParam != 0; } public boolean isFilteredBy(int flag) { - return filterResetFlag == GlobalFlagValues.FILTER_RESET_FLAG && (mFilterParam & flag) == flag; + return filterResetFlag == flags.FILTER_RESET_FLAG && (mFilterParam & flag) == flag; } public void setVisibility(boolean b) { if (b) { - this.visibleResetFlag = GlobalFlagValues.VISIBLE_RESET_FLAG; + this.visibleResetFlag = flags.VISIBLE_RESET_FLAG; this.visibility = VISIBLE; } else this.visibility = INVISIBLE; diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/Danmaku.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/Danmaku.java index 6a50b2ec..5d1a3f3c 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/Danmaku.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/Danmaku.java @@ -16,12 +16,12 @@ package master.flame.danmaku.danmaku.model; -import master.flame.danmaku.danmaku.parser.DanmakuFactory; +import master.flame.danmaku.danmaku.util.DanmakuUtils; public class Danmaku extends BaseDanmaku { public Danmaku(String text) { - DanmakuFactory.fillText(this, text); + DanmakuUtils.fillText(this, text); } @Override diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/GlobalFlagValues.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/GlobalFlagValues.java index 7bb77657..29519b65 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/GlobalFlagValues.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/GlobalFlagValues.java @@ -2,25 +2,25 @@ public class GlobalFlagValues { - public static int MEASURE_RESET_FLAG = 0; - public static int VISIBLE_RESET_FLAG = 0; - public static int FILTER_RESET_FLAG = 0; - - public static void resetAll(){ + public int MEASURE_RESET_FLAG = 0; + public int VISIBLE_RESET_FLAG = 0; + public int FILTER_RESET_FLAG = 0; + + public void resetAll() { VISIBLE_RESET_FLAG = 0; MEASURE_RESET_FLAG = 0; FILTER_RESET_FLAG = 0; } - - public static void updateVisibleFlag(){ + + public void updateVisibleFlag() { VISIBLE_RESET_FLAG++; } - - public static void updateMeasureFlag(){ + + public void updateMeasureFlag() { MEASURE_RESET_FLAG++; } - public static void updateFilterFlag() { + public void updateFilterFlag() { FILTER_RESET_FLAG++; } } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/IDisplayer.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/IDisplayer.java index b1a24a5c..5065f5cd 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/IDisplayer.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/IDisplayer.java @@ -19,39 +19,47 @@ public interface IDisplayer { - public abstract int getWidth(); + int DANMAKU_STYLE_DEFAULT = -1; // 自动 + int DANMAKU_STYLE_NONE = 0; // 无 + int DANMAKU_STYLE_SHADOW = 1; // 阴影 + int DANMAKU_STYLE_STROKEN = 2; // 描边 + int DANMAKU_STYLE_PROJECTION = 3; // 投影 - public abstract int getHeight(); + int getWidth(); - public abstract float getDensity(); + int getHeight(); - public abstract int getDensityDpi(); + float getDensity(); - public abstract int draw(BaseDanmaku danmaku); + int getDensityDpi(); - public abstract float getScaledDensity(); + int draw(BaseDanmaku danmaku); - public abstract int getSlopPixel(); + float getScaledDensity(); - public abstract void measure(BaseDanmaku danmaku); + int getSlopPixel(); - public abstract float getStrokeWidth(); + void measure(BaseDanmaku danmaku); - public abstract void setHardwareAccelerated(boolean enable); + float getStrokeWidth(); - public abstract boolean isHardwareAccelerated(); + void setHardwareAccelerated(boolean enable); - public abstract int getMaximumCacheWidth(); + boolean isHardwareAccelerated(); - public abstract int getMaximumCacheHeight(); + int getMaximumCacheWidth(); + + int getMaximumCacheHeight(); ////////////////// setter /////////////////////////// - public abstract void resetSlopPixel(float factor); + void resetSlopPixel(float factor); + + void setDensities(float density, int densityDpi, float scaledDensity); - public abstract void setDensities(float density, int densityDpi, float scaledDensity); + void setSize(int width, int height); - public abstract void setSize(int width, int height); + void setDanmakuStyle(int style, float[] data); } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/AndroidDisplayer.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/AndroidDisplayer.java index 865573fd..ff9888a6 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/AndroidDisplayer.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/AndroidDisplayer.java @@ -35,33 +35,27 @@ import master.flame.danmaku.danmaku.parser.DanmakuFactory; import master.flame.danmaku.danmaku.renderer.IRenderer; -public class AndroidDisplayer extends AbsDisplayer { +public class AndroidDisplayer extends AbsDisplayer { private Camera camera = new Camera(); private Matrix matrix = new Matrix(); - private static float sLastScaleTextSize; - private final static Map sCachedScaleSize = new HashMap(10); + private float sLastScaleTextSize; + private final Map sCachedScaleSize = new HashMap<>(10); - @SuppressWarnings("unused") - private int HIT_CACHE_COUNT = 0; + public TextPaint PAINT, PAINT_DUPLICATE; - @SuppressWarnings("unused") - private int NO_CACHE_COUNT = 0; + private Paint ALPHA_PAINT; - public static TextPaint PAINT, PAINT_DUPLICATE; + private Paint UNDERLINE_PAINT; - private static Paint ALPHA_PAINT; - - private static Paint UNDERLINE_PAINT; - - private static Paint BORDER_PAINT; + private Paint BORDER_PAINT; /** * 下划线高度 */ - public static int UNDERLINE_HEIGHT = 4; + public int UNDERLINE_HEIGHT = 4; /** * 边框厚度 @@ -71,47 +65,51 @@ public class AndroidDisplayer extends AbsDisplayer { /** * 阴影半径 */ - private static float SHADOW_RADIUS = 4.0f; + private float SHADOW_RADIUS = 4.0f; /** * 描边宽度 */ - private static float STROKE_WIDTH = 3.5f; + private float STROKE_WIDTH = 3.5f; /** * 投影参数 */ - private static float sProjectionOffsetX = 1.0f; - private static float sProjectionOffsetY = 1.0f; - private static int sProjectionAlpha = 0xCC; + private float sProjectionOffsetX = 1.0f; + private float sProjectionOffsetY = 1.0f; + private int sProjectionAlpha = 0xCC; /** * 开启阴影,可动态改变 */ - public static boolean CONFIG_HAS_SHADOW = false; - private static boolean HAS_SHADOW = CONFIG_HAS_SHADOW; + public boolean CONFIG_HAS_SHADOW = false; + private boolean HAS_SHADOW = CONFIG_HAS_SHADOW; /** * 开启描边,可动态改变 */ - public static boolean CONFIG_HAS_STROKE = true; - private static boolean HAS_STROKE = CONFIG_HAS_STROKE; + public boolean CONFIG_HAS_STROKE = true; + private boolean HAS_STROKE = CONFIG_HAS_STROKE; /** * 开启投影,可动态改变 */ - public static boolean CONFIG_HAS_PROJECTION = false; - private static boolean HAS_PROJECTION = CONFIG_HAS_PROJECTION; + public boolean CONFIG_HAS_PROJECTION = false; + private boolean HAS_PROJECTION = CONFIG_HAS_PROJECTION; /** * 开启抗锯齿,可动态改变 */ - public static boolean CONFIG_ANTI_ALIAS = true; - private static boolean ANTI_ALIAS = CONFIG_ANTI_ALIAS; + public boolean CONFIG_ANTI_ALIAS = true; + private boolean ANTI_ALIAS = CONFIG_ANTI_ALIAS; - private static BaseCacheStuffer sStuffer = new SimpleTextCacheStuffer(); + private BaseCacheStuffer sStuffer = new SimpleTextCacheStuffer(); + private boolean isTranslucent; + private int transparency = AlphaValue.MAX; + private float scaleTextSize = 1.0f; + private boolean isTextScaled = false; - static { + public AndroidDisplayer() { PAINT = new TextPaint(); PAINT.setStrokeWidth(STROKE_WIDTH); PAINT_DUPLICATE = new TextPaint(PAINT); @@ -142,21 +140,21 @@ private static final int getMaximumBitmapHeight(Canvas c) { } } - public static void setTypeFace(Typeface font) { + public void setTypeFace(Typeface font) { if (PAINT != null) PAINT.setTypeface(font); } - public static void setShadowRadius(float s) { + public void setShadowRadius(float s) { SHADOW_RADIUS = s; } - public static void setPaintStorkeWidth(float s) { + public void setPaintStorkeWidth(float s) { PAINT.setStrokeWidth(s); STROKE_WIDTH = s; } - public static void setProjectionConfig(float offsetX, float offsetY, int alpha) { + public void setProjectionConfig(float offsetX, float offsetY, int alpha) { if (sProjectionOffsetX != offsetX || sProjectionOffsetY != offsetY || sProjectionAlpha != alpha) { sProjectionOffsetX = (offsetX > 1.0f) ? offsetX : 1.0f; sProjectionOffsetY = (offsetY > 1.0f) ? offsetY : 1.0f; @@ -164,11 +162,24 @@ public static void setProjectionConfig(float offsetX, float offsetY, int alpha) } } - public static void setFakeBoldText(boolean fakeBoldText) { + public void setFakeBoldText(boolean fakeBoldText) { PAINT.setFakeBoldText(fakeBoldText); } - public static void setCacheStuffer(BaseCacheStuffer cacheStuffer) { + @Override + public void setTransparency(int newTransparency) { + isTranslucent = (newTransparency != AlphaValue.MAX); + transparency = newTransparency; + } + + @Override + public void setScaleTextSizeFactor(float factor) { + isTextScaled = (factor != 1f); + scaleTextSize = factor; + } + + @Override + public void setCacheStuffer(BaseCacheStuffer cacheStuffer) { if (cacheStuffer != sStuffer) { sStuffer = cacheStuffer; } @@ -306,8 +317,9 @@ private int saveCanvas(BaseDanmaku danmaku, Canvas canvas, float left, float top return count; } - public static void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, float top, - boolean quick) { + @Override + public void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, float top, + boolean quickly) { float _left = left; float _top = top; left += danmaku.padding; @@ -320,8 +332,8 @@ public static void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, f HAS_STROKE = CONFIG_HAS_STROKE; HAS_SHADOW = CONFIG_HAS_SHADOW; HAS_PROJECTION = CONFIG_HAS_PROJECTION; - ANTI_ALIAS = !quick && CONFIG_ANTI_ALIAS; - TextPaint paint = getPaint(danmaku, quick); + ANTI_ALIAS = !quickly && CONFIG_ANTI_ALIAS; + TextPaint paint = getPaint(danmaku, quickly); sStuffer.drawBackground(danmaku, canvas, _left, _top); if (danmaku.lines != null) { String[] lines = danmaku.lines; @@ -334,13 +346,9 @@ public static void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, f strokeLeft += sProjectionOffsetX; strokeTop += sProjectionOffsetY; } - //TODO drawStroke(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, Paint paint); -// canvas.drawText(lines[0], strokeLeft, strokeTop, paint); sStuffer.drawStroke(danmaku, lines[0], canvas, strokeLeft, strokeTop, paint); } applyPaintConfig(danmaku, paint, false); - //TODO drawText(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, Paint paint); -// canvas.drawText(lines[0], left, top - paint.ascent(), paint); sStuffer.drawText(danmaku, lines[0], canvas, left, top - paint.ascent(), paint); } else { float textHeight = (danmaku.paintHeight - 2 * danmaku.padding) / lines.length; @@ -356,14 +364,10 @@ public static void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, f strokeLeft += sProjectionOffsetX; strokeTop += sProjectionOffsetY; } - //TODO drawStroke(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, Paint paint); -// canvas.drawText(lines[t], strokeLeft, strokeTop, paint); sStuffer.drawStroke(danmaku, lines[t], canvas, strokeLeft, strokeTop, paint); } applyPaintConfig(danmaku, paint, false); - //TODO drawText(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, Paint paint); -// canvas.drawText(lines[t], left, t * textHeight + top - paint.ascent(), paint); - sStuffer.drawText(danmaku, lines[t], canvas, left, t * textHeight + top - paint.ascent(), paint); + sStuffer.drawText(danmaku, lines[t], canvas, left, t * textHeight + top - paint.ascent(), paint); } } } else { @@ -376,13 +380,10 @@ public static void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, f strokeLeft += sProjectionOffsetX; strokeTop += sProjectionOffsetY; } - //TODO drawStroke(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, Paint paint); sStuffer.drawStroke(danmaku, null, canvas, strokeLeft, strokeTop, paint); } applyPaintConfig(danmaku, paint, false); - - //TODO drawText(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, Paint paint); sStuffer.drawText(danmaku, null, canvas, left, top - paint.ascent(), paint); } @@ -402,21 +403,21 @@ public static void drawDanmaku(BaseDanmaku danmaku, Canvas canvas, float left, f } - private static boolean hasStroke(BaseDanmaku danmaku) { + private boolean hasStroke(BaseDanmaku danmaku) { return (HAS_STROKE || HAS_PROJECTION) && STROKE_WIDTH > 0 && danmaku.textShadowColor != 0; } - public static Paint getBorderPaint(BaseDanmaku danmaku) { + public Paint getBorderPaint(BaseDanmaku danmaku) { BORDER_PAINT.setColor(danmaku.borderColor); return BORDER_PAINT; } - public static Paint getUnderlinePaint(BaseDanmaku danmaku) { + public Paint getUnderlinePaint(BaseDanmaku danmaku) { UNDERLINE_PAINT.setColor(danmaku.underlineColor); return UNDERLINE_PAINT; } - private static TextPaint getPaint(BaseDanmaku danmaku, boolean quick) { + private TextPaint getPaint(BaseDanmaku danmaku, boolean quick) { TextPaint paint; if (quick) { paint = PAINT_DUPLICATE; @@ -437,23 +438,23 @@ private static TextPaint getPaint(BaseDanmaku danmaku, boolean quick) { return paint; } - public static TextPaint getPaint(BaseDanmaku danmaku) { + public TextPaint getPaint(BaseDanmaku danmaku) { return getPaint(danmaku, false); } - private static void applyPaintConfig(BaseDanmaku danmaku, Paint paint, boolean stroke) { + private void applyPaintConfig(BaseDanmaku danmaku, Paint paint, boolean stroke) { - if (DanmakuGlobalConfig.DEFAULT.isTranslucent) { + if (isTranslucent) { if (stroke) { paint.setStyle(HAS_PROJECTION ? Style.FILL : Style.STROKE); paint.setColor(danmaku.textShadowColor & 0x00FFFFFF); - int alpha = HAS_PROJECTION ? (int) (sProjectionAlpha * ((float) DanmakuGlobalConfig.DEFAULT.transparency / AlphaValue.MAX)) - : DanmakuGlobalConfig.DEFAULT.transparency; + int alpha = HAS_PROJECTION ? (int) (sProjectionAlpha * ((float) transparency / AlphaValue.MAX)) + : transparency; paint.setAlpha(alpha); } else { paint.setStyle(Style.FILL); paint.setColor(danmaku.textColor & 0x00FFFFFF); - paint.setAlpha(DanmakuGlobalConfig.DEFAULT.transparency); + paint.setAlpha(transparency); } } else { if (stroke) { @@ -470,14 +471,14 @@ private static void applyPaintConfig(BaseDanmaku danmaku, Paint paint, boolean s } - private static void applyTextScaleConfig(BaseDanmaku danmaku, Paint paint) { - if (!DanmakuGlobalConfig.DEFAULT.isTextScaled) { + private void applyTextScaleConfig(BaseDanmaku danmaku, Paint paint) { + if (!isTextScaled) { return; } Float size = sCachedScaleSize.get(danmaku.textSize); - if (size == null || sLastScaleTextSize != DanmakuGlobalConfig.DEFAULT.scaleTextSize) { - sLastScaleTextSize = DanmakuGlobalConfig.DEFAULT.scaleTextSize; - size = danmaku.textSize * DanmakuGlobalConfig.DEFAULT.scaleTextSize; + if (size == null || sLastScaleTextSize != scaleTextSize) { + sLastScaleTextSize = scaleTextSize; + size = danmaku.textSize * scaleTextSize; sCachedScaleSize.put(danmaku.textSize, size); } paint.setTextSize(size); @@ -496,7 +497,6 @@ public void measure(BaseDanmaku danmaku) { } private void calcPaintWH(BaseDanmaku danmaku, TextPaint paint) { - //TODO measure(danmaku, paint); sStuffer.measure(danmaku, paint); setDanmakuPaintWidthAndHeight(danmaku, danmaku.paintWidth, danmaku.paintHeight); } @@ -512,8 +512,8 @@ private void setDanmakuPaintWidthAndHeight(BaseDanmaku danmaku, float w, float h danmaku.paintHeight = ph; } - public static void clearTextHeightCache() { - //TODO clear(); sTextHeightCache.clear(); + @Override + public void clearTextHeightCache() { sStuffer.clearCaches(); sCachedScaleSize.clear(); } @@ -525,8 +525,7 @@ public float getScaledDensity() { @Override public void resetSlopPixel(float factor) { - float d = Math.max(density, scaledDensity); - d = Math.max(factor, getWidth() / (float) DanmakuFactory.BILI_PLAYER_WIDTH); //correct for low density and high resolution + float d = Math.max(factor, getWidth() / DanmakuFactory.BILI_PLAYER_WIDTH); //correct for low density and high resolution float slop = d * DanmakuFactory.DANMAKU_MEDIUM_TEXTSIZE; mSlopPixel = (int) slop; if (factor > 1f) @@ -551,6 +550,36 @@ public void setSize(int width, int height) { this.height = height; } + @Override + public void setDanmakuStyle(int style, float[] values) { + switch (style) { + case DANMAKU_STYLE_NONE: + CONFIG_HAS_SHADOW = false; + CONFIG_HAS_STROKE = false; + CONFIG_HAS_PROJECTION = false; + break; + case DANMAKU_STYLE_SHADOW: + CONFIG_HAS_SHADOW = true; + CONFIG_HAS_STROKE = false; + CONFIG_HAS_PROJECTION = false; + setShadowRadius(values[0]); + break; + case DANMAKU_STYLE_DEFAULT: + case DANMAKU_STYLE_STROKEN: + CONFIG_HAS_SHADOW = false; + CONFIG_HAS_STROKE = true; + CONFIG_HAS_PROJECTION = false; + setPaintStorkeWidth(values[0]); + break; + case DANMAKU_STYLE_PROJECTION: + CONFIG_HAS_SHADOW = false; + CONFIG_HAS_STROKE = false; + CONFIG_HAS_PROJECTION = true; + setProjectionConfig(values[0], values[1], (int) values[2]); + break; + } + } + @Override public void setExtraData(Canvas data) { update(data); diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/DanmakuGlobalConfig.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/DanmakuContext.java similarity index 72% rename from DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/DanmakuGlobalConfig.java rename to DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/DanmakuContext.java index cdb93dcf..8f7e09de 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/DanmakuGlobalConfig.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/model/android/DanmakuContext.java @@ -1,6 +1,8 @@ package master.flame.danmaku.danmaku.model.android; +import android.graphics.Typeface; + import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; @@ -9,14 +11,17 @@ import master.flame.danmaku.controller.DanmakuFilters; import master.flame.danmaku.controller.DanmakuFilters.IDanmakuFilter; +import master.flame.danmaku.danmaku.model.AbsDisplayer; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.GlobalFlagValues; import master.flame.danmaku.danmaku.parser.DanmakuFactory; -import android.graphics.Typeface; +public class DanmakuContext { -public class DanmakuGlobalConfig { + public static DanmakuContext create() { + return new DanmakuContext(); + } public enum DanmakuConfigTag { FT_DANMAKU_VISIBILITY, FB_DANMAKU_VISIBILITY, L2R_DANMAKU_VISIBILITY, R2L_DANMAKU_VISIBILIY, SPECIAL_DANMAKU_VISIBILITY, TYPEFACE, TRANSPARENCY, SCALE_TEXTSIZE, MAXIMUM_NUMS_IN_SCREEN, DANMAKU_STYLE, DANMAKU_BOLD, COLOR_VALUE_WHITE_LIST, USER_ID_BLACK_LIST, USER_HASH_BLACK_LIST, SCROLL_SPEED_FACTOR, BLOCK_GUEST_DANMAKU, DUPLICATE_MERGING_ENABLED, MAXIMUN_LINES, OVERLAPPING_ENABLE; @@ -29,8 +34,6 @@ public boolean isVisibilityRelatedTag() { } } - public static DanmakuGlobalConfig DEFAULT = new DanmakuGlobalConfig(); - /** * 默认字体 */ @@ -41,15 +44,8 @@ public boolean isVisibilityRelatedTag() { */ public int transparency = AlphaValue.MAX; - public boolean isTranslucent = false; - public float scaleTextSize = 1.0f; - /** - * 弹幕大小是否被缩放 - */ - public boolean isTextScaled = false; - /** * 弹幕显示隐藏设置 */ @@ -87,12 +83,6 @@ public boolean isVisibilityRelatedTag() { public enum BorderType { NONE, SHADOW, STROKEN } - - public final static int DANMAKU_STYLE_DEFAULT = -1; // 自动 - public final static int DANMAKU_STYLE_NONE = 0; // 无 - public final static int DANMAKU_STYLE_SHADOW = 1; // 阴影 - public final static int DANMAKU_STYLE_STROKEN = 2; // 描边 - public final static int DANMAKU_STYLE_PROJECTION = 3; // 投影 public BorderType shadowType = BorderType.SHADOW; @@ -116,40 +106,52 @@ public enum BorderType { private boolean mIsPreventOverlappingEnabled; + private final AbsDisplayer mDisplayer = new AndroidDisplayer(); + + public final GlobalFlagValues mGlobalFlagValues = new GlobalFlagValues(); + + public final DanmakuFilters mDanmakuFilters = new DanmakuFilters(); + + public final DanmakuFactory mDanmakuFactory = new DanmakuFactory(); + + public AbsDisplayer getDisplayer() { + return mDisplayer; + } + /** * set typeface * * @param font */ - public DanmakuGlobalConfig setTypeface(Typeface font) { + public DanmakuContext setTypeface(Typeface font) { if (mFont != font) { mFont = font; - AndroidDisplayer.clearTextHeightCache(); - AndroidDisplayer.setTypeFace(font); + mDisplayer.clearTextHeightCache(); + mDisplayer.setTypeFace(font); notifyConfigureChanged(DanmakuConfigTag.TYPEFACE); } return this; } - public DanmakuGlobalConfig setDanmakuTransparency(float p) { + public DanmakuContext setDanmakuTransparency(float p) { int newTransparency = (int) (p * AlphaValue.MAX); if (newTransparency != transparency) { transparency = newTransparency; - isTranslucent = (newTransparency != AlphaValue.MAX); + mDisplayer.setTransparency(newTransparency); notifyConfigureChanged(DanmakuConfigTag.TRANSPARENCY, p); } return this; } - public DanmakuGlobalConfig setScaleTextSize(float p) { + public DanmakuContext setScaleTextSize(float p) { if (scaleTextSize != p) { scaleTextSize = p; - AndroidDisplayer.clearTextHeightCache(); - GlobalFlagValues.updateMeasureFlag(); - GlobalFlagValues.updateVisibleFlag(); + mDisplayer.clearTextHeightCache(); + mDisplayer.setScaleTextSizeFactor(p); + mGlobalFlagValues.updateMeasureFlag(); + mGlobalFlagValues.updateVisibleFlag(); notifyConfigureChanged(DanmakuConfigTag.SCALE_TEXTSIZE, p); } - isTextScaled = (scaleTextSize != 1f); return this; } @@ -165,10 +167,10 @@ public boolean getFTDanmakuVisibility() { * * @param visible */ - public DanmakuGlobalConfig setFTDanmakuVisibility(boolean visible) { + public DanmakuContext setFTDanmakuVisibility(boolean visible) { setDanmakuVisible(visible, BaseDanmaku.TYPE_FIX_TOP); setFilterData(DanmakuFilters.TAG_TYPE_DANMAKU_FILTER, mFilterTypes); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); if (FTDanmakuVisibility != visible) { FTDanmakuVisibility = visible; notifyConfigureChanged(DanmakuConfigTag.FT_DANMAKU_VISIBILITY, visible); @@ -182,7 +184,7 @@ private void setFilterData(String tag, T data) { private void setFilterData(String tag, T data, boolean primary) { @SuppressWarnings("unchecked") - IDanmakuFilter filter = (IDanmakuFilter) DanmakuFilters.getDefault().get(tag, primary); + IDanmakuFilter filter = (IDanmakuFilter) mDanmakuFilters.get(tag, primary); filter.setData(data); } @@ -206,10 +208,10 @@ public boolean getFBDanmakuVisibility() { * * @param visible */ - public DanmakuGlobalConfig setFBDanmakuVisibility(boolean visible) { + public DanmakuContext setFBDanmakuVisibility(boolean visible) { setDanmakuVisible(visible, BaseDanmaku.TYPE_FIX_BOTTOM); setFilterData(DanmakuFilters.TAG_TYPE_DANMAKU_FILTER, mFilterTypes); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); if (FBDanmakuVisibility != visible) { FBDanmakuVisibility = visible; notifyConfigureChanged(DanmakuConfigTag.FB_DANMAKU_VISIBILITY, visible); @@ -229,10 +231,10 @@ public boolean getL2RDanmakuVisibility() { * * @param visible */ - public DanmakuGlobalConfig setL2RDanmakuVisibility(boolean visible) { + public DanmakuContext setL2RDanmakuVisibility(boolean visible) { setDanmakuVisible(visible, BaseDanmaku.TYPE_SCROLL_LR); setFilterData(DanmakuFilters.TAG_TYPE_DANMAKU_FILTER, mFilterTypes); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); if(L2RDanmakuVisibility != visible){ L2RDanmakuVisibility = visible; notifyConfigureChanged(DanmakuConfigTag.L2R_DANMAKU_VISIBILITY, visible); @@ -252,10 +254,10 @@ public boolean getR2LDanmakuVisibility() { * * @param visible */ - public DanmakuGlobalConfig setR2LDanmakuVisibility(boolean visible) { + public DanmakuContext setR2LDanmakuVisibility(boolean visible) { setDanmakuVisible(visible, BaseDanmaku.TYPE_SCROLL_RL); setFilterData(DanmakuFilters.TAG_TYPE_DANMAKU_FILTER, mFilterTypes); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); if (R2LDanmakuVisibility != visible) { R2LDanmakuVisibility = visible; notifyConfigureChanged(DanmakuConfigTag.R2L_DANMAKU_VISIBILIY, visible); @@ -275,10 +277,10 @@ public boolean getSecialDanmakuVisibility() { * * @param visible */ - public DanmakuGlobalConfig setSpecialDanmakuVisibility(boolean visible) { + public DanmakuContext setSpecialDanmakuVisibility(boolean visible) { setDanmakuVisible(visible, BaseDanmaku.TYPE_SPECIAL); setFilterData(DanmakuFilters.TAG_TYPE_DANMAKU_FILTER, mFilterTypes); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); if (SecialDanmakuVisibility != visible) { SecialDanmakuVisibility = visible; notifyConfigureChanged(DanmakuConfigTag.SPECIAL_DANMAKU_VISIBILITY, visible); @@ -292,27 +294,24 @@ public DanmakuGlobalConfig setSpecialDanmakuVisibility(boolean visible) { * @param maxSize * @return */ - public DanmakuGlobalConfig setMaximumVisibleSizeInScreen(int maxSize) { + public DanmakuContext setMaximumVisibleSizeInScreen(int maxSize) { maximumNumsInScreen = maxSize; // 无限制 if (maxSize == 0) { - DanmakuFilters.getDefault() - .unregisterFilter(DanmakuFilters.TAG_QUANTITY_DANMAKU_FILTER); - DanmakuFilters.getDefault().unregisterFilter(DanmakuFilters.TAG_ELAPSED_TIME_FILTER); + mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_QUANTITY_DANMAKU_FILTER); + mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_ELAPSED_TIME_FILTER); notifyConfigureChanged(DanmakuConfigTag.MAXIMUM_NUMS_IN_SCREEN, maxSize); return this; } // 自动调整 if (maxSize == -1) { - DanmakuFilters.getDefault() - .unregisterFilter(DanmakuFilters.TAG_QUANTITY_DANMAKU_FILTER); - DanmakuFilters.getDefault() - .registerFilter(DanmakuFilters.TAG_ELAPSED_TIME_FILTER); + mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_QUANTITY_DANMAKU_FILTER); + mDanmakuFilters.registerFilter(DanmakuFilters.TAG_ELAPSED_TIME_FILTER); notifyConfigureChanged(DanmakuConfigTag.MAXIMUM_NUMS_IN_SCREEN, maxSize); return this; } setFilterData(DanmakuFilters.TAG_QUANTITY_DANMAKU_FILTER, maxSize); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.MAXIMUM_NUMS_IN_SCREEN, maxSize); return this; } @@ -331,34 +330,9 @@ public DanmakuGlobalConfig setMaximumVisibleSizeInScreen(int maxSize) { * alpha: 投影透明度 [0...255] * @return */ - public DanmakuGlobalConfig setDanmakuStyle(int style, float... values) { - switch (style) { - case DANMAKU_STYLE_NONE: - AndroidDisplayer.CONFIG_HAS_SHADOW = false; - AndroidDisplayer.CONFIG_HAS_STROKE = false; - AndroidDisplayer.CONFIG_HAS_PROJECTION = false; - break; - case DANMAKU_STYLE_SHADOW: - AndroidDisplayer.CONFIG_HAS_SHADOW = true; - AndroidDisplayer.CONFIG_HAS_STROKE = false; - AndroidDisplayer.CONFIG_HAS_PROJECTION = false; - AndroidDisplayer.setShadowRadius(values[0]); - break; - case DANMAKU_STYLE_DEFAULT: - case DANMAKU_STYLE_STROKEN: - AndroidDisplayer.CONFIG_HAS_SHADOW = false; - AndroidDisplayer.CONFIG_HAS_STROKE = true; - AndroidDisplayer.CONFIG_HAS_PROJECTION = false; - AndroidDisplayer.setPaintStorkeWidth(values[0]); - break; - case DANMAKU_STYLE_PROJECTION: - AndroidDisplayer.CONFIG_HAS_SHADOW = false; - AndroidDisplayer.CONFIG_HAS_STROKE = false; - AndroidDisplayer.CONFIG_HAS_PROJECTION = true; - AndroidDisplayer.setProjectionConfig(values[0], values[1], (int)values[2]); - break; - } - notifyConfigureChanged(DanmakuConfigTag.DANMAKU_STYLE, style, values[0]); + public DanmakuContext setDanmakuStyle(int style, float... values) { + mDisplayer.setDanmakuStyle(style, values); + notifyConfigureChanged(DanmakuConfigTag.DANMAKU_STYLE, style, values); return this; } @@ -368,8 +342,8 @@ public DanmakuGlobalConfig setDanmakuStyle(int style, float... values) { * @param bold * @return */ - public DanmakuGlobalConfig setDanmakuBold(boolean bold) { - AndroidDisplayer.setFakeBoldText(bold); + public DanmakuContext setDanmakuBold(boolean bold) { + mDisplayer.setFakeBoldText(bold); notifyConfigureChanged(DanmakuConfigTag.DANMAKU_BOLD, bold); return this; } @@ -379,16 +353,15 @@ public DanmakuGlobalConfig setDanmakuBold(boolean bold) { * @param colors * @return */ - public DanmakuGlobalConfig setColorValueWhiteList(Integer... colors) { + public DanmakuContext setColorValueWhiteList(Integer... colors) { mColorValueWhiteList.clear(); if (colors == null || colors.length == 0) { - DanmakuFilters.getDefault().unregisterFilter( - DanmakuFilters.TAG_TEXT_COLOR_DANMAKU_FILTER); + mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_TEXT_COLOR_DANMAKU_FILTER); } else { Collections.addAll(mColorValueWhiteList, colors); setFilterData(DanmakuFilters.TAG_TEXT_COLOR_DANMAKU_FILTER, mColorValueWhiteList); } - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.COLOR_VALUE_WHITE_LIST, mColorValueWhiteList); return this; } @@ -402,21 +375,20 @@ public List getColorValueWhiteList(){ * @param hashes * @return */ - public DanmakuGlobalConfig setUserHashBlackList(String... hashes) { + public DanmakuContext setUserHashBlackList(String... hashes) { mUserHashBlackList.clear(); if (hashes == null || hashes.length == 0) { - DanmakuFilters.getDefault().unregisterFilter( - DanmakuFilters.TAG_USER_HASH_FILTER); + mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_USER_HASH_FILTER); } else { Collections.addAll(mUserHashBlackList, hashes); setFilterData(DanmakuFilters.TAG_USER_HASH_FILTER, mUserHashBlackList); } - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.USER_HASH_BLACK_LIST, mUserHashBlackList); return this; } - public DanmakuGlobalConfig removeUserHashBlackList(String... hashes){ + public DanmakuContext removeUserHashBlackList(String... hashes){ if(hashes == null || hashes.length == 0) { return this; } @@ -424,7 +396,7 @@ public DanmakuGlobalConfig removeUserHashBlackList(String... hashes){ mUserHashBlackList.remove(hash); } setFilterData(DanmakuFilters.TAG_USER_HASH_FILTER, mUserHashBlackList); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.USER_HASH_BLACK_LIST, mUserHashBlackList); return this; } @@ -434,13 +406,13 @@ public DanmakuGlobalConfig removeUserHashBlackList(String... hashes){ * @param hashes * @return */ - public DanmakuGlobalConfig addUserHashBlackList(String... hashes){ + public DanmakuContext addUserHashBlackList(String... hashes){ if(hashes == null || hashes.length == 0) { return this; } Collections.addAll(mUserHashBlackList, hashes); setFilterData(DanmakuFilters.TAG_USER_HASH_FILTER, mUserHashBlackList); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.USER_HASH_BLACK_LIST, mUserHashBlackList); return this; } @@ -455,21 +427,20 @@ public List getUserHashBlackList(){ * @param ids * @return */ - public DanmakuGlobalConfig setUserIdBlackList(Integer... ids) { + public DanmakuContext setUserIdBlackList(Integer... ids) { mUserIdBlackList.clear(); if (ids == null || ids.length == 0) { - DanmakuFilters.getDefault().unregisterFilter( - DanmakuFilters.TAG_USER_ID_FILTER); + mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_USER_ID_FILTER); } else { Collections.addAll(mUserIdBlackList, ids); setFilterData(DanmakuFilters.TAG_USER_ID_FILTER, mUserIdBlackList); } - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.USER_ID_BLACK_LIST, mUserIdBlackList); return this; } - public DanmakuGlobalConfig removeUserIdBlackList(Integer... ids){ + public DanmakuContext removeUserIdBlackList(Integer... ids){ if(ids == null || ids.length == 0) { return this; } @@ -477,7 +448,7 @@ public DanmakuGlobalConfig removeUserIdBlackList(Integer... ids){ mUserIdBlackList.remove(id); } setFilterData(DanmakuFilters.TAG_USER_ID_FILTER, mUserIdBlackList); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.USER_ID_BLACK_LIST, mUserIdBlackList); return this; } @@ -487,13 +458,13 @@ public DanmakuGlobalConfig removeUserIdBlackList(Integer... ids){ * @param ids * @return */ - public DanmakuGlobalConfig addUserIdBlackList(Integer... ids){ + public DanmakuContext addUserIdBlackList(Integer... ids){ if(ids == null || ids.length == 0) { return this; } Collections.addAll(mUserIdBlackList, ids); setFilterData(DanmakuFilters.TAG_USER_ID_FILTER, mUserIdBlackList); - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.USER_ID_BLACK_LIST, mUserIdBlackList); return this; } @@ -507,15 +478,15 @@ public List getUserIdBlackList(){ * @param block true屏蔽,false不屏蔽 * @return */ - public DanmakuGlobalConfig blockGuestDanmaku(boolean block) { + public DanmakuContext blockGuestDanmaku(boolean block) { if (mBlockGuestDanmaku != block) { mBlockGuestDanmaku = block; if (block) { setFilterData(DanmakuFilters.TAG_GUEST_FILTER, block); } else { - DanmakuFilters.getDefault().unregisterFilter(DanmakuFilters.TAG_GUEST_FILTER); + mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_GUEST_FILTER); } - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.BLOCK_GUEST_DANMAKU, block); } return this; @@ -526,12 +497,12 @@ public DanmakuGlobalConfig blockGuestDanmaku(boolean block) { * @param p * @return */ - public DanmakuGlobalConfig setScrollSpeedFactor(float p){ + public DanmakuContext setScrollSpeedFactor(float p){ if (scrollSpeedFactor != p) { scrollSpeedFactor = p; - DanmakuFactory.updateDurationFactor(p); - GlobalFlagValues.updateMeasureFlag(); - GlobalFlagValues.updateVisibleFlag(); + mDanmakuFactory.updateDurationFactor(p); + mGlobalFlagValues.updateMeasureFlag(); + mGlobalFlagValues.updateVisibleFlag(); notifyConfigureChanged(DanmakuConfigTag.SCROLL_SPEED_FACTOR, p); } return this; @@ -542,10 +513,10 @@ public DanmakuGlobalConfig setScrollSpeedFactor(float p){ * @param enable * @return */ - public DanmakuGlobalConfig setDuplicateMergingEnabled(boolean enable) { + public DanmakuContext setDuplicateMergingEnabled(boolean enable) { if (mDuplicateMergingEnable != enable) { mDuplicateMergingEnable = enable; - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.DUPLICATE_MERGING_ENABLED, enable); } return this; @@ -562,21 +533,20 @@ public boolean isDuplicateMergingEnabled() { * V = 最大行数 * @return */ - public DanmakuGlobalConfig setMaximumLines(Map pairs) { + public DanmakuContext setMaximumLines(Map pairs) { mIsMaxLinesLimited = (pairs != null); if (pairs == null) { - DanmakuFilters.getDefault() - .unregisterFilter(DanmakuFilters.TAG_MAXIMUN_LINES_FILTER, false); + mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_MAXIMUN_LINES_FILTER, false); } else { setFilterData(DanmakuFilters.TAG_MAXIMUN_LINES_FILTER, pairs, false); } - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.MAXIMUN_LINES, pairs); return this; } @Deprecated - public DanmakuGlobalConfig setOverlapping(Map pairs) { + public DanmakuContext setOverlapping(Map pairs) { return preventOverlapping(pairs); } @@ -587,15 +557,14 @@ public DanmakuGlobalConfig setOverlapping(Map pairs) { * V = true|false 是否重叠 * @return */ - public DanmakuGlobalConfig preventOverlapping(Map pairs) { + public DanmakuContext preventOverlapping(Map pairs) { mIsPreventOverlappingEnabled = (pairs != null); if (pairs == null) { - DanmakuFilters.getDefault() - .unregisterFilter(DanmakuFilters.TAG_OVERLAPPING_FILTER, false); + mDanmakuFilters.unregisterFilter(DanmakuFilters.TAG_OVERLAPPING_FILTER, false); } else { setFilterData(DanmakuFilters.TAG_OVERLAPPING_FILTER, pairs, false); } - GlobalFlagValues.updateFilterFlag(); + mGlobalFlagValues.updateFilterFlag(); notifyConfigureChanged(DanmakuConfigTag.OVERLAPPING_ENABLE, pairs); return this; } @@ -613,16 +582,16 @@ public boolean isPreventOverlappingEnabled() { * 如果需要定制其他样式请扩展{@link SimpleTextCacheStuffer}|{@link SpannedCacheStuffer} * @param cacheStuffer */ - public DanmakuGlobalConfig setCacheStuffer(BaseCacheStuffer cacheStuffer) { + public DanmakuContext setCacheStuffer(BaseCacheStuffer cacheStuffer) { this.mCacheStuffer = cacheStuffer; if (this.mCacheStuffer != null) { - AndroidDisplayer.setCacheStuffer(this.mCacheStuffer); + mDisplayer.setCacheStuffer(this.mCacheStuffer); } return this; } public interface ConfigChangedCallback { - public boolean onDanmakuConfigChanged(DanmakuGlobalConfig config, DanmakuConfigTag tag, + public boolean onDanmakuConfigChanged(DanmakuContext config, DanmakuConfigTag tag, Object... value); } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/BaseDanmakuParser.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/BaseDanmakuParser.java index f2d48116..f52d966c 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/BaseDanmakuParser.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/BaseDanmakuParser.java @@ -19,6 +19,7 @@ import master.flame.danmaku.danmaku.model.DanmakuTimer; import master.flame.danmaku.danmaku.model.IDanmakus; import master.flame.danmaku.danmaku.model.IDisplayer; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; /** * @@ -35,15 +36,16 @@ public abstract class BaseDanmakuParser { private IDanmakus mDanmakus; protected IDisplayer mDisp; - + protected DanmakuContext mContext; + public BaseDanmakuParser setDisplayer(IDisplayer disp){ mDisp = disp; mDispWidth = disp.getWidth(); mDispHeight = disp.getHeight(); mDispDensity = disp.getDensity(); mScaledDensity = disp.getScaledDensity(); - DanmakuFactory.updateViewportState(mDispWidth, mDispHeight, getViewportSizeFactor()); - DanmakuFactory.updateMaxDanmakuDuration(); + mContext.mDanmakuFactory.updateViewportState(mDispWidth, mDispHeight, getViewportSizeFactor()); + mContext.mDanmakuFactory.updateMaxDanmakuDuration(); return this; } @@ -76,10 +78,10 @@ public DanmakuTimer getTimer() { public IDanmakus getDanmakus() { if (mDanmakus != null) return mDanmakus; - DanmakuFactory.resetDurationsData(); + mContext.mDanmakuFactory.resetDurationsData(); mDanmakus = parse(); releaseDataSource(); - DanmakuFactory.updateMaxDanmakuDuration(); + mContext.mDanmakuFactory.updateMaxDanmakuDuration(); return mDanmakus; } @@ -95,4 +97,8 @@ public void release() { releaseDataSource(); } + public BaseDanmakuParser setConfig(DanmakuContext config) { + mContext = config; + return this; + } } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/DanmakuFactory.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/DanmakuFactory.java index bf4bdc32..d646323f 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/DanmakuFactory.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/DanmakuFactory.java @@ -16,8 +16,6 @@ package master.flame.danmaku.danmaku.parser; -import android.text.TextUtils; - import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.FBDanmaku; @@ -29,7 +27,7 @@ import master.flame.danmaku.danmaku.model.R2LDanmaku; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.SpecialDanmaku.LinePath; -import master.flame.danmaku.danmaku.model.android.DanmakuGlobalConfig; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.model.android.Danmakus; public class DanmakuFactory { @@ -37,38 +35,39 @@ public class DanmakuFactory { public final static float OLD_BILI_PLAYER_WIDTH = 539; public final static float BILI_PLAYER_WIDTH = 682; - - public static int CURRENT_DISP_WIDTH = 0, CURRENT_DISP_HEIGHT = 0; - - private static float CURRENT_DISP_SIZE_FACTOR = 1.0f; public final static float OLD_BILI_PLAYER_HEIGHT = 385; - + public final static float BILI_PLAYER_HEIGHT = 438; public final static long COMMON_DANMAKU_DURATION = 3800; // B站原始分辨率下弹幕存活时间 - - public static final int DANMAKU_MEDIUM_TEXTSIZE = 25; + + public final static int DANMAKU_MEDIUM_TEXTSIZE = 25; public final static long MIN_DANMAKU_DURATION = 4000; + + public final static long MAX_DANMAKU_DURATION_HIGH_DENSITY = 9000; + + public int CURRENT_DISP_WIDTH = 0, CURRENT_DISP_HEIGHT = 0; - public static long REAL_DANMAKU_DURATION = COMMON_DANMAKU_DURATION; + private float CURRENT_DISP_SIZE_FACTOR = 1.0f; - public static long MAX_DANMAKU_DURATION = MIN_DANMAKU_DURATION; + public long REAL_DANMAKU_DURATION = COMMON_DANMAKU_DURATION; - public final static long MAX_DANMAKU_DURATION_HIGH_DENSITY = 9000; + public long MAX_DANMAKU_DURATION = MIN_DANMAKU_DURATION; - public static Duration MAX_Duration_Scroll_Danmaku; + public Duration MAX_Duration_Scroll_Danmaku; - public static Duration MAX_Duration_Fix_Danmaku; + public Duration MAX_Duration_Fix_Danmaku; - public static Duration MAX_Duration_Special_Danmaku; + public Duration MAX_Duration_Special_Danmaku; - public static IDanmakus sSpecialDanmakus = new Danmakus(); + public IDanmakus sSpecialDanmakus = new Danmakus(); - public static IDisplayer sLastDisp; - - public static void resetDurationsData() { + public IDisplayer sLastDisp; + private DanmakuContext sLastConfig; + + public void resetDurationsData() { sLastDisp = null; CURRENT_DISP_WIDTH = CURRENT_DISP_HEIGHT = 0; sSpecialDanmakus.clear(); @@ -78,28 +77,29 @@ public static void resetDurationsData() { MAX_DANMAKU_DURATION = MIN_DANMAKU_DURATION; } - public static void notifyDispSizeChanged(IDisplayer disp) { - if (disp != null) - sLastDisp = disp; - createDanmaku(BaseDanmaku.TYPE_SCROLL_RL, disp); + public void notifyDispSizeChanged(DanmakuContext context) { + sLastConfig = context; + sLastDisp = context.getDisplayer(); + createDanmaku(BaseDanmaku.TYPE_SCROLL_RL, context); } - public static BaseDanmaku createDanmaku(int type) { - return createDanmaku(type, sLastDisp); + public BaseDanmaku createDanmaku(int type) { + return createDanmaku(type, sLastConfig); } - public static BaseDanmaku createDanmaku(int type, IDisplayer disp) { - if (disp == null) + public BaseDanmaku createDanmaku(int type, DanmakuContext context) { + if (context == null) return null; - sLastDisp = disp; - return createDanmaku(type, disp.getWidth(), disp.getHeight(), CURRENT_DISP_SIZE_FACTOR); + sLastConfig = context; + sLastDisp = context.getDisplayer(); + return createDanmaku(type, sLastDisp.getWidth(), sLastDisp.getHeight(), CURRENT_DISP_SIZE_FACTOR, context.scrollSpeedFactor); } - public static BaseDanmaku createDanmaku(int type, IDisplayer disp, float viewportScale) { + public BaseDanmaku createDanmaku(int type, IDisplayer disp, float viewportScale, float scrollSpeedFactor) { if (disp == null) return null; sLastDisp = disp; - return createDanmaku(type, disp.getWidth(), disp.getHeight(), viewportScale); + return createDanmaku(type, disp.getWidth(), disp.getHeight(), viewportScale, scrollSpeedFactor); } /** @@ -110,9 +110,9 @@ public static BaseDanmaku createDanmaku(int type, IDisplayer disp, float viewpor * @param viewportScale 缩放比例,会影响滚动弹幕的存活时间(duration) * @return */ - public static BaseDanmaku createDanmaku(int type, int viewportWidth, int viewportHeight, - float viewportScale) { - return createDanmaku(type, (float) viewportWidth, (float) viewportHeight, viewportScale); + public BaseDanmaku createDanmaku(int type, int viewportWidth, int viewportHeight, + float viewportScale, float scrollSpeedFactor) { + return createDanmaku(type, (float) viewportWidth, (float) viewportHeight, viewportScale, scrollSpeedFactor); } /** @@ -123,12 +123,12 @@ public static BaseDanmaku createDanmaku(int type, int viewportWidth, int viewpor * @param viewportSizeFactor 会影响滚动弹幕的速度/存活时间(duration) * @return */ - public static BaseDanmaku createDanmaku(int type, float viewportWidth, float viewportHeight, - float viewportSizeFactor) { + public BaseDanmaku createDanmaku(int type, float viewportWidth, float viewportHeight, + float viewportSizeFactor, float scrollSpeedFactor) { boolean sizeChanged = updateViewportState(viewportWidth, viewportHeight, viewportSizeFactor); if (MAX_Duration_Scroll_Danmaku == null) { MAX_Duration_Scroll_Danmaku = new Duration(REAL_DANMAKU_DURATION); - MAX_Duration_Scroll_Danmaku.setFactor(DanmakuGlobalConfig.DEFAULT.scrollSpeedFactor); + MAX_Duration_Scroll_Danmaku.setFactor(scrollSpeedFactor); } else if (sizeChanged) { MAX_Duration_Scroll_Danmaku.setValue(REAL_DANMAKU_DURATION); } @@ -172,7 +172,7 @@ public static BaseDanmaku createDanmaku(int type, float viewportWidth, float vie return instance; } - public static boolean updateViewportState(float viewportWidth, float viewportHeight, + public boolean updateViewportState(float viewportWidth, float viewportHeight, float viewportSizeFactor) { boolean sizeChanged = false; if (CURRENT_DISP_WIDTH != (int) viewportWidth @@ -192,7 +192,7 @@ public static boolean updateViewportState(float viewportWidth, float viewportHei return sizeChanged; } - private static void updateSpecialDanmakusDate(float scaleX, float scaleY) { + private void updateSpecialDanmakusDate(float scaleX, float scaleY) { IDanmakus list = sSpecialDanmakus; IDanmakuIterator it = list.iterator(); while (it.hasNext()) { @@ -213,7 +213,7 @@ private static void updateSpecialDanmakusDate(float scaleX, float scaleY) { } } - public static void updateMaxDanmakuDuration() { + public void updateMaxDanmakuDuration() { long maxScrollDuration = (MAX_Duration_Scroll_Danmaku == null ? 0: MAX_Duration_Scroll_Danmaku.value), maxFixDuration = (MAX_Duration_Fix_Danmaku == null ? 0 : MAX_Duration_Fix_Danmaku.value), maxSpecialDuration = (MAX_Duration_Special_Danmaku == null ? 0: MAX_Duration_Special_Danmaku.value); @@ -225,24 +225,12 @@ public static void updateMaxDanmakuDuration() { MAX_DANMAKU_DURATION = Math.max(REAL_DANMAKU_DURATION, MAX_DANMAKU_DURATION); } - public static void updateDurationFactor(float f) { + public void updateDurationFactor(float f) { if (MAX_Duration_Scroll_Danmaku == null || MAX_Duration_Fix_Danmaku == null) return; MAX_Duration_Scroll_Danmaku.setFactor(f); updateMaxDanmakuDuration(); } - - public static void fillText(BaseDanmaku danmaku, String text) { - danmaku.text = text; - if (TextUtils.isEmpty(text) || !text.contains(BaseDanmaku.DANMAKU_BR_CHAR)) { - return; - } - - String[] lines = String.valueOf(danmaku.text).split(BaseDanmaku.DANMAKU_BR_CHAR, -1); - if (lines.length > 1) { - danmaku.lines = lines; - } - } /** * Initial translation data of the special danmaku @@ -256,7 +244,7 @@ public static void fillText(BaseDanmaku danmaku, String text) { * @param translationDuration * @param translationStartDelay */ - public static void fillTranslationData(BaseDanmaku item, float beginX, float beginY, + public void fillTranslationData(BaseDanmaku item, float beginX, float beginY, float endX, float endY, long translationDuration, long translationStartDelay, float scaleX, float scaleY) { if (item.getType() != BaseDanmaku.TYPE_SPECIAL) @@ -286,7 +274,7 @@ public static void fillLinePathData(BaseDanmaku item, float[][] points, float sc * @param endAlpha * @param alphaDuraion */ - public static void fillAlphaData(BaseDanmaku item, int beginAlpha, int endAlpha, + public void fillAlphaData(BaseDanmaku item, int beginAlpha, int endAlpha, long alphaDuraion) { if (item.getType() != BaseDanmaku.TYPE_SPECIAL) return; @@ -294,7 +282,7 @@ public static void fillAlphaData(BaseDanmaku item, int beginAlpha, int endAlpha, updateSpecicalDanmakuDuration(item); } - private static void updateSpecicalDanmakuDuration(BaseDanmaku item) { + private void updateSpecicalDanmakuDuration(BaseDanmaku item) { if (MAX_Duration_Special_Danmaku == null || (item.duration != null && item.duration.value > MAX_Duration_Special_Danmaku.value)) { MAX_Duration_Special_Danmaku = item.duration; updateMaxDanmakuDuration(); diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/AcFunDanmakuParser.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/AcFunDanmakuParser.java index be94ffb0..e96cce30 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/AcFunDanmakuParser.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/AcFunDanmakuParser.java @@ -25,7 +25,7 @@ import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; -import master.flame.danmaku.danmaku.parser.DanmakuFactory; +import master.flame.danmaku.danmaku.util.DanmakuUtils; public class AcFunDanmakuParser extends BaseDanmakuParser { @@ -82,13 +82,13 @@ private Danmakus _parse(JSONObject jsonObject, Danmakus danmakus) { long time = (long) (Float.parseFloat(values[0]) * 1000); // 出现时间 int color = Integer.parseInt(values[1]) | 0xFF000000; // 颜色 float textSize = Float.parseFloat(values[3]); // 字体大小 - BaseDanmaku item = DanmakuFactory.createDanmaku(type, mDisp); + BaseDanmaku item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.time = time; item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; - DanmakuFactory.fillText(item, obj.optString("m", "....")); + DanmakuUtils.fillText(item, obj.optString("m", "....")); item.index = i; item.setTimer(mTimer); danmakus.addItem(item); diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/BiliDanmukuParser.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/BiliDanmukuParser.java index 328772a2..b76b88e8 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/BiliDanmukuParser.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/parser/android/BiliDanmukuParser.java @@ -38,6 +38,7 @@ import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.parser.DanmakuFactory; +import master.flame.danmaku.danmaku.util.DanmakuUtils; public class BiliDanmukuParser extends BaseDanmakuParser { @@ -120,7 +121,7 @@ public void startElement(String uri, String localName, String qName, Attributes float textSize = Float.parseFloat(values[2]); // 字体大小 int color = Integer.parseInt(values[3]) | 0xFF000000; // 颜色 // int poolType = Integer.parseInt(values[5]); // 弹幕池类型(忽略 - item = DanmakuFactory.createDanmaku(type, mDisp); + item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.time = time; item.textSize = textSize * (mDispDensity - 0.6f); @@ -148,7 +149,7 @@ public void endElement(String uri, String localName, String qName) throws SAXExc @Override public void characters(char[] ch, int start, int length) { if (item != null) { - DanmakuFactory.fillText(item, decodeXmlString(new String(ch, start, length))); + DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data @@ -203,9 +204,9 @@ public void characters(char[] ch, int start, int length) { item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; - DanmakuFactory.fillTranslationData(item, beginX, + mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); - DanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); + mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/renderer/android/DanmakuRenderer.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/renderer/android/DanmakuRenderer.java index be636892..18385030 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/renderer/android/DanmakuRenderer.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/renderer/android/DanmakuRenderer.java @@ -16,13 +16,12 @@ package master.flame.danmaku.danmaku.renderer.android; -import master.flame.danmaku.controller.DanmakuFilters; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.DanmakuTimer; -import master.flame.danmaku.danmaku.model.GlobalFlagValues; import master.flame.danmaku.danmaku.model.IDanmakuIterator; import master.flame.danmaku.danmaku.model.IDanmakus; import master.flame.danmaku.danmaku.model.IDisplayer; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.renderer.IRenderer; import master.flame.danmaku.danmaku.renderer.Renderer; @@ -31,11 +30,12 @@ public class DanmakuRenderer extends Renderer { private final DanmakuTimer mStartTimer = new DanmakuTimer(); private final RenderingState mRenderingState = new RenderingState(); + private final DanmakuContext mContext; private DanmakusRetainer.Verifier mVerifier; private final DanmakusRetainer.Verifier verifier = new DanmakusRetainer.Verifier() { @Override public boolean skipLayout(BaseDanmaku danmaku, float fixedTop, int lines, boolean willHit) { - if (danmaku.priority == 0 && DanmakuFilters.getDefault().filterSecondary(danmaku, lines, 0, mStartTimer, willHit)) { + if (danmaku.priority == 0 && mContext.mDanmakuFilters.filterSecondary(danmaku, lines, 0, mStartTimer, willHit, mContext)) { danmaku.setVisibility(false); return true; } @@ -43,16 +43,20 @@ public boolean skipLayout(BaseDanmaku danmaku, float fixedTop, int lines, boolea } }; + public DanmakuRenderer(DanmakuContext config) { + mContext = config; + } + @Override public void clear() { DanmakusRetainer.clear(); - DanmakuFilters.getDefault().clear(); + mContext.mDanmakuFilters.clear(); } @Override public void release() { DanmakusRetainer.release(); - DanmakuFilters.getDefault().clear(); + mContext.mDanmakuFilters.clear(); } @Override @@ -78,7 +82,7 @@ public RenderingState draw(IDisplayer disp, IDanmakus danmakus, long startRender } if (!drawItem.hasPassedFilter()) { - DanmakuFilters.getDefault().filter(drawItem, orderInScreen, sizeInScreen, mStartTimer, false); + mContext.mDanmakuFilters.filter(drawItem, orderInScreen, sizeInScreen, mStartTimer, false, mContext); } if (drawItem.time < startRenderTime diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/util/DanmakuUtils.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/util/DanmakuUtils.java index d546819b..32071842 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/util/DanmakuUtils.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/danmaku/util/DanmakuUtils.java @@ -16,6 +16,9 @@ package master.flame.danmaku.danmaku.util; +import android.text.TextUtils; + +import master.flame.danmaku.danmaku.model.AbsDisplayer; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.android.AndroidDisplayer; @@ -90,7 +93,7 @@ public static DrawingCache buildDanmakuDrawingCache(BaseDanmaku danmaku, IDispla cache.build((int) Math.ceil(danmaku.paintWidth), (int) Math.ceil(danmaku.paintHeight), disp.getDensityDpi(), false); DrawingCacheHolder holder = cache.get(); if (holder != null) { - AndroidDisplayer.drawDanmaku(danmaku, holder.canvas, 0, 0, false); + ((AbsDisplayer) disp).drawDanmaku(danmaku, holder.canvas, 0, 0, false); if(disp.isHardwareAccelerated()) { holder.splitWith(disp.getWidth(), disp.getHeight(), disp.getMaximumCacheWidth(), disp.getMaximumCacheHeight()); @@ -186,4 +189,15 @@ public final static boolean isOverSize(IDisplayer disp, BaseDanmaku item) { return disp.isHardwareAccelerated() && (item.paintWidth > disp.getMaximumCacheWidth() || item.paintHeight > disp.getMaximumCacheHeight()); } + public static void fillText(BaseDanmaku danmaku, String text) { + danmaku.text = text; + if (TextUtils.isEmpty(text) || !text.contains(BaseDanmaku.DANMAKU_BR_CHAR)) { + return; + } + + String[] lines = String.valueOf(danmaku.text).split(BaseDanmaku.DANMAKU_BR_CHAR, -1); + if (lines.length > 1) { + danmaku.lines = lines; + } + } } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuSurfaceView.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuSurfaceView.java index c5ba268f..9c033463 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuSurfaceView.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuSurfaceView.java @@ -37,6 +37,7 @@ import master.flame.danmaku.controller.IDanmakuViewController; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.IDanmakus; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; @@ -211,8 +212,9 @@ private void prepare() { } @Override - public void prepare(BaseDanmakuParser parser) { + public void prepare(BaseDanmakuParser parser, DanmakuContext config) { prepare(); + handler.setConfig(config); handler.setParser(parser); handler.setCallback(mCallback); handler.prepare(); @@ -223,6 +225,14 @@ public boolean isPrepared() { return handler != null && handler.isPrepared(); } + @Override + public DanmakuContext getConfig() { + if (handler == null) { + return null; + } + return handler.getConfig(); + } + @Override public void showFPS(boolean show){ mShowFps = show; diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuTextureView.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuTextureView.java index e7840f35..087ebfb8 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuTextureView.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuTextureView.java @@ -39,6 +39,7 @@ import master.flame.danmaku.controller.IDanmakuViewController; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.IDanmakus; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; @@ -220,8 +221,9 @@ private void prepare() { } @Override - public void prepare(BaseDanmakuParser parser) { + public void prepare(BaseDanmakuParser parser, DanmakuContext config) { prepare(); + handler.setConfig(config); handler.setParser(parser); handler.setCallback(mCallback); handler.prepare(); @@ -232,6 +234,14 @@ public boolean isPrepared() { return handler != null && handler.isPrepared(); } + @Override + public DanmakuContext getConfig() { + if (handler == null) { + return null; + } + return handler.getConfig(); + } + @Override public void showFPS(boolean show) { mShowFps = show; diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuView.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuView.java index 1fda175c..9886f936 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuView.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuView.java @@ -37,6 +37,7 @@ import master.flame.danmaku.controller.IDanmakuViewController; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.IDanmakus; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; @@ -191,8 +192,9 @@ private void prepare() { } @Override - public void prepare(BaseDanmakuParser parser) { - prepare(); + public void prepare(BaseDanmakuParser parser, DanmakuContext config) { + prepare(); + handler.setConfig(config); handler.setParser(parser); handler.setCallback(mCallback); handler.prepare(); @@ -203,6 +205,14 @@ public boolean isPrepared() { return handler != null && handler.isPrepared(); } + @Override + public DanmakuContext getConfig() { + if (handler == null) { + return null; + } + return handler.getConfig(); + } + @Override public void showFPS(boolean show){ mShowFps = show; diff --git a/Sample/src/main/java/com/sample/MainActivity.java b/Sample/src/main/java/com/sample/MainActivity.java index ce8f1236..42981b87 100644 --- a/Sample/src/main/java/com/sample/MainActivity.java +++ b/Sample/src/main/java/com/sample/MainActivity.java @@ -36,11 +36,11 @@ import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.DanmakuTimer; import master.flame.danmaku.danmaku.model.IDanmakus; -import master.flame.danmaku.danmaku.model.android.DanmakuGlobalConfig; +import master.flame.danmaku.danmaku.model.IDisplayer; +import master.flame.danmaku.danmaku.model.android.DanmakuContext; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.model.android.SpannedCacheStuffer; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; -import master.flame.danmaku.danmaku.parser.DanmakuFactory; import master.flame.danmaku.danmaku.parser.IDataSource; import master.flame.danmaku.danmaku.parser.android.BiliDanmukuParser; @@ -69,6 +69,7 @@ public class MainActivity extends Activity implements View.OnClickListener { private Button mBtnSendDanmakuTextAndImage; private Button mBtnSendDanmakus; + private DanmakuContext mContext; /** * 绘制背景(自定义弹幕样式) @@ -162,7 +163,8 @@ private void findViews() { overlappingEnablePair.put(BaseDanmaku.TYPE_FIX_TOP, true); mDanmakuView = (IDanmakuView) findViewById(R.id.sv_danmaku); - DanmakuGlobalConfig.DEFAULT.setDanmakuStyle(DanmakuGlobalConfig.DANMAKU_STYLE_STROKEN, 3).setDuplicateMergingEnabled(false).setScrollSpeedFactor(1.2f).setScaleTextSize(1.2f) + mContext = DanmakuContext.create(); + mContext.setDanmakuStyle(IDisplayer.DANMAKU_STYLE_STROKEN, 3).setDuplicateMergingEnabled(false).setScrollSpeedFactor(1.2f).setScaleTextSize(1.2f) .setCacheStuffer(new SpannedCacheStuffer()) // 图文混排使用SpannedCacheStuffer // .setCacheStuffer(new BackgroundCacheStuffer()) // 绘制背景使用BackgroundCacheStuffer .setMaximumLines(maxLinesPair) @@ -190,8 +192,7 @@ public void onDanmakuClick(IDanmakus danmakus) { Log.d("DFM", "onDanmakuClick danmakus size:" + danmakus.size()); } }); - - mDanmakuView.prepare(mParser); + mDanmakuView.prepare(mParser, mContext); mDanmakuView.showFPS(true); mDanmakuView.enableDanmakuDrawingCache(true); ((View) mDanmakuView).setOnClickListener(new View.OnClickListener() { @@ -309,7 +310,7 @@ public void run() { }; private void addDanmaku(boolean islive) { - BaseDanmaku danmaku = DanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL); + BaseDanmaku danmaku = mContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL); if (danmaku == null || mDanmakuView == null) { return; } @@ -330,7 +331,7 @@ private void addDanmaku(boolean islive) { } private void addDanmaKuShowTextAndImage(boolean islive) { - BaseDanmaku danmaku = DanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL); + BaseDanmaku danmaku = mContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL); String text = "bitmap"; SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text); Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);