From 16ac9b5069ff09233f6ccbd108933bc58e97d37e Mon Sep 17 00:00:00 2001 From: kmfish Date: Tue, 13 Oct 2015 15:12:05 +0800 Subject: [PATCH] click support --- .../flame/danmaku/controller/DrawHandler.java | 3 +- .../flame/danmaku/controller/DrawTask.java | 9 +-- .../danmaku/controller/IDanmakuView.java | 17 +++-- .../flame/danmaku/controller/IDrawTask.java | 5 +- .../danmaku/ui/widget/DanmakuSurfaceView.java | 13 ++-- .../danmaku/ui/widget/DanmakuTextureView.java | 13 ++-- .../danmaku/ui/widget/DanmakuTouchHelper.java | 75 ++++++++++++------- .../flame/danmaku/ui/widget/DanmakuView.java | 4 +- .../main/java/com/sample/MainActivity.java | 10 ++- 9 files changed, 85 insertions(+), 64 deletions(-) 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 76a1c2aa..247baf7c 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawHandler.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawHandler.java @@ -40,7 +40,6 @@ import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; import master.flame.danmaku.danmaku.util.AndroidUtils; import tv.cjump.jni.DeviceUtils; -import java.util.List; public class DrawHandler extends Handler { @@ -660,7 +659,7 @@ public void removeAllLiveDanmakus() { } } - public List getCurrentVisibleDanmakus() { + public IDanmakus getCurrentVisibleDanmakus() { if (drawTask != null) { return drawTask.getVisibleDanmakusOnTime(getCurrentTime()); } 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 07b60d37..4cabeb3d 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawTask.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/DrawTask.java @@ -18,9 +18,6 @@ import android.graphics.Canvas; -import java.util.ArrayList; -import java.util.List; - import master.flame.danmaku.danmaku.model.AbsDisplayer; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.DanmakuTimer; @@ -165,17 +162,17 @@ protected synchronized void removeUnusedLiveDanmakusIn(int msec) { } @Override - public List getVisibleDanmakusOnTime(long time) { + public IDanmakus getVisibleDanmakusOnTime(long time) { long beginMills = time - DanmakuFactory.MAX_DANMAKU_DURATION - 100; long endMills = time + DanmakuFactory.MAX_DANMAKU_DURATION; IDanmakus subDanmakus = danmakuList.sub(beginMills, endMills); - List visibleDanmakus = new ArrayList(); + IDanmakus visibleDanmakus = new Danmakus(); if (null != subDanmakus && !subDanmakus.isEmpty()) { IDanmakuIterator iterator = subDanmakus.iterator(); while (iterator.hasNext()) { BaseDanmaku danmaku = iterator.next(); if (danmaku.isShown() && !danmaku.isOutside()) { - visibleDanmakus.add(danmaku); + visibleDanmakus.addItem(danmaku); } } } 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 39dd2377..279673f9 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDanmakuView.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDanmakuView.java @@ -3,11 +3,8 @@ import android.view.View; -import java.util.List; - import master.flame.danmaku.controller.DrawHandler.Callback; 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.parser.BaseDanmakuParser; @@ -46,7 +43,7 @@ public interface IDanmakuView { public void removeAllLiveDanmakus(); - public List getCurrentVisibleDanmakus(); + public IDanmakus getCurrentVisibleDanmakus(); public void setCallback(Callback callback); @@ -112,7 +109,17 @@ public interface IDanmakuView { // ------------- Click Listener ------------------- public interface OnDanmakuClickListener { - void onDanmakuClick(BaseDanmaku danmaku); + /** + * + * @param latest the latest one is clicked + */ + void onDanmakuClick(BaseDanmaku latest); + + /** + * + * @param danmakus all to be clicked + */ + void onDanmakuClick(IDanmakus danmakus); } public void setOnDanmakuClickListener(OnDanmakuClickListener listener); 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 ca0f1ffa..2c360c9f 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDrawTask.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/controller/IDrawTask.java @@ -16,11 +16,8 @@ package master.flame.danmaku.controller; -import java.util.List; - 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.IDanmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; @@ -35,7 +32,7 @@ public interface IDrawTask { public void clearDanmakusOnScreen(long currMillis); - public List getVisibleDanmakusOnTime(long time); + public IDanmakus getVisibleDanmakusOnTime(long time); public RenderingState draw(AbsDisplayer displayer); 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 2f74d7c8..defd0ffa 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 @@ -27,20 +27,19 @@ import android.view.SurfaceView; import android.view.View; +import java.util.LinkedList; +import java.util.Locale; + import master.flame.danmaku.controller.DrawHandler; -import master.flame.danmaku.controller.IDanmakuView; -import master.flame.danmaku.controller.IDanmakuViewController; import master.flame.danmaku.controller.DrawHandler.Callback; import master.flame.danmaku.controller.DrawHelper; +import master.flame.danmaku.controller.IDanmakuView; +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.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; - public class DanmakuSurfaceView extends SurfaceView implements IDanmakuView, IDanmakuViewController, SurfaceHolder.Callback { public static final String TAG = "DanmakuSurfaceView"; @@ -115,7 +114,7 @@ public void removeAllLiveDanmakus() { } @Override - public List getCurrentVisibleDanmakus() { + public IDanmakus getCurrentVisibleDanmakus() { if (handler != null) { return handler.getCurrentVisibleDanmakus(); } 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 2c90b4d4..b92834b8 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 @@ -29,20 +29,19 @@ import android.view.TextureView; import android.view.View; +import java.util.LinkedList; +import java.util.Locale; + import master.flame.danmaku.controller.DrawHandler; -import master.flame.danmaku.controller.IDanmakuView; -import master.flame.danmaku.controller.IDanmakuViewController; import master.flame.danmaku.controller.DrawHandler.Callback; import master.flame.danmaku.controller.DrawHelper; +import master.flame.danmaku.controller.IDanmakuView; +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.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; - /** * DanmakuTextureView需要开启GPU加速才能显示弹幕 * 很遗憾...经过测试TextureView没有提升绘制速度,也许哪里用的不对 @@ -123,7 +122,7 @@ public void removeAllLiveDanmakus() { } @Override - public List getCurrentVisibleDanmakus() { + public IDanmakus getCurrentVisibleDanmakus() { if (handler != null) { return handler.getCurrentVisibleDanmakus(); } diff --git a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuTouchHelper.java b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuTouchHelper.java index 5e0f7c8f..d380fd2b 100644 --- a/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuTouchHelper.java +++ b/DanmakuFlameMaster/src/main/java/master/flame/danmaku/ui/widget/DanmakuTouchHelper.java @@ -3,12 +3,11 @@ import android.graphics.RectF; import android.view.MotionEvent; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - import master.flame.danmaku.controller.IDanmakuView; import master.flame.danmaku.danmaku.model.BaseDanmaku; +import master.flame.danmaku.danmaku.model.IDanmakuIterator; +import master.flame.danmaku.danmaku.model.IDanmakus; +import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.util.DanmakuUtils; /** @@ -18,12 +17,12 @@ public class DanmakuTouchHelper { private IDanmakuView danmakuView; private RectF mDanmakuBounds; - private List hitDanmakuList; + private IDanmakus hitDanmakus; private DanmakuTouchHelper(IDanmakuView danmakuView) { this.danmakuView = danmakuView; this.mDanmakuBounds = new RectF(); - this.hitDanmakuList = new ArrayList(); + this.hitDanmakus = new Danmakus(); } public static synchronized DanmakuTouchHelper instance(IDanmakuView danmakuView) { @@ -33,9 +32,15 @@ public static synchronized DanmakuTouchHelper instance(IDanmakuView danmakuView) public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: - BaseDanmaku clickDanmaku = touchHitDanmaku(event.getX(), event.getY()); - if (null != clickDanmaku) { - performClick(clickDanmaku); + IDanmakus clickDanmakus = touchHitDanmaku(event.getX(), event.getY()); + BaseDanmaku newestDanmaku = null; + if (null != clickDanmakus && !clickDanmakus.isEmpty()) { + performClick(clickDanmakus); + newestDanmaku = fetchLatestOne(clickDanmakus); + } + + if (null != newestDanmaku) { + performClickWithlatest(newestDanmaku); } break; default: @@ -45,44 +50,56 @@ public boolean onTouchEvent(MotionEvent event) { return false; } - private void performClick(BaseDanmaku danmaku) { + private void performClickWithlatest(BaseDanmaku newest) { if (danmakuView.getOnDanmakuClickListener() != null) { - danmakuView.getOnDanmakuClickListener().onDanmakuClick(danmaku); + danmakuView.getOnDanmakuClickListener().onDanmakuClick(newest); } } - private BaseDanmaku touchHitDanmaku(float x, float y) { - hitDanmakuList.clear(); - mDanmakuBounds.setEmpty(); + private void performClick(IDanmakus danmakus) { + if (danmakuView.getOnDanmakuClickListener() != null) { + danmakuView.getOnDanmakuClickListener().onDanmakuClick(danmakus); + } + } - List danmakus = danmakuView.getCurrentVisibleDanmakus(); + private IDanmakus touchHitDanmaku(float x, float y) { + hitDanmakus.clear(); + mDanmakuBounds.setEmpty(); + IDanmakus danmakus = danmakuView.getCurrentVisibleDanmakus(); if (null != danmakus && !danmakus.isEmpty()) { - Iterator iterator = danmakus.iterator(); + IDanmakuIterator iterator = danmakus.iterator(); while (iterator.hasNext()) { BaseDanmaku danmaku = iterator.next(); mDanmakuBounds.set(danmaku.getLeft(), danmaku.getTop(), danmaku.getRight(), danmaku.getBottom()); if (mDanmakuBounds.contains(x, y)) { - hitDanmakuList.add(danmaku); + hitDanmakus.addItem(danmaku); } } - if (!hitDanmakuList.isEmpty()) { - final int size = hitDanmakuList.size(); - BaseDanmaku newestDanmaku = null; - for (int i = 0; i < size; i++) { - BaseDanmaku hitDanmaku = hitDanmakuList.get(i); - if (null == newestDanmaku - || DanmakuUtils.compare(hitDanmaku, newestDanmaku) > 0) { - newestDanmaku = hitDanmaku; - } - } - return newestDanmaku; + } + + return hitDanmakus; + } + + private BaseDanmaku fetchLatestOne(IDanmakus danmakus) { + if (danmakus.isEmpty()) { + return null; + } + + BaseDanmaku newestDanmaku = null; + IDanmakuIterator hitIterator = danmakus.iterator(); + while (hitIterator.hasNext()) { + BaseDanmaku hitDanmaku = hitIterator.next(); + if (null == newestDanmaku + || DanmakuUtils.compare(hitDanmaku, newestDanmaku) > 0) { + newestDanmaku = hitDanmaku; } } - return null; + return newestDanmaku; + } } 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 b08c1f93..9aa0db1c 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 @@ -28,7 +28,6 @@ import android.view.View; import java.util.LinkedList; -import java.util.List; import java.util.Locale; import master.flame.danmaku.controller.DrawHandler; @@ -37,6 +36,7 @@ import master.flame.danmaku.controller.IDanmakuView; 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.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState; @@ -116,7 +116,7 @@ public void removeAllLiveDanmakus() { } @Override - public List getCurrentVisibleDanmakus() { + public IDanmakus getCurrentVisibleDanmakus() { if (handler != null) { return handler.getCurrentVisibleDanmakus(); } diff --git a/Sample/src/main/java/com/sample/MainActivity.java b/Sample/src/main/java/com/sample/MainActivity.java index 102b9fde..ce8f1236 100644 --- a/Sample/src/main/java/com/sample/MainActivity.java +++ b/Sample/src/main/java/com/sample/MainActivity.java @@ -35,6 +35,7 @@ import master.flame.danmaku.danmaku.loader.android.DanmakuLoaderFactory; 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.android.Danmakus; import master.flame.danmaku.danmaku.model.android.SpannedCacheStuffer; @@ -180,8 +181,13 @@ public void prepared() { }); mDanmakuView.setOnDanmakuClickListener(new IDanmakuView.OnDanmakuClickListener() { @Override - public void onDanmakuClick(BaseDanmaku danmaku) { - Log.d("DFM", "onDanmakuClick text:" + danmaku.text); + public void onDanmakuClick(BaseDanmaku latest) { + Log.d("DFM", "onDanmakuClick text:" + latest.text); + } + + @Override + public void onDanmakuClick(IDanmakus danmakus) { + Log.d("DFM", "onDanmakuClick danmakus size:" + danmakus.size()); } });