繁体   English   中英

Android相机SurfaceView方向

[英]Android Camera SurfaceView Orientation

我正在构建一个使用android相机的应用程序。 我使用具有2个子项的FrameLayout。 第一个子项是预览相机的SurfaceView,第二个子项是带有某些按钮的LinearLayout。

我已经读过,在Android 2.1及更低版本中,使用相机的唯一方法是在横向模式下,因此我在清单文件中的活动中使用了android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"

有没有一种方法可以仅在横向模式下使用SurfaceView(相机),并在纵向/横向模式下使用LinearLayout(按钮)?

(例如,在Google Goggles中,相机不会更改方向,但在更改方向时按钮会旋转。)

Android中包含的Camera应用程序位于AOSP中。 自己检查源。 从外观上看,它也固定在横向模式下,但是当用户更改方向时只需旋转ImageViews。

请参阅“相机”源代码的这一部分

最好的解决方法就像AOSP一样,可以使用RotateImageView和特殊的ImageView来设置旋转度。 因此,在Camera Activity中,您必须实现OrientationListener和onOrientationChanged,并将新的Degrees设置为RotateImageView。 这是简单的方法,但是您可以在以下位置的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