2014-02-21 61 views
0

相机surfaceview我想知道如何改变活动的意图,同时保持相机预览我的应用程序的背景。我使用addView()来覆盖我的xml布局(充气)在MainActivity上的摄像机SurfaceView的顶部,并重写onBackPressed()并使用removeViewAt()和addView()移回我的视图。如何开始新的活动意图覆盖在mainactivity

我觉得我当前的代码是非常的混乱,因为我只使用一个类(MainActivity类别)通过我的意见进行切换。

我想使用意图,因为我觉得我的代码会更有条理,所以我不会有重写onBackPressed(),因为每一项活动将是“回堆叠”的问题。

+0

我知道这不是一个答案,但可以帮助你一些指导,我已经开始使用自定义视图使用手机摄像头一个项目,尝试看看类PlayerSelectionActivity和CameraActivity https://开头github.com/lpbaptista/Party-Monsters,如果你需要更多的帮助,只需发送消息 – GhostDerfel

回答

0

没关系。我一直在阅读和发现,最好的办法做,这是通过使用片段。这解决了问题。

0

为了让您的相机在整个应用程序运行时,尝试有CameraPreview.java类,并简单地称这种现象你想有摄像头运行的每一个活动。

private Camera mCamera = null; 
private CameraPreview mPreview; 
SurfaceView surfaceView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    surfaceView = (SurfaceView)findViewById(R.id.cameralayout); 
    mPreview = new CameraPreview(this, surfaceView); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    mCamera = CameraPreview.getCameraInstance(); 
    mPreview.setCamera(mCamera);  
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (mCamera != null) { 
     mPreview.setCamera(null); 
     mCamera.release(); 
     mCamera = null; 
    } 
} 

在另一方面,你可以尝试设置标志,而开关活动清除栈

Intent i = new Intent(ActivityA.this, ActivityB.class); 
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(i); 
+0

虐待它试一试。以后发布结果 –

+0

getCameraInstance()和setCamera()里面的代码是什么? –

+0

此代码是否会在每个活动中重新启动相机?因为我宁愿相机不会在更改活动 –