I spent a lot of time and coffee, but found the cause of the trouble.
The problem in using SurfaceView to display a preview.
Use TextureView to display a preview.
It will be useful: How can I make my view pager more smooth?
Good luck!
UPDATED:
Added example of a TextureView
CameraModule.java
public class CameraModule implements SurfaceTextureListener {
private Camera mCamera;
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCamera = getCamera();
try {
mCamera.setPreviewTexture(surface);
mCamera.startPreview();
} catch (IOException ioe) {
// Something bad happened
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Ignored, Camera does all the work for us
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCamera.stopPreview();
mCamera.release();
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Invoked every time there's a new Camera preview frame
}
private Camera getCamera() {
Camera cam = null;
try {
cam = Camera.open();
} catch (RuntimeException e) {
loggerManager.error("Camera not available");
}
return cam;
}
}
CameraFragment.java
public class CameraFragment extends Fragment {
// You create an instance of the module. I use a singleton.
CameraModule mCameraModule = new CameraModule();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
TextureView mTextureView = (TextureView) view.findViewById(R.id.camera_preview);
mTextureView.setSurfaceTextureListener(mCameraModule);
return view;
}
}
fragment_camera.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical">
<TextureView
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:background="#000"
android:layout_height="match_parent" />
</RelativeLayout>
Good luck!