android.view.OrientationEventListener Java Examples
The following examples show how to use
android.view.OrientationEventListener.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RCTCameraView.java From react-native-camera-face-detector with MIT License | 6 votes |
public RCTCameraView(Context context) { super(context); this._context = context; RCTCamera.createInstance(getDeviceOrientation(context)); _orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { if (setActualDeviceOrientation(_context)) { layoutViewFinder(); } } }; if (_orientationListener.canDetectOrientation()) { _orientationListener.enable(); } else { _orientationListener.disable(); } }
Example #2
Source File: DisplayOrientationDetector.java From LockDemo with Apache License 2.0 | 6 votes |
public DisplayOrientationDetector(Context context) { mOrientationEventListener = new OrientationEventListener(context) { /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */ private int mLastKnownRotation = -1; @Override public void onOrientationChanged(int orientation) { if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN || mDisplay == null) { return; } final int rotation = mDisplay.getRotation(); if (mLastKnownRotation != rotation) { mLastKnownRotation = rotation; dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation)); } } }; }
Example #3
Source File: VideoCaptureAndroid.java From droidkit-webrtc with BSD 3-Clause "New" or "Revised" License | 6 votes |
public VideoCaptureAndroid(int id, long native_capturer) { this.id = id; this.native_capturer = native_capturer; this.info = new Camera.CameraInfo(); Camera.getCameraInfo(id, info); // Must be the last thing in the ctor since we pass a reference to |this|! final VideoCaptureAndroid self = this; orientationListener = new OrientationEventListener(GetContext()) { @Override public void onOrientationChanged(int degrees) { if (degrees == OrientationEventListener.ORIENTATION_UNKNOWN) { return; } if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { degrees = (info.orientation - degrees + 360) % 360; } else { // back-facing degrees = (info.orientation + degrees) % 360; } self.OnOrientationChanged(self.native_capturer, degrees); } }; // Don't add any code here; see the comment above |self| above! }
Example #4
Source File: ImageHelper.java From fanfouapp-opensource with Apache License 2.0 | 6 votes |
public static int roundOrientation(final int orientationInput) { // landscape mode int orientation = orientationInput; if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) { orientation = 0; } orientation = orientation % 360; int retVal; if (orientation < ((0 * 90) + 45)) { retVal = 0; } else if (orientation < ((1 * 90) + 45)) { retVal = 90; } else if (orientation < ((2 * 90) + 45)) { retVal = 180; } else if (orientation < ((3 * 90) + 45)) { retVal = 270; } else { retVal = 0; } return retVal; }
Example #5
Source File: DisplayOrientationDetector.java From TikTok with Apache License 2.0 | 6 votes |
public DisplayOrientationDetector(Context context) { mOrientationEventListener = new OrientationEventListener(context) { /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */ private int mLastKnownRotation = -1; @Override public void onOrientationChanged(int orientation) { if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN || mDisplay == null) { return; } final int rotation = mDisplay.getRotation(); if (mLastKnownRotation != rotation) { mLastKnownRotation = rotation; dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation)); } } }; }
Example #6
Source File: DisplayOrientationDetector.java From SimpleVideoEditor with Apache License 2.0 | 6 votes |
public DisplayOrientationDetector(Context context) { mOrientationEventListener = new OrientationEventListener(context) { /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */ private int mLastKnownRotation = -1; @Override public void onOrientationChanged(int orientation) { if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN || mDisplay == null) { return; } final int rotation = mDisplay.getRotation(); if (mLastKnownRotation != rotation) { mLastKnownRotation = rotation; dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation)); } } }; }
Example #7
Source File: Camera2RawFragment.java From android-Camera2Raw with Apache License 2.0 | 6 votes |
@Override public void onViewCreated(final View view, Bundle savedInstanceState) { view.findViewById(R.id.picture).setOnClickListener(this); view.findViewById(R.id.info).setOnClickListener(this); mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture); // Setup a new OrientationEventListener. This is used to handle rotation events like a // 180 degree rotation that do not normally trigger a call to onCreate to do view re-layout // or otherwise cause the preview TextureView's size to change. mOrientationListener = new OrientationEventListener(getActivity(), SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { if (mTextureView != null && mTextureView.isAvailable()) { configureTransform(mTextureView.getWidth(), mTextureView.getHeight()); } } }; }
Example #8
Source File: CameraFragment.java From io2015-codelabs with Apache License 2.0 | 6 votes |
/** * Translates from the device orientation given by the Android sensor APIs to the * orientation for a JPEG image. */ private int getJpegOrientation(CameraCharacteristics c, int deviceOrientation) { Log.d(TAG, "getJpegOrientation: "); if (deviceOrientation == android.view.OrientationEventListener.ORIENTATION_UNKNOWN) return 0; int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION); // Round device orientation to a multiple of 90 deviceOrientation = (deviceOrientation + 45) / 90 * 90; // Reverse device orientation for front-facing cameras boolean facingFront = c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT; if (facingFront) deviceOrientation = -deviceOrientation; // Calculate desired JPEG orientation relative to camera orientation to make // the image upright relative to the device orientation int jpegOrientation = (sensorOrientation + deviceOrientation + 360) % 360; return jpegOrientation; }
Example #9
Source File: SensorsDataScreenOrientationDetector.java From sa-sdk-android with Apache License 2.0 | 6 votes |
@Override public void onOrientationChanged(int orientation) { if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) { return; } //只检测是否有四个角度的改变 if (orientation < 45 || orientation > 315) { //0度 mCurrentOrientation = 0; } else if (orientation > 45 && orientation < 135) { //90度 mCurrentOrientation = 90; } else if (orientation > 135 && orientation < 225) { //180度 mCurrentOrientation = 180; } else if (orientation > 225 && orientation < 315) { //270度 mCurrentOrientation = 270; } }
Example #10
Source File: AdvCamera.java From adv_camera with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void identifyOrientationEvents() { OrientationEventListener myOrientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int iAngle) { final int[] iLookup = {0, 0, 0, 90, 90, 90, 90, 90, 90, 180, 180, 180, 180, 180, 180, 270, 270, 270, 270, 270, 270, 0, 0, 0}; // 15-degree increments if (iAngle != ORIENTATION_UNKNOWN) { int iNewOrientation = iLookup[iAngle / 15]; if (iOrientation != iNewOrientation) { iOrientation = iNewOrientation; } mPhotoAngle = normalize(iAngle); } } }; if (myOrientationEventListener.canDetectOrientation()) { myOrientationEventListener.enable(); } }
Example #11
Source File: RotateByOrientationActivity.java From ShareBox with Apache License 2.0 | 6 votes |
private void initOrientationListener() { mOrientationListener = new OrientationEventListener(this) { public void onOrientationChanged(int rotation) { // 设置竖屏 if (rotation >= 0 && rotation <= 45 || rotation >= 315 || rotation >= 135 && rotation <= 225) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else if (rotation > 45 && rotation < 135 || rotation > 225 && rotation < 315) { // 设置横屏 if (rotation > 225 && rotation < 315) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } } } }; }
Example #12
Source File: CompassView.java From android with Apache License 2.0 | 6 votes |
public CompassView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initSensors(); int colorAccent = ContextCompat.getColor(getContext(), R.color.colorAccent); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompassView); mPrimaryColor = a.getColor(R.styleable.CompassView_primaryColor, colorAccent); mSecondaryColor = a.getColor(R.styleable.CompassView_secondaryColor, Color.WHITE); a.recycle(); mOrientationEventListener = new OrientationEventListener(getContext()) { @Override public void onOrientationChanged(int i) { configureOrientation(); } }; }
Example #13
Source File: DisplayOrientationDetector.java From MediaPickerInstagram with Apache License 2.0 | 6 votes |
public DisplayOrientationDetector(Context context) { mOrientationEventListener = new OrientationEventListener(context) { /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */ private int mLastKnownRotation = -1; @Override public void onOrientationChanged(int orientation) { if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN || mDisplay == null) { return; } final int rotation = mDisplay.getRotation(); if (mLastKnownRotation != rotation) { mLastKnownRotation = rotation; dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation)); } } }; }
Example #14
Source File: DisplayOrientationDetector.java From cameraview with Apache License 2.0 | 6 votes |
public DisplayOrientationDetector(Context context) { mOrientationEventListener = new OrientationEventListener(context) { /** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */ private int mLastKnownRotation = -1; @Override public void onOrientationChanged(int orientation) { if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN || mDisplay == null) { return; } final int rotation = mDisplay.getRotation(); if (mLastKnownRotation != rotation) { mLastKnownRotation = rotation; dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation)); } } }; }
Example #15
Source File: CameraFragment.java From io2015-codelabs with Apache License 2.0 | 6 votes |
/** * Translates from the device orientation given by the Android sensor APIs to the * orientation for a JPEG image. */ private int getJpegOrientation(CameraCharacteristics c, int deviceOrientation) { Log.d(TAG, "getJpegOrientation: "); if (deviceOrientation == android.view.OrientationEventListener.ORIENTATION_UNKNOWN) return 0; int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION); // Round device orientation to a multiple of 90 deviceOrientation = (deviceOrientation + 45) / 90 * 90; // Reverse device orientation for front-facing cameras boolean facingFront = c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT; if (facingFront) deviceOrientation = -deviceOrientation; // Calculate desired JPEG orientation relative to camera orientation to make // the image upright relative to the device orientation int jpegOrientation = (sensorOrientation + deviceOrientation + 360) % 360; return jpegOrientation; }
Example #16
Source File: CameraSession.java From Telegram with GNU General Public License v2.0 | 5 votes |
public CameraSession(CameraInfo info, Size preview, Size picture, int format) { previewSize = preview; pictureSize = picture; pictureFormat = format; cameraInfo = info; SharedPreferences sharedPreferences = ApplicationLoader.applicationContext.getSharedPreferences("camera", Activity.MODE_PRIVATE); currentFlashMode = sharedPreferences.getString(cameraInfo.frontCamera != 0 ? "flashMode_front" : "flashMode", Camera.Parameters.FLASH_MODE_OFF); orientationEventListener = new OrientationEventListener(ApplicationLoader.applicationContext) { @Override public void onOrientationChanged(int orientation) { if (orientationEventListener == null || !initied || orientation == ORIENTATION_UNKNOWN) { return; } jpegOrientation = roundOrientation(orientation, jpegOrientation); WindowManager mgr = (WindowManager) ApplicationLoader.applicationContext.getSystemService(Context.WINDOW_SERVICE); int rotation = mgr.getDefaultDisplay().getRotation(); if (lastOrientation != jpegOrientation || rotation != lastDisplayOrientation) { if (!isVideo) { configurePhotoCamera(); } lastDisplayOrientation = rotation; lastOrientation = jpegOrientation; } } }; if (orientationEventListener.canDetectOrientation()) { orientationEventListener.enable(); } else { orientationEventListener.disable(); orientationEventListener = null; } }
Example #17
Source File: MainActivity.java From programming with GNU General Public License v3.0 | 5 votes |
private void addOrientationListener() { listener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) { public void onOrientationChanged(int orientation) { if ((orientation >= 230 && orientation <= 290) || (orientation >= 70 && orientation <= 90)) { portrait.setText("True"); } } }; if (listener.canDetectOrientation()) listener.enable(); }
Example #18
Source File: ReactOrientationListenerModule.java From react-native-orientation-listener with MIT License | 5 votes |
public ReactOrientationListenerModule(ReactApplicationContext reactContext) { super(reactContext); this.reactContext = reactContext; final ReactApplicationContext thisContext = reactContext; mOrientationListener = new OrientationEventListener(reactContext, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { WritableNativeMap params = new WritableNativeMap(); String orientationValue = ""; if(orientation == 0) { orientationValue = "PORTRAIT"; } else { orientationValue = "LANDSCAPE"; } params.putString("orientation", orientationValue); params.putString("device", getDeviceName()); if (thisContext.hasActiveCatalystInstance()) { thisContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit("orientationDidChange", params); } } }; if (mOrientationListener.canDetectOrientation() == true) { mOrientationListener.enable(); } else { mOrientationListener.disable(); } }
Example #19
Source File: CameraPreview.java From CuXtomCam with Apache License 2.0 | 5 votes |
/** * Possible Picture Orientation fix * * @param param * @param orientation */ public void onOrientationChanged(Parameters param, int orientation) { if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) return; CameraInfo info = new CameraInfo(); Camera.getCameraInfo(0, info); orientation = (orientation + 45) / 90 * 90; int rotation = 0; if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { rotation = (info.orientation - orientation + 360) % 360; } else { // back-facing camera rotation = (info.orientation + orientation) % 360; } param.setRotation(rotation); }
Example #20
Source File: CameraSession.java From Telegram-FOSS with GNU General Public License v2.0 | 5 votes |
protected void configureRecorder(int quality, MediaRecorder recorder) { Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraInfo.cameraId, info); int displayOrientation = getDisplayOrientation(info, false); int outputOrientation = 0; if (jpegOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) { if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { outputOrientation = (info.orientation - jpegOrientation + 360) % 360; } else { outputOrientation = (info.orientation + jpegOrientation) % 360; } } recorder.setOrientationHint(outputOrientation); int highProfile = getHigh(); boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile); boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW); if (canGoHigh && (quality == 1 || !canGoLow)) { recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile)); } else if (canGoLow) { recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW)); } else { throw new IllegalStateException("cannot find valid CamcorderProfile"); } isVideo = true; }
Example #21
Source File: CameraSession.java From Telegram-FOSS with GNU General Public License v2.0 | 5 votes |
public CameraSession(CameraInfo info, Size preview, Size picture, int format) { previewSize = preview; pictureSize = picture; pictureFormat = format; cameraInfo = info; SharedPreferences sharedPreferences = ApplicationLoader.applicationContext.getSharedPreferences("camera", Activity.MODE_PRIVATE); currentFlashMode = sharedPreferences.getString(cameraInfo.frontCamera != 0 ? "flashMode_front" : "flashMode", Camera.Parameters.FLASH_MODE_OFF); orientationEventListener = new OrientationEventListener(ApplicationLoader.applicationContext) { @Override public void onOrientationChanged(int orientation) { if (orientationEventListener == null || !initied || orientation == ORIENTATION_UNKNOWN) { return; } jpegOrientation = roundOrientation(orientation, jpegOrientation); WindowManager mgr = (WindowManager) ApplicationLoader.applicationContext.getSystemService(Context.WINDOW_SERVICE); int rotation = mgr.getDefaultDisplay().getRotation(); if (lastOrientation != jpegOrientation || rotation != lastDisplayOrientation) { if (!isVideo) { configurePhotoCamera(); } lastDisplayOrientation = rotation; lastOrientation = jpegOrientation; } } }; if (orientationEventListener.canDetectOrientation()) { orientationEventListener.enable(); } else { orientationEventListener.disable(); orientationEventListener = null; } }
Example #22
Source File: CameraSession.java From Telegram-FOSS with GNU General Public License v2.0 | 5 votes |
private int roundOrientation(int orientation, int orientationHistory) { boolean changeOrientation; if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) { changeOrientation = true; } 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; }
Example #23
Source File: Util.java From VideoFaceDetection with MIT License | 5 votes |
public static int roundOrientation(int orientation, int orientationHistory) { boolean changeOrientation = false; if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) { changeOrientation = true; } 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; }
Example #24
Source File: CameraSession.java From Telegram with GNU General Public License v2.0 | 5 votes |
private int roundOrientation(int orientation, int orientationHistory) { boolean changeOrientation; if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) { changeOrientation = true; } 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; }
Example #25
Source File: CameraView.java From LLApp with Apache License 2.0 | 5 votes |
/** * 启动屏幕朝向改变监听函数 用于在屏幕横竖屏切换时改变保存的图片的方向 */ private void startOrientationChangeListener() { OrientationEventListener mOrEventListener = new OrientationEventListener(getContext()) { @Override public void onOrientationChanged(int rotation) { if (((rotation >= 0) && (rotation <= 45)) || (rotation > 315)){ rotation=0; }else if ((rotation > 45) && (rotation <= 135)) { rotation=90; } else if ((rotation > 135) && (rotation <= 225)) { rotation=180; } else if((rotation > 225) && (rotation <= 315)) { rotation=270; }else { rotation=0; } if(rotation==mOrientation) return; mOrientation=rotation; updateCameraOrientation(); } }; mOrEventListener.enable(); }
Example #26
Source File: RotationListener.java From Viewer with Apache License 2.0 | 5 votes |
public void listen(Context context, RotationCallback callback) { // Stop to make sure we're not registering the listening twice. stop(); // Only use the ApplicationContext. In case of a memory leak (e.g. from a framework bug), // this will result in less being leaked. context = context.getApplicationContext(); this.callback = callback; this.windowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); this.orientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { WindowManager localWindowManager = windowManager; RotationCallback localCallback = RotationListener.this.callback; if(windowManager != null && localCallback != null) { int newRotation = localWindowManager.getDefaultDisplay().getRotation(); if (newRotation != lastRotation) { lastRotation = newRotation; localCallback.onRotationChanged(newRotation); } } } }; this.orientationEventListener.enable(); lastRotation = windowManager.getDefaultDisplay().getRotation(); }
Example #27
Source File: CameraSession.java From Telegram with GNU General Public License v2.0 | 5 votes |
protected void configureRecorder(int quality, MediaRecorder recorder) { Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(cameraInfo.cameraId, info); int displayOrientation = getDisplayOrientation(info, false); int outputOrientation = 0; if (jpegOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) { if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { outputOrientation = (info.orientation - jpegOrientation + 360) % 360; } else { outputOrientation = (info.orientation + jpegOrientation) % 360; } } recorder.setOrientationHint(outputOrientation); int highProfile = getHigh(); boolean canGoHigh = CamcorderProfile.hasProfile(cameraInfo.cameraId, highProfile); boolean canGoLow = CamcorderProfile.hasProfile(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW); if (canGoHigh && (quality == 1 || !canGoLow)) { recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, highProfile)); } else if (canGoLow) { recorder.setProfile(CamcorderProfile.get(cameraInfo.cameraId, CamcorderProfile.QUALITY_LOW)); } else { throw new IllegalStateException("cannot find valid CamcorderProfile"); } isVideo = true; }
Example #28
Source File: WhatsappCameraActivity.java From WhatsAppCamera with MIT License | 5 votes |
private void identifyOrientationEvents() { myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int iAngle) { final int iLookup[] = {0, 0, 0, 90, 90, 90, 90, 90, 90, 180, 180, 180, 180, 180, 180, 270, 270, 270, 270, 270, 270, 0, 0, 0}; // 15-degree increments if (iAngle != ORIENTATION_UNKNOWN) { int iNewOrientation = iLookup[iAngle / 15]; if (iOrientation != iNewOrientation) { iOrientation = iNewOrientation; if (iOrientation == 0) { mOrientation = 90; } else if (iOrientation == 270) { mOrientation = 0; } else if (iOrientation == 90) { mOrientation = 180; } } mPhotoAngle = normalize(iAngle); } } }; if (myOrientationEventListener.canDetectOrientation()) { myOrientationEventListener.enable(); } }
Example #29
Source File: Camera2FilterActivity.java From EZFilter with MIT License | 5 votes |
private int roundOrientation(int orientation, int orientationHistory) { boolean changeOrientation; if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) { changeOrientation = true; } 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; }
Example #30
Source File: OrientationHelper.java From Lassi-Android with MIT License | 5 votes |
public OrientationHelper(@NonNull Context context, @NonNull Callback callback) { mCallback = callback; mListener = new OrientationEventListener(context.getApplicationContext(), SensorManager.SENSOR_DELAY_NORMAL) { @SuppressWarnings("ConstantConditions") @Override public void onOrientationChanged(int orientation) { int or = 0; if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) { or = mDeviceOrientation != -1 ? mDeviceOrientation : 0; } else if (orientation >= 315 || orientation < 45) { or = 0; } else if (orientation >= 45 && orientation < 135) { or = 90; } else if (orientation >= 135 && orientation < 225) { or = 180; } else if (orientation >= 225 && orientation < 315) { or = 270; } if (or != mDeviceOrientation) { mDeviceOrientation = or; mCallback.onDeviceOrientationChanged(mDeviceOrientation); } } }; }