简体   繁体   English

如何通过加速度计确定方向?

[英]How to determine orientation through accelerometer?

I'm using Processing for Android and my app is almost finished. 我正在使用Android处理器,我的应用程序几乎完成了。 However, Processing for Android doesn't adapt to phone orientation, which I want to implement. 但是,Android版处理功能无法适应我想要实现的手机方向。 It reads Accelerometer values and I've been able to roughly determine the orientation through this simple algorhitm: 它读取加速度计值,我已经能够通过这个简单的算法粗略地确定方向:

  if (y > 7)
  {
    o = UPRIGHT;
  }
  else if (y < -7)
  {
    o = UPSIDEDOWN;
  }      
  else if (x > 7)
  {
    o = TURNLEFT;
  }
  else
  {
    o = TURNRIGHT;
  }   

It's not flawless, so I'm wondering if there's a better solution. 它并不完美,所以我想知道是否有更好的解决方案。 Is there a way to poll Android for the current phone UI orientation? 有没有办法针对当前手机UI方向推荐Android? Is there a more stable value than 7 (which I chose kind of arbitrarily)? 是否有一个比7更稳定的值(我选择了哪种方式)?

By monitoring for the following configuration change on orientation, based on the AndroidManifest.xml , for example, assuming that activity is called myActivity , then the following would suffice: 通过监视方向的以下配置更改,例如,基于AndroidManifest.xml ,假设该活动被称为myActivity ,那么以下就足够了:

<activity android:name=".myActivity" 
    android:configChanges="orientation" 
    android:label="MyActivity" />

And from the myActivity class, handle it with this: myActivity类中,使用以下方法处理它:

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    switch(newConfig.orientation){
    case Configuration.ORIENTATION_UNDEFINED:
        Log.v(TAG, "[onConfigurationChanged] - Undefined");
        break;
    case Configuration.ORIENTATION_SQUARE:
        Log.v(TAG, "[onConfigurationChanged] - Square");
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        Log.v(TAG, "[onConfigurationChanged] - Portrait");
        break;
    case Configuration.ORIENTATION_LANDSCAPE:
        Log.v(TAG, "[onConfigurationChanged] - Landscape");
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM