round Orientation - Android android.view

Android examples for android.view:OrientationEventListener

Description

round Orientation

Demo Code

import android.view.OrientationEventListener;

public class Main {

  public static final int ORIENTATION_HYSTERESIS = 5;

  public static int roundOrientation(int orientation, int orientationHistory) {
    boolean changeOrientation = false;
    if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) {
      changeOrientation = true;/*w ww.  j  av  a  2s.  c  o  m*/
    } else {
      int dist = Math.abs(orientation - orientationHistory);
      dist = Math.min(dist, 360 - dist);
      changeOrientation = (dist >= 45 + ORIENTATION_HYSTERESIS);
    }
    if (changeOrientation) {
      return ((orientation + 45) / 90 * 90) % 360;
    }
    return orientationHistory;
  }

}

Related Tutorials