将文本设置为半径方向

时间:2014-03-22 04:39:01

标签: android android-sensors android-orientation

所以这是我的活动类中的代码。我不知道自己做错了什么。我试图将文本更改为以度为单位的方向。什么都没发生,只是停留在0。有什么建议吗?这是我的活动类中的代码。我遗漏了细节

public class WorkoutBuddy extends Activity implements SensorEventListener {

    SensorManager sm;
    MediaPlayer m;
    MediaPlayer r;
    TextView t1, t2, t3; 
    float x, y, z, gyroX, gyroY, gyroZ;
    float[] Rm = new float[16];
    float[] I = new float[16];
    boolean correctPosition = false, mPlayed = false, rPlayed = false;
    SensorManager mSensorManager;
    Sensor accelerometer;
    Sensor magnetometer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exercise_buddy);
        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        accelerometer = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        magnetometer = mSensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).get(0);
        t1 =(TextView) findViewById(R.id.textView1);
        t2 =(TextView) findViewById(R.id.textView2);
        t3 =(TextView) findViewById(R.id.textView3);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
    }

      float[] geomagnetic;
      float[] gravity;
      float azimuth,pitch,roll;
      boolean havemag, haveacc;

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            geomagnetic[0] = event.values[0];
            geomagnetic[1] = event.values[1];
            geomagnetic[2] = event.values[2];
            havemag = true;
        }
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            gravity[0] = event.values[0];
            gravity[1] = event.values[1];
            gravity[2] = event.values[2];
            haveacc = true;
        }

        if (haveacc && havemag) {
            if (SensorManager.getRotationMatrix(Rm, I, gravity, geomagnetic)) {
                float[] result = new float[3];
                SensorManager.getOrientation(Rm, result);

                azimuth = result[0];
                pitch = result[1];
                roll = result[2];
            }
        }

        t1.setText("Sensor reading for Y = "+ pitch);
        t2.setText("Sensor reading for Y = "+ roll);
        t3.setText("Sensor reading for Y = "+ azimuth);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        haveacc = false;
        havemag = false;
        mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);

        super.onResume();
    }




}

0 个答案:

没有答案