2016-02-12 138 views
-1
boolean sensor=sensorManager.getRotationMatrix(orentation_factr,inclination,mGravity,mMagnetic); 
if(sensor) { 
    sensorManager.getOrientation(orentation_factr, orientation_angle); 
    Log.e("testore_0", "true " + orientation_angle[0]); 
}else{Log.e("testore_0", "false");} 

这是我试过的,getrotationmatrix的返回值总是假的。所以getoreintation给出0.0getorientation()返回o.o总是

这是全班。

public class AndroidAccelerometerExample extends Activity implements SensorEventListener { 

public Vibrator v; 
int orientation; 
float[] orentation_factr = new float[9]; 
float[] orientation_angle = new float[9]; 
float[] inclination = new float[9]; 
private float lastX, lastY, lastZ; 
private SensorManager sensorManager; 
private Sensor accelerometer; 
private float deltaX = 0; 
private float deltaY = 0; 
private float deltaZ = 0; 
private float vibrateThreshold = 0; 
private TextView currentX, currentY, currentZ; 
private float[] mGravity = new float[3]; 
private float[] mMagnetic = new float[3]; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    initializeViews(); 
    mGravity = new float[3]; 
    mMagnetic = new float[3]; 
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) { 
     // success! we have an accelerometer 

     accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     //magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 
     sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); 
     vibrateThreshold = accelerometer.getMaximumRange()/2; 
    } else { 
     // fai! we dont have an accelerometer! 
    } 

    //initialize vibration 
    v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE); 

} 

public void initializeViews() { 
    currentX = (TextView) findViewById(R.id.currentX); 
    currentY = (TextView) findViewById(R.id.currentY); 
    currentZ = (TextView) findViewById(R.id.currentZ); 

} 

//onResume() register the accelerometer for listening the events 
protected void onResume() { 
    super.onResume(); 
    sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); 
} 

//onPause() unregister the accelerometer for stop listening the events 
protected void onPause() { 
    super.onPause(); 
    sensorManager.unregisterListener(this); 
} 

@Override 
public void onAccuracyChanged(Sensor sensor, int accuracy) { 

} 

@Override 
public void onSensorChanged(SensorEvent event) { 
    displayCleanValues(); 
    displayCurrentValues(); 

    // get the change of the x,y,z values of the accelerometer 
    deltaX = Math.abs(lastX - event.values[0]); 
    deltaY = Math.abs(lastY - event.values[1]); 
    deltaZ = Math.abs(lastZ - event.values[2]); 

    // if the change is below 2, it is just plain noise 
    if (deltaX < 2) 
     deltaX = 0; 
    if (deltaY < 2) 
     deltaY = 0; 
    if (deltaZ < 2) 
     deltaZ = 0; 

    // set the last know values of x,y,z 
    lastX = event.values[0]; 
    lastY = event.values[1]; 
    lastZ = event.values[2]; 

    switch (event.sensor.getType()) { 
     case Sensor.TYPE_ACCELEROMETER: 
      mGravity = event.values.clone(); 

      break; 
     case Sensor.TYPE_MAGNETIC_FIELD: 
      mMagnetic = event.values.clone(); 
      break; 
    } 

    boolean sensor = sensorManager.getRotationMatrix(orentation_factr, inclination, mGravity, mMagnetic); 
    if (sensor) { 
     sensorManager.getOrientation(orentation_factr, orientation_angle); 
     Log.e("testore_0", "true " + orientation_angle[0]); 
    } else { 
     Log.e("testore_0", "false"); 
    } 

    vibrate(); 
    orentationchange(event); 
} 

public void orentationchange(SensorEvent event) { 
    synchronized (this) { 
     if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
      RotateAnimation animation = null; 
      if (event.values[0] < 4 && event.values[0] > -4) { 
       if (event.values[1] > 0 && orientation != ExifInterface.ORIENTATION_ROTATE_90) { 
        // UP 
        orientation = ExifInterface.ORIENTATION_ROTATE_90; 

        Log.e("test", "up"); 
       } else if (event.values[1] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_270) { 
        // UP SIDE DOWN 
        Log.e("test", "down"); 
        orientation = ExifInterface.ORIENTATION_ROTATE_270; 
       } 
      } else if (event.values[1] < 4 && event.values[1] > -4) { 
       if (event.values[0] > 0 && orientation != ExifInterface.ORIENTATION_NORMAL) { 
        // LEFT 
        Log.e("test", "left"); 
        orientation = ExifInterface.ORIENTATION_NORMAL; 
       } else if (event.values[0] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_180) { 
        // RIGHT 
        Log.e("test", "right"); 
        orientation = ExifInterface.ORIENTATION_ROTATE_180; 
       } 
      } 
      if (animation != null) { 
      } 
     } 

    } 
} 

public void vibrate() { 
    if ((deltaX > vibrateThreshold) || (deltaY > vibrateThreshold) || (deltaZ > vibrateThreshold)) { 
     v.vibrate(50); 

     Toast.makeText(getApplicationContext(), Math.cos(Math.toRadians(50)) + "", Toast.LENGTH_SHORT).show(); 
    } 
} 

public void displayCleanValues() { 
    currentX.setText("0.0"); 
    currentY.setText("0.0"); 
    currentZ.setText("0.0"); 
} 

// display the current x,y,z accelerometer values 
public void displayCurrentValues() { 
    currentX.setText(Float.toString(deltaX)); 
    currentY.setText(Float.toString(deltaY)); 
    currentZ.setText(Float.toString(deltaZ)); 
} 
} 
+0

尝试阅读此答案,它可能会有所帮助:[无法从getOrientation方法获取正确度](http://stackoverflow.com/questions/15315970/not-able-to-fetch-correct-degree-from- getorientation方法) – Emna

回答

0

音高的计算公式为:pitch =(float)Math.asin(-RMatrix [7]); arcsin函数的范围是[-PI/2,PI/2],因此只能在-PI/2和PI/2之间取值。