Android Screen Orientation Check reverseDirectionNow(final float direction)

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

Description

Reverse 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 reverseDirectionNow(final float direction) 

Method Source Code

//License from project: Apache License 

import android.content.Context;
import android.view.Surface;
import android.view.WindowManager;

public class Main{
    /**// w w  w  .ja v a 2s  .c  o  m
     * Reverse 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 reverseDirectionNow(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. getDirectionNow(final float direction)
  2. getRotationOffset()
  3. isDefaultToPortrait(Activity activity)
  4. isLandScape(Context context)
  5. isLandscape(final Context context)
  6. roundOrientation(int orientation, int orientationHistory)
  7. getDisplayOrientation(Context context)