Is it possible to manually destroy SurfaceView?

帅比萌擦擦* 提交于 2019-12-07 09:18:03

问题


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()?


回答1:


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();



回答2:


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.




回答3:


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);
              }


来源:https://stackoverflow.com/questions/15134604/is-it-possible-to-manually-destroy-surfaceview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!