fix overlay drawing

pull/421/head
Giacomo Randazzo 7 years ago
parent 809b25a135
commit 86a7a0f1f3
  1. 64
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  2. 49
      cameraview/src/main/java/com/otaliastudios/cameraview/OverlayLayout.java
  3. 5
      cameraview/src/main/res/layout/cameraview_gl_view.xml
  4. 6
      cameraview/src/main/res/values/attrs.xml
  5. 3
      demo/src/main/res/layout/activity_camera.xml

@ -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);
}
}
}
}
@ -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

@ -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();

@ -6,11 +6,6 @@
android:layout_gravity="center"
android:gravity="center">
<com.otaliastudios.cameraview.OverlayLayout
android:id="@+id/no_preview_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.opengl.GLSurfaceView
android:id="@+id/gl_surface_view"
android:layout_width="match_parent"

@ -127,11 +127,5 @@
<attr name="layout_overlay" format="boolean"/>
<attr name="layout_showInPreview" format="boolean"/>
<attr name="layout_showInPicture" format="boolean"/>
<attr name="layout_showInVideo" format="boolean"/>
</declare-styleable>
</resources>

@ -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"
/>
</com.otaliastudios.cameraview.CameraView>

Loading…
Cancel
Save