Specify where to draw the overlay

pull/421/head
Giacomo Randazzo 6 years ago
parent a74ef36f2f
commit 3d0c7ffacb
  1. 44
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  2. 42
      cameraview/src/main/java/com/otaliastudios/cameraview/OverlayLayout.java
  3. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotPictureRecorder.java
  4. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotVideoRecorder.java
  5. 3
      cameraview/src/main/java/com/otaliastudios/cameraview/SurfaceDrawer.java
  6. 4
      cameraview/src/main/res/values/attrs.xml
  7. 14
      demo/src/main/res/layout/activity_camera.xml

@ -299,12 +299,10 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
@Override
public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
TypedArray a = getContext().obtainStyledAttributes(attributeSet, R.styleable.CameraView_Layout);
if (a.getBoolean(R.styleable.CameraView_Layout_layout_isOverlay, false)) {
a.recycle();
return new OverlayLayoutParams(this.getContext(), attributeSet);
OverlayLayoutParams toBeChecked = new OverlayLayoutParams(this.getContext(), attributeSet);
if (toBeChecked.isOverlay()) {
return toBeChecked;
}
a.recycle();
return super.generateLayoutParams(attributeSet);
}
@ -1865,7 +1863,9 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
public static class OverlayLayoutParams extends FrameLayout.LayoutParams {
private boolean isOverlay = false;
private boolean drawInPreview = false;
private boolean drawInPictureSnapshot = false;
private boolean drawInVideoSnapshot = false;
public OverlayLayoutParams(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
@ -1883,18 +1883,40 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
private void readStyleParameters(Context context, AttributeSet attributeSet) {
TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.CameraView_Layout);
try {
this.isOverlay = a.getBoolean(R.styleable.CameraView_Layout_layout_isOverlay, false);
this.drawInPreview = a.getBoolean(R.styleable.CameraView_Layout_layout_drawInPreview, false);
this.drawInPictureSnapshot = a.getBoolean(R.styleable.CameraView_Layout_layout_drawInPictureSnapshot, false);
this.drawInVideoSnapshot = a.getBoolean(R.styleable.CameraView_Layout_layout_drawInVideoSnapshot, false);
} finally {
a.recycle();
}
}
public boolean isOverlay() {
return isOverlay;
public boolean isDrawInPreview() {
return drawInPreview;
}
public void setDrawInPreview(boolean drawInPreview) {
this.drawInPreview = drawInPreview;
}
public void setOverlay(boolean overlay) {
isOverlay = overlay;
public boolean isDrawInPictureSnapshot() {
return drawInPictureSnapshot;
}
public void setDrawInPictureSnapshot(boolean drawInPictureSnapshot) {
this.drawInPictureSnapshot = drawInPictureSnapshot;
}
public boolean isDrawInVideoSnapshot() {
return drawInVideoSnapshot;
}
public void setDrawInVideoSnapshot(boolean drawInVideoSnapshot) {
this.drawInVideoSnapshot = drawInVideoSnapshot;
}
public boolean isOverlay() {
return drawInPreview || drawInPictureSnapshot || drawInVideoSnapshot;
}
}

@ -26,7 +26,7 @@ class OverlayLayout extends FrameLayout implements SurfaceDrawer {
}
@Override
public void drawOnSurface(Surface outputSurface) {
public void drawOnSurfaceForPictureSnapshot(Surface outputSurface) {
try {
final Canvas surfaceCanvas = outputSurface.lockCanvas(null);
@ -34,11 +34,49 @@ class OverlayLayout extends FrameLayout implements SurfaceDrawer {
float yScale = surfaceCanvas.getHeight() / (float) getHeight();
surfaceCanvas.scale(xScale, yScale);
surfaceCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
super.draw(surfaceCanvas);
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (((CameraView.OverlayLayoutParams) child.getLayoutParams()).isDrawInPictureSnapshot()) {
drawChild(surfaceCanvas, child, getDrawingTime());
}
}
outputSurface.unlockCanvasAndPost(surfaceCanvas);
} catch (Surface.OutOfResourcesException e) {
e.printStackTrace();
}
}
@Override
public void drawOnSurfaceForVideoSnapshot(Surface outputSurface) {
try {
final Canvas surfaceCanvas = outputSurface.lockCanvas(null);
float xScale = surfaceCanvas.getWidth() / (float) getWidth();
float yScale = surfaceCanvas.getHeight() / (float) getHeight();
surfaceCanvas.scale(xScale, yScale);
surfaceCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i); if (((CameraView.OverlayLayoutParams) child.getLayoutParams()).isDrawInVideoSnapshot()) {
drawChild(surfaceCanvas, child, getDrawingTime());
}
}
outputSurface.unlockCanvasAndPost(surfaceCanvas);
} catch (Surface.OutOfResourcesException e) {
e.printStackTrace();
}
}
@Override
public void draw(Canvas canvas) {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i); if (((CameraView.OverlayLayoutParams) child.getLayoutParams()).isDrawInPreview()) {
drawChild(canvas, child, getDrawingTime());
}
}
}
}

@ -134,7 +134,7 @@ class SnapshotPictureRecorder extends PictureRecorder {
Surface drawOnto = new Surface(mOverlaySurfaceTexture);
if (mWithOverlay) {
for (SurfaceDrawer surfaceDrawer : mSurfaceDrawerList) {
surfaceDrawer.drawOnSurface(drawOnto);
surfaceDrawer.drawOnSurfaceForPictureSnapshot(drawOnto);
}
mOverlaySurfaceTexture.updateTexImage();
mOverlaySurfaceTexture.getTransformMatrix(mOverlayTransform);

@ -131,7 +131,7 @@ class SnapshotVideoRecorder extends VideoRecorder implements GlCameraPreview.Ren
// get overlay
if (mOverlaySurfaceTexture != null) {
for (SurfaceDrawer surfaceDrawer : mSurfaceDrawerList) {
surfaceDrawer.drawOnSurface(mOverlaySurface);
surfaceDrawer.drawOnSurfaceForVideoSnapshot(mOverlaySurface);
}
mOverlaySurfaceTexture.updateTexImage();
mOverlaySurfaceTexture.getTransformMatrix(textureFrame.overlayTransform);

@ -3,5 +3,6 @@ package com.otaliastudios.cameraview;
import android.view.Surface;
interface SurfaceDrawer {
void drawOnSurface(Surface surface);
void drawOnSurfaceForPictureSnapshot(Surface surface);
void drawOnSurfaceForVideoSnapshot(Surface surface);
}

@ -128,7 +128,9 @@
<declare-styleable name="CameraView_Layout">
<attr name="layout_isOverlay" format="boolean"/>
<attr name="layout_drawInPreview" format="boolean"/>
<attr name="layout_drawInPictureSnapshot" format="boolean"/>
<attr name="layout_drawInVideoSnapshot" format="boolean"/>
</declare-styleable>
</resources>

@ -33,7 +33,9 @@
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom|end"
app:layout_isOverlay="true"
app:layout_drawInPreview="false"
app:layout_drawInVideoSnapshot="true"
app:layout_drawInPictureSnapshot="true"
android:gravity="center"
android:padding="8dp"
>
@ -54,6 +56,16 @@
</LinearLayout>
<TextView
android:id="@+id/other_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CameraView"
app:layout_drawInPreview="true"
app:layout_drawInPictureSnapshot="true"
android:textColor="@android:color/white"
/>
</com.otaliastudios.cameraview.CameraView>

Loading…
Cancel
Save