Is it possible to manually destroy SurfaceView?

2020-07-14 05:37发布

My SurfaceView is not getting destroyed even if onPause of the activity is called.

I am taking care of the thread in

public void surfaceCreated(SurfaceHolder holder) {
    if (mGameThread.getState() == Thread.State.TERMINATED) {
        createGameThread(getHolder(), getContext());
    }
    mGameThread.setRunning(true);
    mGameThread.start();
}


public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    mGameThread.setRunning(false);
    while (retry) {
        try {
            mGameThread.join();
            retry = false;
        } catch (InterruptedException e) {
        }
    }
}

As an hack I have to check the state of the thread in onResume and if the thread is already terminated, I would finish the activity

protected void onResume() {
    Log.d(mLogTag, "onResume()");
    super.onResume();
    if (mGameThread != null) {
        if (mGameThread.getState() == Thread.State.TERMINATED) {
            finish();
        }
    }

}

Unfortunately it is not possible to move the thread handling from surfaceDestroyed and surfaceCreated to onPause() and onResume() of the activity. Is it possible to manually destroy the SurfaceView in the onPause() and recreate it in onResume()?

3条回答
地球回转人心会变
2楼-- · 2020-07-14 06:04

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);
              }
查看更多
beautiful°
3楼-- · 2020-07-14 06:17

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();
查看更多
乱世女痞
4楼-- · 2020-07-14 06:17

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.

查看更多
登录 后发表回答