开发者

Android Camera SurfaceView Orientation

开发者 https://www.devze.com 2023-02-19 08:22 出处:网络
I am building an app that uses the android camera. I use a FrameLayout that has 2 children. The first child is a SurfaceView that previews the Camera and the second child is a LinearLayout with some b

I am building an app that uses the android camera. I use a FrameLayout that has 2 children. The first child is a SurfaceView that previews the Camera and the second child is a LinearLayout with some buttons.

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.

Is there a way to use the SurfaceView (camera) in landscape mode only, and use the LinearLayout (buttons开发者_Python百科) either portrait/landscape?

(For example in Google Goggles the camera doesn't change orientation but the buttons rotate when changing orientation.)


The Camera app included in Android is in 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.

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. So in your Camera Activity you have to implement OrientationListener and onOrientationChanged you set the new Degrees to your RotateImageView. Here is the simple way but you can look for a better example at Camera implementation at

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

精彩评论

暂无评论...
验证码 换一张
取 消