google / cameraview

[DEPRECATED] Easily integrate Camera features into your Android app
Apache License 2.0
4.74k stars 1.03k forks source link

camera view lag showing when call mCamera.stop() #234

Open ashish0121 opened 6 years ago

ashish0121 commented 6 years ago

The cameraview object is causing lag when used in a view pager. When scrolling back from cameraview to anothertab, the transition isnt smooth. Can you please suggest if there is a way to handle it smoothly.

kelvinwatson commented 5 years ago

@ashish0121 Any update on this?

ashish0121 commented 5 years ago

1) In view pager when ViewPager.SimpleOnPageChangeListener() function onPageSelected is called, then you can call in your mCamera.stop() if it doesnt match with camera preview page. 2) The view pager was still causing jerk but to a lesser extent. So it was kind of a partial fix. 3) You can try combination of coordinator layout with view pager though i haven't tried it myself.

kelvinwatson commented 5 years ago

Yes that is what I'm doing, except I'm starting up another camera in another fragment when I swipe to switch fragments, i.e. I call mCamera1.stop() for Fragment1, and mCamera2.start() for Fragment2.

kelvinwatson commented 5 years ago

I was able to resolve this using an async task:

private static class StopStartCameraAsyncTask extends AsyncTask<Void, Void, Void>
    {
        private final CameraView cameraToStop;
        private final CameraView cameraToStart;

        StopStartCameraAsyncTask(@NonNull CameraViewListener cameraToStop, @NonNull CameraViewListener cameraToStart)
        {
            this.cameraToStart = cameraToStart;
            this.cameraToStop = cameraToStop;
        }

        @Override
        protected Void doInBackground(Void... voids)
        {
            cameraToStop.stopCamera();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid)
        {
            super.onPostExecute(aVoid);
            cameraToStart.startCamera();
        }
    }