本篇按说应该放在第三篇,当相机初始化成功后要直接开启预览才对,前面忘讲这部分,在这里补上。还是先来看一下整体流程:
在第二篇中讲过,相机的初始化过程是在一个子线程中执行的,也就是图中的内部类CameraStartUpThread,所以我们接着前面camera open之后,将参数应用到设备,即执行如下方法:
private void applyFirstParameters () {
Log.i(TAG, "applyFirstParameters");
mIsFirstOpenCamera = false;
mMainHandler.sendEmptyMessage(MSG_SET_PREVIEW_ASPECT_RATIO);
switchCameraPreview();
mCurCameraDevice.setJpegRotation(mOrientation);
mCameraAppUi.setZoomParameter();
mCurCameraDevice.setDisplayOrientation(true);
mCurCameraDevice.setPreviewFormat(ImageFormat.YV12);
// Camera do not open zsd mode launched by 3rd party.
if (!mCameraActivity.isImageCaptureIntent() && !mCameraActivity.isVideoCaptureIntent()) {
mCurCameraDevice.getParametersExt()
.setZSDMode(SettingUtils.getPreferenceValue(mCameraActivity,
mPreferences,SettingConstants.ROW_SETTING_ZSD, Util.OFF));
}
mCurCameraDevice.getParametersExt().set(ParametersHelper.KEY_FIRST_PREVIEW_FRAME,
Util.FIRST_PREVIEW_BLACK_ON);
mCurCameraDevice.applyParametersToServer();
// for launch performance
mMainHandler.sendEmptyMessageDelayed(MSG_REMOVE_PREVIEW_COVER, 150);
mCameraActor.onCameraParameterReady(true);
mCurCameraDevice.setOneShotPreviewCallback(mOneShotPreviewCallback);
mMainHandler.sendEmptyMessage(MSG_CAMERA_PARAMETERS_READY);
mMainHandler.sendEmptyMessage(MSG_CAMERA_PREVIEW_DONE);
}
private void switchCameraPreview() {
CameraPerformanceTracker.onEvent(TAG,
CameraPerformanceTracker.NAME_SET_PREVIEW_DISP,
CameraPerformanceTracker.ISBEGIN);
mCameraActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
setSurfaceViewVisible(View.VISIBLE);
}
});
mCurCameraDevice.setPreviewDisplayAsync(mSurfaceView.getHolder());
CameraPerformanceTracker.onEvent(TAG,
CameraPerformanceTracker.NAME_SET_PREVIEW_DISP,
CameraPerformanceTracker.ISEND);
}
我们都知道,相机预览所要使用的渲染视图属于需要频繁刷新的UI,因此需要使用SurfaceView,而在这个switchCameraPreview()方法里,确实将一个mSurfaceView对象传递给了我们自定义的AndroidCamera的代理对象。我们看一下这个SurfaceView有没有什么特别的地方:
public class PreviewSurfaceView extends SurfaceView {...}
它是一个自定义的SurfaceView,在CameraActivity初始化执行onCreate方法时attach到界面上:
public void attachSurfaceViewLayout() {
Log.i(TAG, "[attachSurfaceViewLayout] begin mCurSurfaceViewLayout = " + mCurSurfaceViewLayout);
if (mSurfaceView == null) {
FrameLayout surfaceViewRoot = (FrameLayout) mCameraActivity.findViewById(R.id.camera_surfaceview_root);
mLastSurfaceViewLayout = mCurSurfaceViewLayout;
mCurSurfaceViewLayout = (FrameLayout) mCameraActivity.getLayoutInflater().inflate(R.layout.camera_preview_layout, null);
mSurfaceView = (PreviewSurfaceView) mCurSurfaceViewLayout.findViewById(R.id.camera_preview);
mSurfaceView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mCameraActivity.getGestureRecognizer().onTouchEvent(event);
return true;
}
});
SurfaceHolder surfaceHolder = mSurfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceViewRoot.addView(mCurSurfaceViewLayout);
if (mIsFirstStartUp) {
mSurfaceView.setVisibility(View.VISIBLE);
} else if (mModuleManager != null) {
mSurfaceView.setVisibility(mModuleManager.isDisplayUseSurfaceView() ? View.VISIBLE
: View.INVISIBLE);
}
}
Log.i(TAG, "[attachSurfaceViewLayout] end ");
}
这个自定义的SurfaceView外层还是一个FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/camera_preview_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible"
>
<com.android.camera.ui.PreviewSurfaceView
android:id="@+id/camera_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"/>
</FrameLayout>
它本身主要重写了onMeasure方法,用来动态改变在相机选择不同比例的分辨率时所使用的大小,此外并没有什么特别的地方。
从时序图上来看,以CameraDeviceCtrl为中线,左边主要是视图UI的刷新,右边主要是对Camera设备的操作,而初始化的源头是在子线程当中,其中的线程切换(即跨线程通信)主要是通过Handler+Messager来实现的。右边设置好了SurfaceView与Parameter后,左边的UI部分就可以开启预览startPreview了。另外,PhotoMode本身并不持有任何CameraDevice的对象,但它的父类CameraMode持有一个ICameraDevice的对象mICameraDevice。而ICameraDevice的实现位于CameraDeviceImpl中,该类内部持有AndroidCamera的代理对象,这样左边开启预览后就可以操作到之前设置SurfaceView所用的CameraProxy对象了。