Search code examples
javac#androidcamerasurface

How to replicate Androids stock camera orientation change


I have an Samsung Edge, API 24, Android 7.0 and a Samsung Tablet, API 24, Android 7.0.

Skip this blockquote part if you don't want to know about the backstory:

I want to recreate the stock camera and implement it into my app. I'm actually very close to having it finished (using Xamarin.Android + C#), using the Xamarin Version of the Google Camera Sample.

However there are orientation problems left... 180° rotations (landscape -> reverse landspace or vice versa) don't trigger the "SurfaceChanged" event, which is used to set the matrix of the surface to make it display correctly.

I've seen different approaches, one seems stable: Fix the orientation to landscape and handle orientations myself.

Looking at the stock camera of my two devices, I see that the camera preview never moves a bit, only the button rotate + the activity (status bar moves to top on orientation change) How is it possible to achieve this behaviour?

An abstract idea, or code snippets in Java or C# would be nice.


Solution

  • Fix the activity orientation on start (for example, inside onResume method):

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    

    Specify in AndroidManifest.xml that you want the activity to listen to orientation changes:

    <activity
        android:configChanges="keyboardHidden|orientation|screenSize"
        ...
    

    Then process the orientation changes inside activity:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // rotate buttons using newConfig.orientation value
    }