Code example for SurfaceView

Methods: getHoldersetVisibility, setLayoutParams

0
 
        // start preview 
//        if (useTexture()) { 
//            mTextureView.setVisibility(View.VISIBLE); 
//        } else { 
            surfaceView.setVisibility(View.VISIBLE);
//        } 
    } 
 
    public void onResume() { 
        Log.v(TAG, "onResume.");
        paused = false;
 
        // Open the camera 
        if (camera == null) {
            openCamera(); 
            if (camera == null) {
                showCameraErrorAndFinish(); 
                return; 
            } 
        } 
 
        // Initialize the surface texture or surface view 
//        if (useTexture() && mTextureView == null) { 
//            initTextureView(); 
//            mTextureView.setVisibility(View.VISIBLE); 
//        } else if (!useTexture() && mSurfaceView == null) { 
            initSurfaceView(); 
            surfaceView.setVisibility(View.VISIBLE);
//        } 
 
        // Start the preview 
        if (!mPreviewing) {
            startPreview(); 
        } 
    } 
 
    private void openCamera() { 
        Log.v(TAG, "openCamera");
        try { 
            camera = Camera.open();
            camera.setErrorCallback(this);
            camera.setDisplayOrientation(90); // Since we only support portrait mode
            cameraParameters = camera.getParameters();
        } catch (RuntimeException e) {
            e.printStackTrace();
            camera = null;
        } 
    } 
 
    private void closeCamera() { 
        Log.v(TAG, "closeCamera");
        if (camera == null) {
            Log.d(TAG, "Already stopped.");
            return; 
        } 
 
        camera.setErrorCallback(null);
        if (mPreviewing) {
            stopPreview(); 
        } 
        camera.release();
        camera = null;
    } 
 
//    private void initTextureView() { 
//        mTextureView = new TextureView(currentActivity); 
//        mTextureView.setSurfaceTextureListener(new SurfaceTextureCallback()); 
//        mTextureView.setVisibility(View.GONE); 
//        FrameLayout.LayoutParams params = new LayoutParams( 
//                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER); 
//        mTextureView.setLayoutParams(params); 
//        Log.d(TAG, "add texture view to preview frame"); 
//        mPreviewFrame.addView(mTextureView); 
//    } 
// 
//    private void releaseSurfaceTexture() { 
//        if (mSurfaceTexture != null) { 
//            mPreviewFrame.removeAllViews(); 
//            mTextureView = null; 
//            mSurfaceTexture = null; 
//            mSurfaceTextureReady = false; 
//        } 
//    } 
// 
    private void initSurfaceView() { 
        surfaceView = new SurfaceView(currentActivity);
        surfaceView.getHolder().addCallback(new SurfaceViewCallback());
        surfaceView.setVisibility(View.GONE);
        FrameLayout.LayoutParams params = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER);
        surfaceView.setLayoutParams(params);
        Log.d(TAG, "add surface view to preview frame");
        previewFrame.addView(surfaceView);
    } 
 
    private void releaseSurfaceView() {