获得 Phone 个角度

Getting Phone Angles

现在我正在尝试获得 phone 的旋转。我已经设法编写代码来执行此操作,但不幸的是,它仅在单击按钮时才提供值。每次值更改时如何使 TextView 更新?换句话说 - 这个有 EventListener 吗?如果是,如何实现?

public class MainActivity extends Activity {

SensorManager _sensorManager;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        

    SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);        

    final float[] mValuesMagnet      = new float[3];
    final float[] mValuesAccel       = new float[3];
    final float[] mValuesOrientation = new float[3];
    final float[] mRotationMatrix    = new float[9];

    final Button btn_valider = (Button) findViewById(R.id.btn1);
    final TextView txt1 = (TextView) findViewById(R.id.textView1);
    final SensorEventListener mEventListener = new SensorEventListener() {
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        public void onSensorChanged(SensorEvent event) {
            // Handle the events for which we registered
            switch (event.sensor.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
                    break;

                case Sensor.TYPE_MAGNETIC_FIELD:
                    System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
                    break;
            }
        };
    };

    // You have set the event lisetner up, now just need to register this with the
    // sensor manager along with the sensor wanted.
    setListners(sensorManager, mEventListener);

    btn_valider.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
            SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
            final CharSequence test;
            float XAxis = (float) (mValuesOrientation[0] * 180/Math.PI);
            float YAxis = (float) (mValuesOrientation[1] * 180/Math.PI);
            float ZAxis = (float) (mValuesOrientation[2] * 180/Math.PI);
            test = "results: " + XAxis +" "+ YAxis+ " "+ ZAxis;
            txt1.setText(test);
        }
    });

}

// Register the event listener and sensor type.
public void setListners(SensorManager sensorManager, SensorEventListener mEventListener)
{
    sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
            SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
                SensorManager.SENSOR_DELAY_NORMAL);

    }
}

将方位计算部分移到单独的方法中:

private void calculateOrientation(){
      SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
      SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
      final CharSequence test;
      float XAxis = (float) (mValuesOrientation[0] * 180/Math.PI);
      float YAxis = (float) (mValuesOrientation[1] * 180/Math.PI);
      float ZAxis = (float) (mValuesOrientation[2] * 180/Math.PI);
      test = "results: " + XAxis +" "+ YAxis+ " "+ ZAxis;
      txt1.setText(test);
}

请注意,您已经在计算方向后通过调用 "txt1.setText(test);" 更新了 TextView 的值。因此,现在您只需确保每次更改传感器数据时都会调用此方向计算方法。

那么,只需从 onSensorChanges() 方法内部调用此方法即可:

public void onSensorChanged(SensorEvent event) {
      // Handle the events for which we registered
      switch (event.sensor.getType()) {
         case Sensor.TYPE_ACCELEROMETER:
            System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
            break;

         case Sensor.TYPE_MAGNETIC_FIELD:
             System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
             break;
      }

      //Call the orientation calculation method
      calculateOrientation();
 };

每次有可用的新传感器数据时,这应该会更新您的 TextView。