Is it possible to manually destroy SurfaceView?

纵然是瞬间 提交于 2019-12-05 13:03:23
kailash barochiya

You can add surface view dynamically on your view.

Example :layout.xml

    <FrameLayout
        android:id="@+id/fragment_file_videoplayer_surface_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>

MainActivity.java

FrameLayout fl_surfaceview_container = 
              (FrameLayout)findViewById(R.id.fragment_file_videoplayer_surface_container);

// Add surfaceView on Framelayout

SurfaceView videoSurface = new SurfaceView(getActivity());
fl_surfaceview_container.addView(videoSurface);

//if remove or destroy surfaceview

fl_surfaceview_container.removeAllViews();

You can add a layout as a parent of the surfaceview, then set visibility of the layout GONE in onPause(), and set VISIBLE in onResume() of the activity.

Yes possible. First initialize Size

   Size currentSurfaceSize;

   cameraSurface.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(QR_Reader_Activity.this,
                                new String[]{Manifest.permission.CAMERA}, RequestCameraPermission);
                        permission = true;
                        return;
                    }
                    try {
                        cameraSource.start(cameraSurface.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        currentSurfaceSize = new Size(width, height);
                    }
                }
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    onPause();
                }
            });

where do you want to destroy the surface, use this below code.

              if (currentSurfaceSize==null){
                    cameraSurface = (SurfaceView) cameraSurface.getHolder();
                    cameraSurface.removeCallbacks((Runnable) cameraSurface);
              }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!