I want to implement a camera preview. To do this, I have a custom view CameraView extends ViewGroup, which in the constructor programmatically creates a surfaceView.
I have the following components (extremely simplified for beverity):
ScannerFragment.java
public View onCreateView(..) {
}
public void onResume() {
}
public void onPause() {
}
CameraView.java extends ViewGroup
public void setUpCalledInConstructor(Context context) {
}
public void surfaceCreated(SurfaceHolder holder) {
camera.setPreviewDisplay(holder);
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
camera.getParameters().setPreviewSize(previewSize.width, previewSize.height);
}
fragment_scanner.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.myapp.camera.CameraView
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
I think I set the life cycle correctly (getting the resources of onResume (), releasing it onPause () roughly), and the following works just fine:
- clicking on the house and returning
- pressing the Taskswitcher button and returning
- rotation
- cancellation (of course)
, , . : , . , , .
, , , , surfaceView , .. SurfaceHolder.Callback.surfaceDestroyed(SurfaceHolder holder) . , ( ) - , , , "surfaceDestroyed" .
, . surfaceDestroyed(), . , View, , . /, , , . .