Fix layout editor preview (#564)

pull/565/head
Mattia Iavarone 5 years ago committed by GitHub
parent f2ea77ce79
commit 83307c527e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 37
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  2. 6
      demo/src/main/res/layout/activity_camera.xml

@ -138,6 +138,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
private boolean mKeepScreenOn; private boolean mKeepScreenOn;
@SuppressWarnings({"FieldCanBeLocal", "unused"}) @SuppressWarnings({"FieldCanBeLocal", "unused"})
private boolean mExperimental; private boolean mExperimental;
private boolean mInEditor;
// Overlays // Overlays
@VisibleForTesting OverlayLayout mOverlayLayout; @VisibleForTesting OverlayLayout mOverlayLayout;
@ -160,6 +161,9 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
@SuppressWarnings("WrongConstant") @SuppressWarnings("WrongConstant")
private void initialize(@NonNull Context context, @Nullable AttributeSet attrs) { private void initialize(@NonNull Context context, @Nullable AttributeSet attrs) {
mInEditor = isInEditMode();
if (mInEditor) return;
setWillNotDraw(false); setWillNotDraw(false);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0);
ControlParser controls = new ControlParser(context, a); ControlParser controls = new ControlParser(context, a);
@ -244,9 +248,8 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
// Apply filters // Apply filters
setFilter(filters.getFilter()); setFilter(filters.getFilter());
if (!isInEditMode()) { // Create the orientation helper
mOrientationHelper = new OrientationHelper(context, mCameraCallbacks); mOrientationHelper = new OrientationHelper(context, mCameraCallbacks);
}
} }
/** /**
@ -324,22 +327,18 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
@Override @Override
protected void onAttachedToWindow() { protected void onAttachedToWindow() {
super.onAttachedToWindow(); super.onAttachedToWindow();
if (mInEditor) return;
if (mCameraPreview == null) { if (mCameraPreview == null) {
// isHardwareAccelerated will return the real value only after we are // isHardwareAccelerated will return the real value only after we are
// attached. That's why we instantiate the preview here. // attached. That's why we instantiate the preview here.
doInstantiatePreview(); doInstantiatePreview();
} }
if (!isInEditMode()) { mOrientationHelper.enable(getContext());
mOrientationHelper.enable(getContext());
}
} }
@Override @Override
protected void onDetachedFromWindow() { protected void onDetachedFromWindow() {
if (!isInEditMode()) { if (!mInEditor) mOrientationHelper.disable();
mOrientationHelper.disable();
}
super.onDetachedFromWindow(); super.onDetachedFromWindow();
} }
@ -374,6 +373,14 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
*/ */
@Override @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mInEditor) {
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int height = MeasureSpec.getSize(heightMeasureSpec);
super.onMeasure(MeasureSpec.makeMeasureSpec(width, EXACTLY),
MeasureSpec.makeMeasureSpec(height, EXACTLY));
return;
}
Size previewSize = mCameraEngine.getPreviewStreamSize(Reference.VIEW); Size previewSize = mCameraEngine.getPreviewStreamSize(Reference.VIEW);
if (previewSize == null) { if (previewSize == null) {
LOG.w("onMeasure:", "surface is not ready. Calling default behavior."); LOG.w("onMeasure:", "surface is not ready. Calling default behavior.");
@ -595,7 +602,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
// Some gesture layout detected a gesture. It's not known at this moment: // Some gesture layout detected a gesture. It's not known at this moment:
// (1) if it was mapped to some action (we check here) // (1) if it was mapped to some action (we check here)
// (2) if it's supported by the camera (CameraEngine checks) // (2) if it's supported by the camera (CameraEngine checks)
private void onGesture(GestureFinder source, @NonNull CameraOptions options) { private void onGesture(@NonNull GestureFinder source, @NonNull CameraOptions options) {
Gesture gesture = source.getGesture(); Gesture gesture = source.getGesture();
GestureAction action = mGestureMap.get(gesture); GestureAction action = mGestureMap.get(gesture);
PointF[] points = source.getPoints(); PointF[] points = source.getPoints();
@ -691,7 +698,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
*/ */
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void open() { public void open() {
if (!isEnabled()) return; if (mInEditor) return;
if (mCameraPreview != null) mCameraPreview.onResume(); if (mCameraPreview != null) mCameraPreview.onResume();
if (checkPermissions(getAudio())) { if (checkPermissions(getAudio())) {
// Update display orientation for current CameraEngine // Update display orientation for current CameraEngine
@ -760,6 +767,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
*/ */
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void close() { public void close() {
if (mInEditor) return;
mCameraEngine.stop(); mCameraEngine.stop();
if (mCameraPreview != null) mCameraPreview.onPause(); if (mCameraPreview != null) mCameraPreview.onPause();
} }
@ -771,6 +779,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
*/ */
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void destroy() { public void destroy() {
if (mInEditor) return;
clearCameraListeners(); clearCameraListeners();
clearFrameProcessors(); clearFrameProcessors();
mCameraEngine.destroy(); mCameraEngine.destroy();
@ -2158,7 +2167,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
@Override @Override
public LayoutParams generateLayoutParams(AttributeSet attributeSet) { public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
if (mOverlayLayout.isOverlay(attributeSet)) { if (!mInEditor && mOverlayLayout.isOverlay(attributeSet)) {
return mOverlayLayout.generateLayoutParams(attributeSet); return mOverlayLayout.generateLayoutParams(attributeSet);
} }
return super.generateLayoutParams(attributeSet); return super.generateLayoutParams(attributeSet);
@ -2167,7 +2176,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
// We don't support removeView on overlays for now. // We don't support removeView on overlays for now.
@Override @Override
public void addView(View child, int index, ViewGroup.LayoutParams params) { public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (mOverlayLayout.isOverlay(params)) { if (!mInEditor && mOverlayLayout.isOverlay(params)) {
mOverlayLayout.addView(child, params); mOverlayLayout.addView(child, params);
} else { } else {
super.addView(child, index, params); super.addView(child, index, params);

@ -37,10 +37,10 @@
android:layout_width="72dp" android:layout_width="72dp"
android:layout_height="72dp" android:layout_height="72dp"
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
app:layout_drawOnPreview="true" android:src="@mipmap/logo_foreground"
app:layout_drawOnVideoSnapshot="true"
app:layout_drawOnPictureSnapshot="true" app:layout_drawOnPictureSnapshot="true"
android:src="@mipmap/logo_foreground"/> app:layout_drawOnPreview="true"
app:layout_drawOnVideoSnapshot="true" />
</com.otaliastudios.cameraview.CameraView> </com.otaliastudios.cameraview.CameraView>

Loading…
Cancel
Save