SurfaceView cameraView = (CameraView) this.findViewById(R.id.CameraView);
SurfaceHolder surfaceHolder = cameraView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
另外,我们可能会希望在活动中实现SurfaceHolder.Callback,从而使得在创建、修改及销毁该Surface时活动将会获得通知。为了实现回调,需要添加以下方法。
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {}
public void surfaceCreated(SurfaceHolder holder) {}
public void surfaceDestroyed(SurfaceHolder holder) {}
最后,需要告诉SurfaceHolder使用该活动作为回调处理程序。
surfaceHolder.addCallback(this);
现在活动应该看起来如下所示。
package com.apress.proandroidmedia.ch2.snapshot;
import android.app.Activity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SnapShot extends Activity implements SurfaceHolder.Callback {
SurfaceView cameraView;
SurfaceHolder surfaceHolder;
@Override
public void onCreate(Bundle savedInstanceState)
{