From 86a7a0f1f395fd26d3fbd81e746114f893bcdf47 Mon Sep 17 00:00:00 2001 From: Giacomo Randazzo Date: Sun, 20 Jan 2019 11:57:19 +0100 Subject: [PATCH] fix overlay drawing --- .../otaliastudios/cameraview/CameraView.java | 66 +++++++------------ .../cameraview/OverlayLayout.java | 49 ++------------ .../main/res/layout/cameraview_gl_view.xml | 5 -- cameraview/src/main/res/values/attrs.xml | 6 -- demo/src/main/res/layout/activity_camera.xml | 3 - 5 files changed, 27 insertions(+), 102 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index fabe1c82..e576cf4e 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -81,6 +81,10 @@ public class CameraView extends FrameLayout implements LifecycleObserver { private Handler mUiHandler; private WorkerHandler mFrameProcessorsHandler; + // Overlay + private boolean mHasOverlay = false; + private OverlayLayout mPreviewOverlayLayout; + public CameraView(@NonNull Context context) { super(context, null); init(context, null); @@ -253,7 +257,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { } case GL_SURFACE: default: { mPreview = Preview.GL_SURFACE; - return new GlCameraPreview(context, container, null, true); + return new GlCameraPreview(context, container, null, mHasOverlay); } } } @@ -263,26 +267,30 @@ public class CameraView extends FrameLayout implements LifecycleObserver { mCameraController.setPreview(mCameraPreview); } - private OverlayLayout mPreviewOverlayLayout; - private OverlayLayout mNoPreviewOverlayLayout; - @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (mCameraPreview == null) { + + // check if we have any overlay view before instantiating preview + for (int i = 0; i < getChildCount(); i++) { + View view = getChildAt(i); + if (view.getLayoutParams() instanceof OverlayLayoutParams && + ((OverlayLayoutParams) view.getLayoutParams()).isOverlay) { + mHasOverlay = true; + } + } + // isHardwareAccelerated will return the real value only after we are // attached. That's why we instantiate the preview here. instantiatePreview(); mPreviewOverlayLayout = findViewById(R.id.preview_overlay_layout); - mNoPreviewOverlayLayout = findViewById(R.id.no_preview_overlay_layout); - mNoPreviewOverlayLayout.setDrawOnScreen(false); ((GlCameraPreview) mCameraPreview).addOverlayInputSurfaceListener(new GlCameraPreview.OverlayInputSurfaceListener() { @Override public void onSurface(@NonNull Surface surface) { mPreviewOverlayLayout.setOutputSurface(surface); - mNoPreviewOverlayLayout.setOutputSurface(surface); } }); @@ -299,12 +307,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { View view = getChildAt(i); removeViewAt(i); - if (((OverlayLayoutParams) view.getLayoutParams()).showInPreview) { - mPreviewOverlayLayout.addView(view); - } else { - mNoPreviewOverlayLayout.addView(view); - } - + mPreviewOverlayLayout.addView(view); } } } @@ -1111,6 +1114,13 @@ public class CameraView extends FrameLayout implements LifecycleObserver { return mCameraController.getAudio(); } + /** + * Gets a boolean which is true if there is at least one overlay view. + * @return a boolean which is true if there is at least one overlay view + */ + public boolean hasOverlay() { + return mHasOverlay; + } /** * Starts an autofocus process at the given coordinates, with respect @@ -1888,9 +1898,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver { public static class OverlayLayoutParams extends FrameLayout.LayoutParams { private boolean isOverlay = false; - private boolean showInPreview = false; - private boolean showInPicture = false; - private boolean showInVideo = false; public OverlayLayoutParams(Context context, AttributeSet attributeSet) { super(context, attributeSet); @@ -1909,9 +1916,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver { TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.CameraView_Layout); try { this.isOverlay = a.getBoolean(R.styleable.CameraView_Layout_layout_overlay, false); - this.showInPreview = a.getBoolean(R.styleable.CameraView_Layout_layout_showInPreview, false); - this.showInPicture = a.getBoolean(R.styleable.CameraView_Layout_layout_showInPicture, false); - this.showInVideo = a.getBoolean(R.styleable.CameraView_Layout_layout_showInVideo, false); } finally { a.recycle(); } @@ -1924,30 +1928,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver { public void setOverlay(boolean overlay) { isOverlay = overlay; } - - public boolean isShowInPreview() { - return showInPreview; - } - - public void setShowInPreview(boolean showInPreview) { - this.showInPreview = showInPreview; - } - - public boolean isShowInPicture() { - return showInPicture; - } - - public void setShowInPicture(boolean showInPicture) { - this.showInPicture = showInPicture; - } - - public boolean isShowInVideo() { - return showInVideo; - } - - public void setShowInVideo(boolean showInVideo) { - this.showInVideo = showInVideo; - } } //endregion diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/OverlayLayout.java b/cameraview/src/main/java/com/otaliastudios/cameraview/OverlayLayout.java index b30a843d..4b4a1472 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/OverlayLayout.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/OverlayLayout.java @@ -5,7 +5,6 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; -import android.util.Log; import android.view.Surface; import android.view.View; import android.view.ViewGroup; @@ -17,7 +16,6 @@ import androidx.annotation.Nullable; public class OverlayLayout extends FrameLayout { private Surface outputSurface = null; - private boolean drawOnScreen = true; public OverlayLayout(@NonNull Context context) { super(context); @@ -36,12 +34,6 @@ public class OverlayLayout extends FrameLayout { try { final Canvas surfaceCanvas = outputSurface.lockCanvas(null); - Paint drawPaint = new Paint(); - drawPaint.setColor(Color.GREEN); - drawPaint.setStrokeWidth(5); - drawPaint.setStyle(Paint.Style.FILL_AND_STROKE); - surfaceCanvas.drawCircle(200, 200, 40, drawPaint); - float xScale = surfaceCanvas.getWidth() / (float) canvas.getWidth(); surfaceCanvas.scale(xScale, xScale); super.draw(surfaceCanvas); @@ -52,41 +44,12 @@ public class OverlayLayout extends FrameLayout { } } - if (drawOnScreen) { - // draw the view normally - super.draw(canvas); - } + // draw the view as always, since we are already drawing on the preview this step can be + // skipped + //super.draw(canvas); } -// @Override -// protected void onDraw(Canvas canvas) { -// if (outputSurface != null ) { -// // Requires a try/catch for .lockCanvas( null ) -// try { -// final Canvas surfaceCanvas = outputSurface.lockCanvas(null); -// super.onDraw(surfaceCanvas); -// -// Log.d("ciao", "onDraw: canvas size: " + surfaceCanvas.getWidth() + " " + surfaceCanvas.getHeight()); -// Log.d("ciao", "onDraw: w h: " + getWidth() + " " + getHeight()); -// -// Paint drawPaint = new Paint(); -// drawPaint.setColor(Color.GREEN); -// drawPaint.setStrokeWidth(5); -// drawPaint.setStyle(Paint.Style.FILL_AND_STROKE); -// -// surfaceCanvas.drawCircle(200, 200, 40, drawPaint); -// -// outputSurface.unlockCanvasAndPost(surfaceCanvas); -// } catch (Surface.OutOfResourcesException e) { -// e.printStackTrace(); -// } -// } -// -// if (drawOnScreen) { -// // draw the view normally -// super.onDraw(canvas); -// } -// } + public void setOutputSurface(Surface outputSurface) { this.outputSurface = outputSurface; @@ -96,10 +59,6 @@ public class OverlayLayout extends FrameLayout { postInvalidateRecursive(this); } - public void setDrawOnScreen(boolean drawOnScreen) { - this.drawOnScreen = drawOnScreen; - } - // invalidates children (and nested children) recursively private void postInvalidateRecursive(ViewGroup layout) { int count = layout.getChildCount(); diff --git a/cameraview/src/main/res/layout/cameraview_gl_view.xml b/cameraview/src/main/res/layout/cameraview_gl_view.xml index f18da9ec..670c34d2 100644 --- a/cameraview/src/main/res/layout/cameraview_gl_view.xml +++ b/cameraview/src/main/res/layout/cameraview_gl_view.xml @@ -6,11 +6,6 @@ android:layout_gravity="center" android:gravity="center"> - - - - - - - - \ No newline at end of file diff --git a/demo/src/main/res/layout/activity_camera.xml b/demo/src/main/res/layout/activity_camera.xml index 76b0251d..f93f4939 100644 --- a/demo/src/main/res/layout/activity_camera.xml +++ b/demo/src/main/res/layout/activity_camera.xml @@ -35,9 +35,6 @@ android:textColor="@android:color/white" android:layout_gravity="bottom|end" app:layout_overlay="true" - app:layout_showInPreview="true" - app:layout_showInPicture="true" - app:layout_showInVideo="false" />