get Current Rotation In Degrees - Android Hardware

Android examples for Hardware:Accelerometer

Description

get Current Rotation In Degrees

Demo Code


//package com.java2s;
import android.content.Context;

import android.view.Surface;
import android.view.WindowManager;

public class Main {
    public static int getCurrentRotationInDegrees(Context context) {
        final WindowManager windowManager = getWindowManager(context);
        final int currentRotation = windowManager.getDefaultDisplay()
                .getRotation();//from  w  w w .  ja  v  a 2  s . c  o m

        switch (currentRotation) {
        case Surface.ROTATION_0:
            return 0;
        case Surface.ROTATION_90:
            return 90;
        case Surface.ROTATION_180:
            return 180;
        case Surface.ROTATION_270:
            return 270;
        default:
            throw new IllegalStateException(
                    "the device current rotation is neither 0, 90, 180 nor 270");
        }
    }

    private static WindowManager getWindowManager(Context context) {
        return (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
    }
}

Related Tutorials