Get device angle by using getOrientation() function

别说谁变了你拦得住时间么 提交于 2019-11-30 13:18:28

问题


I was using Sensor.TYPE_ORIENTATION to determine current angle of device but TYPE_ORIENTATION is deprecated on API version 8. In SensorManager manual it refers to getOrientation() function in order to use TYPE_ORIENTATION.

Here is the manual


Here is my old code :

public void onSensorChanged(SensorEvent event) {
        Log.d("debug","Sensor Changed");
        if (event.sensor.getType()==Sensor.TYPE_ORIENTATION) {
            Log.d("debug",Float.toString(event.values[0]));


            float mAzimuth = event.values[0];
            float mPitch = event.values[1];
            float mRoll = event.values[2];

            Log.d("debug","mAzimuth :"+Float.toString(mAzimuth));
            Log.d("debug","mPitch :"+Float.toString(mPitch));
            Log.d("debug","mRoll :"+Float.toString(mRoll));
        }

    }

I'm really confused about using getOrientation() function, can anyone please show me an example how to get the angles?


回答1:


You now use two sensors (ACCELEROMETER and MAGNETIC_FIELD) to get that information. See blog post for more detail.

public class CompassActivity extends Activity implements SensorEventListener {

  private SensorManager mSensorManager;
  Sensor accelerometer;
  Sensor magnetometer;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(mCustomDrawableView);    // Register the sensor listeners
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  }

  protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
    mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
  }

  protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
  }

  public void onAccuracyChanged(Sensor sensor, int accuracy) {  }

  float[] mGravity;
  float[] mGeomagnetic;
  public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
      mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
      mGeomagnetic = event.values;
    if (mGravity != null && mGeomagnetic != null) {
      float R[] = new float[9];
      float I[] = new float[9];
      boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
      if (success) {
        float orientation[] = new float[3];
        SensorManager.getOrientation(R, orientation);
        azimut = orientation[0]; // orientation contains: azimut, pitch and roll
      }
    }
  }
}

Permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>



回答2:


Regarding your second question. When you are registering your sensor listeners, change your code to read:

protected void onResume() {          
    super.onResume();          
    mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);          
    mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);          
}


来源:https://stackoverflow.com/questions/20339942/get-device-angle-by-using-getorientation-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!