Migrate constructor code to init and use a single worker thread.

- Moving the initialization code to an init function allows the creation of
the CameraView in code using the single context constructor.
- Creating a single HandlerThread will avoid having to create new threads
for every operation.
pull/1/head
Yuval Peress 8 years ago
parent 4c2d2a271a
commit 2ab9043851
  1. 36
      camerakit/src/main/java/com/flurgle/camerakit/CameraView.java

@ -8,6 +8,8 @@ import android.content.pm.PackageManager;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.YuvImage; import android.graphics.YuvImage;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
@ -37,6 +39,17 @@ import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_STRICT;
public class CameraView extends FrameLayout { public class CameraView extends FrameLayout {
private static Handler sWorkerHandler;
static {
// Initialize a single worker thread. This can be static since only a single camera
// reference can exist at a time.
HandlerThread workerThread = new HandlerThread("CameraViewWorker");
workerThread.setDaemon(true);
workerThread.start();
sWorkerHandler = new Handler(workerThread.getLooper());
}
@Facing @Facing
private int mFacing; private int mFacing;
@ -57,24 +70,30 @@ public class CameraView extends FrameLayout {
@VideoQuality @VideoQuality
private int mVideoQuality; private int mVideoQuality;
private int mJpegQuality; private int mJpegQuality;
private boolean mCropOutput; private boolean mCropOutput;
private boolean mAdjustViewBounds;
private boolean mAdjustViewBounds;
private CameraListenerMiddleWare mCameraListener; private CameraListenerMiddleWare mCameraListener;
private DisplayOrientationDetector mDisplayOrientationDetector;
private DisplayOrientationDetector mDisplayOrientationDetector;
private CameraImpl mCameraImpl; private CameraImpl mCameraImpl;
private PreviewImpl mPreviewImpl; private PreviewImpl mPreviewImpl;
public CameraView(@NonNull Context context) { public CameraView(@NonNull Context context) {
super(context, null); super(context, null);
init(context, null);
} }
@SuppressWarnings("all") @SuppressWarnings("all")
public CameraView(@NonNull Context context, @Nullable AttributeSet attrs) { public CameraView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
init(context, attrs);
}
@SuppressWarnings("WrongConstant")
private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
if (attrs != null) { if (attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes( TypedArray a = context.getTheme().obtainStyledAttributes(
attrs, attrs,
@ -210,28 +229,27 @@ public class CameraView extends FrameLayout {
break; break;
} }
new Thread(new Runnable() { sWorkerHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
mCameraImpl.start(); mCameraImpl.start();
} }
}).start(); });
} }
public void stop() { public void stop() {
mCameraImpl.stop(); mCameraImpl.stop();
} }
public void setFacing(@Facing public void setFacing(@Facing final int facing) {
final int facing) {
this.mFacing = facing; this.mFacing = facing;
new Thread(new Runnable() { sWorkerHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
mCameraImpl.setFacing(facing); mCameraImpl.setFacing(facing);
} }
}).start(); });
} }
public void setFlash(@Flash int flash) { public void setFlash(@Flash int flash) {

Loading…
Cancel
Save