5

即使调用了活动的 onPause,我的 SurfaceView 也不会被破坏。

我正在处理线程

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

作为一个黑客,我必须检查 onResume 中线程的状态,如果线程已经终止,我将完成活动

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

}

不幸的是,不可能将线程处理从surfaceDestroyed 和surfaceCreated 移动到活动的onPause() 和onResume()。是否可以在 onPause() 中手动销毁 SurfaceView 并在 onResume() 中重新创建它?

4

3 回答 3

4

您可以在视图上动态添加表面视图。

示例: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();
于 2016-12-31T11:13:29.370 回答
3

您可以添加一个布局作为表面视图的父级,然后在 onPause() 中设置布局 GONE 的可见性,并在活动的 onResume() 中设置 VISIBLE。

于 2015-03-03T13:01:17.483 回答
0

是的可能。首先初始化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();
                }
            });

你想在哪里破坏表面,使用下面的代码。

              if (currentSurfaceSize==null){
                    cameraSurface = (SurfaceView) cameraSurface.getHolder();
                    cameraSurface.removeCallbacks((Runnable) cameraSurface);
              }
于 2017-11-15T08:27:46.113 回答