过境活动时如何保存摄像机视图?

时间:2011-11-29 07:41:15

标签: android camera surfaceview

在我的Android应用程序中,我使用相机表面视图作为活动的背景。我想要的是,一旦我移动到另一个活动,相机不应该关闭。我想要在我的每一个后面的相机视图layout.But我不能重新启动相机视图bcoz重新启动相机需要时间。我想运行相机表面视图作为activties.please帮助的背景运行。

    Camera camera;
        SurfaceView surfaceView;
        SurfaceHolder surfaceHolder;
        boolean previewing = false;
        LayoutInflater controlInflater = null;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        
             setContentView(R.layout.main);

             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
             getWindow().setFormat(PixelFormat.UNKNOWN);
             surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
             surfaceHolder = surfaceView.getHolder();
             surfaceHolder.addCallback(this);
             surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
             surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);        
            LinearLayout mainLayout=new LinearLayout(this);
            mainLayout.setOrientation(LinearLayout.VERTICAL);               
            LayoutInflater layoutInflater = getLayoutInflater();        
            mainLayout.addView(layoutInflater.inflate(R.layout.dialpad,null));
            mainLayout.addView(layoutInflater.inflate(R.layout.allbuttons,null));

            this.addContentView(mainLayout, params);
}//end of oncreate method
public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera = Camera.open();
    }


    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;


    }

此代码用于创建摄像机视图。请帮助我并尽快回复。谢谢。

1 个答案:

答案 0 :(得分:0)

经过大量的搜索,我找到了完成任务的方法。通过使用任何活动轻松启动相机然后启动另一个活动。确保你的活动开始是透明的。应用透明主题在android中清单文件。以下链接帮助我以这种方式完成。 How do I create a transparent Activity on Android?