clean adding View to OverlayLayout

pull/421/head
Giacomo Randazzo 6 years ago
parent 8b2d988fdf
commit a74ef36f2f
  1. 81
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  2. 5
      cameraview/src/main/res/layout/cameraview_gl_view.xml

@ -24,9 +24,7 @@ import androidx.annotation.ColorInt;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.Surface;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.FrameLayout; import android.widget.FrameLayout;
@ -34,7 +32,6 @@ import android.widget.FrameLayout;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -74,6 +71,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
PinchGestureLayout mPinchGestureLayout; PinchGestureLayout mPinchGestureLayout;
TapGestureLayout mTapGestureLayout; TapGestureLayout mTapGestureLayout;
ScrollGestureLayout mScrollGestureLayout; ScrollGestureLayout mScrollGestureLayout;
OverlayLayout mOverlayLayout;
private boolean mKeepScreenOn; private boolean mKeepScreenOn;
private boolean mExperimental; private boolean mExperimental;
@ -81,10 +79,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
private Handler mUiHandler; private Handler mUiHandler;
private WorkerHandler mFrameProcessorsHandler; private WorkerHandler mFrameProcessorsHandler;
// Overlay
private boolean mHasOverlay = false;
private OverlayLayout mPreviewOverlayLayout;
public CameraView(@NonNull Context context) { public CameraView(@NonNull Context context) {
super(context, null); super(context, null);
init(context, null); init(context, null);
@ -203,10 +197,12 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
mPinchGestureLayout = new PinchGestureLayout(context); mPinchGestureLayout = new PinchGestureLayout(context);
mTapGestureLayout = new TapGestureLayout(context); mTapGestureLayout = new TapGestureLayout(context);
mScrollGestureLayout = new ScrollGestureLayout(context); mScrollGestureLayout = new ScrollGestureLayout(context);
mOverlayLayout = new OverlayLayout(context);
addView(mGridLinesLayout); addView(mGridLinesLayout);
addView(mPinchGestureLayout); addView(mPinchGestureLayout);
addView(mTapGestureLayout); addView(mTapGestureLayout);
addView(mScrollGestureLayout); addView(mScrollGestureLayout);
addView(mOverlayLayout);
// Apply self managed // Apply self managed
setPlaySounds(playSounds); setPlaySounds(playSounds);
@ -272,41 +268,12 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
super.onAttachedToWindow(); super.onAttachedToWindow();
if (mCameraPreview == null) { if (mCameraPreview == null) {
// check if we have any overlay view before instantiating preview
boolean foundOverlay = false;
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
if (view.getLayoutParams() instanceof OverlayLayoutParams &&
((OverlayLayoutParams) view.getLayoutParams()).isOverlay) {
foundOverlay = true;
}
}
mHasOverlay = foundOverlay;
// 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.
instantiatePreview(); instantiatePreview();
mPreviewOverlayLayout = findViewById(R.id.preview_overlay_layout); mCameraController.addPictureSurfaceDrawer(mOverlayLayout);
mCameraController.addVideoSurfaceDrawer(mOverlayLayout);
LinkedList<View> overlayViews = new LinkedList<>();
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
if (view.getLayoutParams() instanceof OverlayLayoutParams &&
((OverlayLayoutParams) view.getLayoutParams()).isOverlay) {
overlayViews.add(view);
}
}
for (View view : overlayViews) {
if (view != null) {
Log.d(TAG, "onAttachedToWindow: ovel:" + view.toString());
removeView(view);
mPreviewOverlayLayout.addView(view);
}
}
mCameraController.addPictureSurfaceDrawer(mPreviewOverlayLayout);
mCameraController.addVideoSurfaceDrawer(mPreviewOverlayLayout);
} }
if (!isInEditMode()) { if (!isInEditMode()) {
mOrientationHelper.enable(getContext()); mOrientationHelper.enable(getContext());
@ -322,23 +289,31 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
} }
@Override @Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { public void addView(View child, ViewGroup.LayoutParams params) {
return p instanceof OverlayLayoutParams; if (params instanceof OverlayLayoutParams) {
} mOverlayLayout.addView(child, params);
} else {
@Override super.addView(child, params);
protected OverlayLayoutParams generateDefaultLayoutParams() { }
return new OverlayLayoutParams(OverlayLayoutParams.WRAP_CONTENT, OverlayLayoutParams.WRAP_CONTENT);
} }
@Override @Override
public OverlayLayoutParams generateLayoutParams(AttributeSet attributeSet) { public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
return new OverlayLayoutParams(this.getContext(), 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);
}
a.recycle();
return super.generateLayoutParams(attributeSet);
} }
@Override @Override
protected OverlayLayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new OverlayLayoutParams(p); if (p instanceof OverlayLayoutParams) {
return p;
}
return super.generateLayoutParams(p);
} }
//endregion //endregion
@ -1109,14 +1084,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
return mCameraController.getAudio(); 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 * Starts an autofocus process at the given coordinates, with respect
* to the view width and height. * to the view width and height.

@ -11,9 +11,4 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
<com.otaliastudios.cameraview.OverlayLayout
android:id="@+id/preview_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout> </FrameLayout>
Loading…
Cancel
Save