From b61c0dc33255c6c16e6bc56cebb27b3979fbad14 Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Fri, 5 Feb 2016 20:20:11 +0200 Subject: [PATCH] Keep drawing viewfinder after pausing. --- .../barcodescanner/ViewfinderView.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/zxing-android-embedded/src/com/journeyapps/barcodescanner/ViewfinderView.java b/zxing-android-embedded/src/com/journeyapps/barcodescanner/ViewfinderView.java index 3c457b83d..b38231380 100755 --- a/zxing-android-embedded/src/com/journeyapps/barcodescanner/ViewfinderView.java +++ b/zxing-android-embedded/src/com/journeyapps/barcodescanner/ViewfinderView.java @@ -59,6 +59,11 @@ public class ViewfinderView extends View { protected List lastPossibleResultPoints; protected CameraPreview cameraPreview; + // Cache the framingRect and previewFramingRect, so that we can still draw it after the preview + // stopped. + protected Rect framingRect; + protected Rect previewFramingRect; + // This constructor is used when the class is built from an XML resource. public ViewfinderView(Context context, AttributeSet attrs) { super(context, attrs); @@ -92,6 +97,7 @@ public void setCameraPreview(CameraPreview view) { view.addStateListener(new CameraPreview.StateListener() { @Override public void previewSized() { + refreshSizes(); invalidate(); } @@ -112,16 +118,29 @@ public void cameraError(Exception error) { }); } + protected void refreshSizes() { + if(cameraPreview == null) { + return; + } + Rect framingRect = cameraPreview.getFramingRect(); + Rect previewFramingRect = cameraPreview.getPreviewFramingRect(); + if(framingRect != null && previewFramingRect != null) { + this.framingRect = framingRect; + this.previewFramingRect = previewFramingRect; + } + } + @SuppressLint("DrawAllocation") @Override public void onDraw(Canvas canvas) { - if (cameraPreview == null || cameraPreview.getPreviewFramingRect() == null || cameraPreview.getFramingRect() == null) { + refreshSizes(); + if (framingRect == null || previewFramingRect == null) { return; } - Rect frame = cameraPreview.getFramingRect(); - Rect previewFrame = cameraPreview.getPreviewFramingRect(); + Rect frame = framingRect; + Rect previewFrame = previewFramingRect; int width = canvas.getWidth(); int height = canvas.getHeight();