Android 即使应用程序仅限于纵向,也要知道活动的方向

Android 即使应用程序仅限于纵向,也要知道活动的方向,android,camera,screen-orientation,Android,Camera,Screen Orientation,我的应用程序仅限于肖像,但我想知道我的活动方向的变化 我需要它来检测我的相机是否在纵向模式或横向模式 我在MyApplication类中使用了registerActivityLifecycleCallback将应用程序限制为纵向,但我还是想知道用户在进行拍照活动时是否旋转了屏幕 摄像活动: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); mOrientationEventListener=new Orientati

我的应用程序仅限于肖像,但我想知道我的活动方向的变化

我需要它来检测我的相机是否在
纵向模式
横向模式

我在
MyApplication类中使用了
registerActivityLifecycleCallback
将应用程序限制为纵向,但我还是想知道用户在进行拍照活动时是否旋转了屏幕

摄像活动:

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();
        }
setRequestedOrientation(活动信息屏幕方向传感器);
MorentationEventListener=新方向EventListener(此为SensorManager.SENSOR\u DELAY\u NORMAL){
@凌驾
公共无效onOrientationChanged(int方向){
int lastOrientation=Morentation;
显示=((WindowManager)getSystemService(窗口服务)).getDefaultDisplay();
如果(display.getOrientation()==Surface.ROTATION\u 0){//面向横向的设备
摄像机方向=1;
//方法:showtoos(CameraActivity,这是“方向和景观”);
Log.e(标签“onOrientationChanged:ORIENTATION_横向”);
Log.e(标记“onOrientationChanged:CAMERA_-ORIENTATION=“+CAMERA_-ORIENTATION”);
如果(方向>=315 | |方向<45){
if(Morentation!=方向\横向\正常){
mOrientation=方向\横向\正常;
}
}否则如果(方向<315和方向>=225){
如果(Morentation!=方向\u纵向\u倒置){
mOrientation=方向\纵向\倒置;
}
}否则如果(方向<225&&orientation>=135){
如果(Morentation!=方向\横向\倒置){
mOrientation=方向\横向\倒置;
}
}否则,如果(方向45){
if(Morentation!=方向\纵向\正常){
mOrientation=方向\纵向\法线;
}
}
}else{//面向肖像的设备
摄像机方向=0;
//方法:showtoos(CameraActivity.this,“方向图”);
Log.e(标签“onOrientationChanged:ORIENTATION_-Picture”);
Log.e(标记“onOrientationChanged:CAMERA_-ORIENTATION=“+CAMERA_-ORIENTATION”);
如果(方向>=315 | |方向<45){
if(Morentation!=方向\纵向\正常){
mOrientation=方向\纵向\法线;
}
}否则如果(方向<315和方向>=225){
if(Morentation!=方向\横向\正常){
mOrientation=方向\横向\正常;
}
}否则如果(方向<225&&orientation>=135){
如果(Morentation!=方向\u纵向\u倒置){
mOrientation=方向\纵向\倒置;
}
}否则,如果(方向45){
如果(Morentation!=方向\横向\倒置){
mOrientation=方向\横向\倒置;
}
}
}
}
};
if(MoreEntationEventListener.CanDetectorOrientation()){
Toast.makeText(这是“Can检测”,Toast.LENGTH_LONG.show();
MorentationEventListener.enable();
}
否则{
Toast.makeText(这是“无法检测”,Toast.LENGTH_LONG.show();
}

您可以使用SensorManager。getOrientation获取设备在3D空间中的方向,然后将其转换为横向或纵向


或者,如果需要更新,可以注册重力传感器,查看重力是在x轴还是y轴上测量的。如果用户将其平放在桌面上,则该方法不起作用,但其他方法也不起作用。

您可以使用SensorManager。getOrientation获取设备在3D空间中的方向,然后将其转换为横向或纵向


或者,如果需要更新,可以注册重力传感器,查看重力是在x轴还是y轴上测量的。如果用户将其平放在桌面上,这将不起作用,但其他方法也不起作用。

您是否愿意知道拍摄的照片是纵向还是横向?是的,因为android本身会旋转横向拍摄的图像:),但也会旋转纵向拍摄的图像:(为什么不在拍摄后检查图像的高度和宽度,知道是在横向拍摄还是在纵向拍摄?没有,没有任何结果;(没有任何结果意味着?宽度始终大于或高度始终大于?您是否愿意知道拍摄的照片是纵向还是横向?是的,因为android本身会旋转横向拍摄的图像:),但也会旋转纵向拍摄的图像:(为什么不在拍摄后检查图像的高度和宽度,并知道是在横向还是纵向中拍摄的?没有,没有任何结果;(没有任何结果意味着?宽度总是更大还是高度总是更大?请查看我的更新代码..这会使我的屏幕旋转(但在纵向中需要)请查看我的更新代码..这使我的屏幕旋转(但需要纵向)