get Device Orientation Degrees - Android Hardware

Android examples for Hardware:Device Feature

Description

get Device Orientation Degrees

Demo Code


//package com.java2s;

import android.content.Context;

import android.view.Surface;

import android.view.WindowManager;

public class Main {
    public static int getDeviceOrientationDegrees(Context context) {
        int degrees = 0;

        try {//  w ww  .ja  va2s  . c  om
            WindowManager windowManager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            int rotation = windowManager.getDefaultDisplay().getRotation();

            switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
            }
        } catch (Exception e) {
            //Log.e(LOG_TAG, "Error in getDeviceOrientationDegrees: " + e.getMessage());
        }

        return degrees;
    }
}

Related Tutorials