在camera.xml内的R.id.preview_content设置预览界面,这里我们看到camera2的预览界面是一个TextureView
TextureView相关可以参考http://www.wfuyu.com/technology/22762.html
CameraAppUI.java中
public void prepareModuleUI() {
mController.getSettingsManager().addListener(this);
mModuleUI = (FrameLayout) mCameraRootView.findViewById(R.id.module_layout);
mTextureView = (TextureView) mCameraRootView.findViewById(R.id.preview_content);
//将预览界面传入TextureViewHelper
mTextureViewHelper = new TextureViewHelper(mTextureView, mCaptureLayoutHelper,
mController.getCameraProvider());
mTextureViewHelper.setSurfaceTextureListener(this);
mTextureViewHelper.setOnLayoutChangeListener(mPreviewLayoutChangeListener);
......
}
TextureViewHelper.java中
//可以看到TextureViewHelper.java初始时设置mPreview的监听
public TextureViewHelper(TextureView preview, CaptureLayoutHelper helper,
CameraProvider cameraProvider) {
mPreview = preview;
mCameraProvider = cameraProvider;
mPreview.addOnLayoutChangeListener(this);
mPreview.setSurfaceTextureListener(this);
mCaptureLayoutHelper = helper;
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
if (mWidth != 0 && mHeight != 0) {
// Re-apply transform matrix for new surface texture
updateTransform();
}
if (mSurfaceTextureListener != null) {
mSurfaceTextureListener.onSurfaceTextureAvailable(surface, width, height);
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
if (mSurfaceTextureListener != null) {
mSurfaceTextureListener.onSurfaceTextureSizeChanged(surface, width, height);
}
}
//当mPreview的SurfaceTexture发生变化时会响应,这里的mSurfaceTextureListener正是在CameraAppUI.java中prepareModuleUI函数内set的,而CameraAppUI.java的mSurfaceTextureListener是在
CameraActivity初始化module时set,看PhotoModule.java的init函数如下
PhotoModule.java
@Override
public void init(CameraActivity activity, boolean isSecureCamera, boolean isCaptureIntent) {
Log.i(TAG, "init Camera");
mActivity = activity;
// TODO: Need to look at the controller interface to see if we can get
// rid of passing in the activity directly.
mAppController = mActivity;
// SPRD initialize mJpegQualityController.
mJpegQualityController = new JpegQualityController();
mUI = new PhotoUI(mActivity, this, mActivity.getModuleLayoutRoot());
//这里set PhotoUI的SurfaceTextureListener
mActivity.setPreviewStatusListener(mUI);
......
}
//这样我们可以看到当TextureView初始化getHardwareLayer()时第一个回调的是
PhotoUI.java的如下方法
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
//onPreviewUIReady是对应module中的方法
mController.onPreviewUIReady();
}
PhotoModule.java中
@Override
public void onPreviewUIReady() {
Log.i(TAG, "onPreviewUIReady");
startPreview();
}
protected void startPreview(){
......
//这里将TextureView初始化后的surfaceTexture设置到cameraDevice,这样camera预览图就能实时显示在TextureView上
mCameraDevice.setPreviewTexture(mActivity.getCameraAppUI().getSurfaceTexture());
CameraAgent.CameraStartPreviewCallback startPreviewCallback =
new CameraAgent.CameraStartPreviewCallback() {
@Override
public void onPreviewStarted() {
mFocusManager.onPreviewStarted();
PhotoModule.this.onPreviewStarted();
SessionStatsCollector.instance().previewActive(true);
if (mSnapshotOnIdle) {
mHandler.post(mDoSnapRunnable);
}
}
};
if (GservicesHelper.useCamera2ApiThroughPortabilityLayer(mActivity)) {
mCameraDevice.startPreview();
startPreviewCallback.onPreviewStarted();
} else {
mCameraDevice.startPreviewWithCallback(new Handler(Looper.getMainLooper()),startPreviewCallback);
}
}
至此整个预览界面TextureView初始化完成