Android Screen Orientation Check getDirectionNow(final float direction)

Here you can find the source of getDirectionNow(final float direction)

Description

Take the phone rotation (through a given activity) in account and adjust the direction.

License

Apache License

Parameter

Parameter Description
direction the unadjusted direction in degrees, in the [0, 360[ range

Return

the adjusted direction in degrees, in the [0, 360[ range

Declaration

public static float getDirectionNow(final float direction) 

Method Source Code

//License from project: Apache License 

import cgeo.geocaching.CgeoApplication;
import android.content.Context;
import android.view.Surface;
import android.view.WindowManager;

public class Main{
    /**/*from   w  w w  . j  a v a  2 s .  com*/
     * Take the phone rotation (through a given activity) in account and adjust the direction.
     *
     * @param direction the unadjusted direction in degrees, in the [0, 360[ range
     * @return the adjusted direction in degrees, in the [0, 360[ range
     */
    public static float getDirectionNow(final float direction) {
        return normalize(direction + getRotationOffset());
    }
    /**
     * Normalize an angle so that it belongs to the [0, 360[ range.
     * @param angle the angle in degrees
     * @return the same angle in the [0, 360[ range
     */
    public static float normalize(final float angle) {
        return (angle >= 0 ? angle : (360 - ((-angle) % 360))) % 360;
    }
    public static int getRotationOffset() {
        switch (WindowManagerHolder.WINDOW_MANAGER.getDefaultDisplay()
                .getRotation()) {
        case Surface.ROTATION_90:
            return 90;
        case Surface.ROTATION_180:
            return 180;
        case Surface.ROTATION_270:
            return 270;
        default:
            return 0;
        }
    }
}

Related

  1. getRotationOffset()
  2. isDefaultToPortrait(Activity activity)
  3. isLandScape(Context context)
  4. isLandscape(final Context context)