最近在做一个VR项目,使用到Camera功能从项目的不同需求从最开始使用SurfaceView、到TextureView一直到GLSurfaceView。
SurfaceView 官网的解释是:
Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen
简单说就是“SurfaceView是视图(View)的继承类,这个视图里内嵌了一个专门用于绘制的Surface。你可以控制这个Surface的格式和尺寸。Surfaceview控制这个Surface的绘制位置。”
接下蓝看下camera预览如何使用这个view,废话少说,直接上代码:
public class CameraSurfacePreview extends SurfaceView implements SurfaceHolder.Callback {
public static final String TAG = "CameraSurfacePreview";
SurfaceHolder mSurfaceHolder;
Context mContext;
CameraWrapper mCameraWrapper; //Camera api的包装类
@SuppressWarnings("deprecation")
public CameraSurfacePreview(Context context, AttributeSet attrs) {
super(context, attrs);
this.mSurfaceHolder = getHolder();
this.mContext = getContext();
this.mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.mSurfaceHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i(TAG, "surfaceCreated...");
}
<