即使调用活动的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()中重新创建它?
答案 0 :(得分: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();
答案 1 :(得分:3)
您可以添加布局作为surfaceview的父级,然后在onPause()中设置布局GONE的可见性,并在活动的onResume()中设置VISIBLE。
答案 2 :(得分:0)
是的可能。 首先初始化尺寸
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);
}