简体   繁体   English

Android相机SurfaceView方向

[英]Android Camera SurfaceView Orientation

I am building an app that uses the android camera. 我正在构建一个使用android相机的应用程序。 I use a FrameLayout that has 2 children. 我使用具有2个子项的FrameLayout。 The first child is a SurfaceView that previews the Camera and the second child is a LinearLayout with some buttons. 第一个子项是预览相机的SurfaceView,第二个子项是带有某些按钮的LinearLayout。

I have read that in Android 2.1 and below, the only way to use the camera is on Landscape mode, so I use android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation" in my activity in the manifest file. 我已经读过,在Android 2.1及更低版本中,使用相机的唯一方法是在横向模式下,因此我在清单文件中的活动中使用了android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"

Is there a way to use the SurfaceView (camera) in landscape mode only, and use the LinearLayout (buttons) either portrait/landscape? 有没有一种方法可以仅在横向模式下使用SurfaceView(相机),并在纵向/横向模式下使用LinearLayout(按钮)?

(For example in Google Goggles the camera doesn't change orientation but the buttons rotate when changing orientation.) (例如,在Google Goggles中,相机不会更改方向,但在更改方向时按钮会旋转。)

The Camera app included in Android is in AOSP. Android中包含的Camera应用程序位于AOSP中。 Check the source out for yourself. 自己检查源。 From the looks of it, it's fixed in landscape mode as well, but simply rotates the ImageViews when the user changes the orientation. 从外观上看,它也固定在横向模式下,但是当用户更改方向时只需旋转ImageViews。

See this part of Camera's source. 请参阅“相机”源代码的这一部分

The best way to solve is like AOSP does by using RotateImageView and special ImageView that you can set a rotation in degrees. 最好的解决方法就像AOSP一样,可以使用RotateImageView和特殊的ImageView来设置旋转度。 So in your Camera Activity you have to implement OrientationListener and onOrientationChanged you set the new Degrees to your RotateImageView. 因此,在Camera Activity中,您必须实现OrientationListener和onOrientationChanged,并将新的Degrees设置为RotateImageView。 Here is the simple way but you can look for a better example at Camera implementation at 这是简单的方法,但是您可以在以下位置的Camera实施中找到更好的示例

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM