【问题标题】:How do I get the orientation of the device when the app is locked in one direction?当应用程序锁定在一个方向时,如何获取设备的方向?
【发布时间】:2015-02-10 00:39:55
【问题描述】:

我正在尝试使使用相机的照片在保存时方向正确。我尝试使用方向传感器,但似乎无法校准。我只想像这样抓住方向:How can I get the current screen orientation? 但似乎将您的应用锁定为仅纵向应用意味着它将始终仅返回纵向。

那么,尽管应用被锁定在横向模式,我如何获得屏幕的真实方向?

【问题讨论】:

    标签: android xamarin android-orientation


    【解决方案1】:

    如果你想获得活动方向,那么试试这个:

    Activity.getResources().getConfiguration().orientation
    

    如果你想获得 android 设备方向,那么试试这个:

    getResources().getConfiguration().orientation
    

    如果您遇到设备相机的方向问题,那么您将尝试以下代码 sn-p 设置相机方向:

    if (getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {   
        p.set("orientation", "portrait");
        p.set("rotation",90);
    }
    if (getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE)
    {                               
        p.set("orientation", "landscape");          
        p.set("rotation", 90);
    }
    

    【讨论】:

    • Activity.getResources().getConfiguration().orientationgetResources().getConfiguration().orientation 总是为我返回 Portrait。 IWindowManager windowManager = Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>(); var orientation3 = windowManager.DefaultDisplay.Rotation; 也总是为我返回 Rotation0。
    【解决方案2】:

    基于此响应,我终于找到了适合我的解决方案。 Android: Detect Orientation Changed 。我做了一个方向监听器。方向以度为单位,正是我所需要的。

        internal class MyOrientationListener : global::Android.Views.OrientationListener
    {
        private MainActivity _parent;
        public MyOrientationListener(Context context) : base(context)
        {
            _parent = (MainActivity)context;
        }
    
        public override void OnOrientationChanged(int orientation)
        {
            _parent.GlobalOrientation = orientation;
        }
    }
    

    编辑:注意这是针对 xamarin 的。我在 CreateBundle(bundle) 的 MainActivity 中这样设置:_orientationListener = new QuarcOrientationListener(this);

    不要忘记在 Activity 的 OnPause 和 OnResume 中 .Enable()/.Disable() 。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多