我的应用仅限于肖像,我仍然想了解我的活动方向更改
我需要这个以检测我的相机是否已在Portrait mode
或Landscape mode
我在registerActivityLifecycleCallback
中使用MyApplication class
将应用限制为肖像,但我还想知道用户是否在相机活动中旋转了屏幕
CameraActivity:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
mOrientationEventListener=new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
int lastOrientation = mOrientation;
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if (display.getOrientation() == Surface.ROTATION_0) { // landscape oriented devices
CAMERA_ORIENTATION = 1;
// methods.showToast(CameraActivity.this,"ORIENTATION_LANDSCAPE");
Log.e(TAG, "onOrientationChanged: ORIENTATION_LANDSCAPE ");
Log.e(TAG, "onOrientationChanged: CAMERA_ORIENTATION = "+CAMERA_ORIENTATION);
if (orientation >= 315 || orientation < 45) {
if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
}
} else if (orientation < 315 && orientation >= 225) {
if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
mOrientation = ORIENTATION_PORTRAIT_INVERTED;
}
} else if (orientation < 225 && orientation >= 135) {
if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
}
} else if (orientation <135 && orientation > 45) {
if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
mOrientation = ORIENTATION_PORTRAIT_NORMAL;
}
}
} else { // portrait oriented devices
CAMERA_ORIENTATION = 0;
//methods.showToast(CameraActivity.this, "ORIENTATION_PORTRAIT");
Log.e(TAG, "onOrientationChanged: ORIENTATION_PORTRAIT ");
Log.e(TAG, "onOrientationChanged: CAMERA_ORIENTATION = "+CAMERA_ORIENTATION);
if (orientation >= 315 || orientation < 45) {
if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
mOrientation = ORIENTATION_PORTRAIT_NORMAL;
}
} else if (orientation < 315 && orientation >= 225) {
if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
}
} else if (orientation < 225 && orientation >= 135) {
if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
mOrientation = ORIENTATION_PORTRAIT_INVERTED;
}
} else if (orientation <135 && orientation > 45) {
if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
}
}
}
}
};
if (mOrientationEventListener.canDetectOrientation()){
Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
mOrientationEventListener.enable();
}
else{
Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
您可以使用SensorManager。 getOrientation获取设备在3D空间中的方向,然后将其转换为横向或纵向。
或者,如果您需要更新,可以注册重力传感器,看看重力是在x轴还是y轴上测量。如果用户将其平放在桌面上,那将无法工作,但另一种方法也不会。