Forked from tuanchauict/RotateOrientationEventListener.java
Created
December 4, 2018 08:41
-
-
Save Savrov/3f55c37cd5ab4d8b4a1d2ee55f490722 to your computer and use it in GitHub Desktop.
Handle screen rotation without onConfigurationChanged
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.notabasement.common.photopicker.events; | |
import android.content.Context; | |
/** | |
* Created by tuanchauict on 3/16/16. | |
*/ | |
public abstract class RotateOrientationEventListener extends SimpleOrientationEventListener { | |
public RotateOrientationEventListener(Context context) { | |
super(context); | |
} | |
public RotateOrientationEventListener(Context context, int rate) { | |
super(context, rate); | |
} | |
@Override | |
public final void onChanged(int lastOrientation, int orientation) { | |
int startDeg = lastOrientation == 0 | |
? orientation == 3 ? 360 : 0 | |
: lastOrientation == 1 ? 90 | |
: lastOrientation == 2 ? 180 | |
: 270; // don't know how, but it works | |
int endDeg = orientation == 0 | |
? lastOrientation == 1 ? 0 : 360 | |
: orientation == 1 ? 90 | |
: orientation == 2 ? 180 | |
: 270; // don't know how, but it works | |
onRotateChanged(startDeg, endDeg); | |
} | |
public abstract void onRotateChanged(int startDeg, int endDeg); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.view.OrientationEventListener; | |
/** | |
* Created by tuanchauict on 3/16/16. | |
*/ | |
public abstract class SimpleOrientationEventListener extends OrientationEventListener { | |
public static final int ORIENTATION_PORTRAIT = 0; | |
public static final int ORIENTATION_LANDSCAPE = 1; | |
public static final int ORIENTATION_PORTRAIT_REVERSE = 2; | |
public static final int ORIENTATION_LANDSCAPE_REVERSE = 3; | |
private int lastOrientation = 0; | |
public SimpleOrientationEventListener(Context context) { | |
super(context); | |
} | |
public SimpleOrientationEventListener(Context context, int rate) { | |
super(context, rate); | |
} | |
@Override | |
public final void onOrientationChanged(int orientation) { | |
if (orientation < 0) { | |
return; // Flip screen, Not take account | |
} | |
int curOrientation; | |
if (orientation <= 45) { | |
curOrientation = ORIENTATION_PORTRAIT; | |
} else if (orientation <= 135) { | |
curOrientation = ORIENTATION_LANDSCAPE_REVERSE; | |
} else if (orientation <= 225) { | |
curOrientation = ORIENTATION_PORTRAIT_REVERSE; | |
} else if (orientation <= 315) { | |
curOrientation = ORIENTATION_LANDSCAPE; | |
} else { | |
curOrientation = ORIENTATION_PORTRAIT; | |
} | |
if (curOrientation != lastOrientation) { | |
onChanged(lastOrientation, curOrientation); | |
lastOrientation = curOrientation; | |
} | |
} | |
public abstract void onChanged(int lastOrientation, int orientation); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment