@SuppressLint("DrawAllocation") @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { containerSized(new Size(r - l, b - t)); if(surfaceView != null) { if (surfaceRect == null) { // Match the container, to reduce the risk of issues. The preview should never be drawn // while the surface has this size. surfaceView.layout(0, 0, getWidth(), getHeight()); } else { surfaceView.layout(surfaceRect.left, surfaceRect.top, surfaceRect.right, surfaceRect.bottom); } } else if(textureView != null) { textureView.layout(0, 0, getWidth(), getHeight()); } }
public class CroppedCameraPreview extends ViewGroup { private SurfaceView cameraPreview; public CroppedCameraPreview( Context context ) { super( context ); // i'd probably create and add the SurfaceView here, but it doesn't matter } @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) { setMeasuredDimension( croppedWidth, croppedHeight ); } @Override protected void onLayout( boolean changed, int l, int t, int r, int b ) { if ( cameraPreview != null ) { cameraPreview.layout( 0, 0, actualPreviewWidth, actualPreviewHeight ); } } }
public class CroppedCameraPreview extends ViewGroup { private SurfaceView cameraPreview; public CroppedCameraPreview( Context context ) { super( context ); // i'd probably create and add the SurfaceView here, but it doesn't matter } @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) { setMeasuredDimension( croppedWidth, croppedHeight ); } @Override protected void onLayout( boolean changed, int l, int t, int r, int b ) { if ( cameraPreview != null ) { cameraPreview.layout( 0, 0, actualPreviewWidth, actualPreviewHeight ); } } }
public class BarcodeCameraPreview extends ViewGroup { private SurfaceView mSurfaceView; public BarcodeCameraPreview(final Context context, AttributeSet attrs) { super(context, attrs); mSurfaceView = new SurfaceView(context); mSurfaceView.getHolder().addCallback(new SurfaceCallback()); addView(mSurfaceView); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // crop view int actualPreviewWidth = getResources().getDisplayMetrics().widthPixels; int actualPreviewHeight = getResources().getDisplayMetrics().heightPixels; if (mSurfaceView != null) { mSurfaceView.layout(0, 0, actualPreviewWidth, actualPreviewHeight); } } // camera methods }
mSurfaceView.layout(0, 0, actualPreviewWidth, actualPreviewHeight);