Android相机SurfaceView方向
问题描述:
我正在构建使用Android相机的应用程序。我使用了一个有2个孩子的FrameLayout。第一个孩子是预览相机的SurfaceView,第二个孩子是带有一些按钮的LinearLayout。Android相机SurfaceView方向
我已阅读,在Android 2.1及以下,用相机的唯一途径是在风景模式,所以我在清单文件中使用 android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"
在我的活动。
有没有办法在横向模式下使用SurfaceView(相机),并使用LinearLayout(按钮)纵向/横向?
(例如,在谷歌护目镜相机不会改变方向,但按钮改变方向时旋转。)
答
解决问题的最佳方式就像AOSP通过使用RotateImageView和特殊的ImageView一样可以设置角度旋转。 因此,在您的相机活动中,您必须实施OrientationListener和onOrientationChanged,将新Degrees设置为RotateImageView。以下是简单的方法,但您可以在相机实施处寻找更好的示例:
private class MyOrientationEventListener extends OrientationEventListener {
public MyOrientationEventListener(Context context) {
super(context);
}
@Override
public void onOrientationChanged(int orientation) {
mOrientation = roundOrientation(orientation);
((RotateImageView)findViewById(R.id.btnFlash)).setDegree(mOrientation);
}
}
public static int roundOrientation(int orientation) {
return ((orientation + 45)/90 * 90) % 360;
}