2016-07-22 1 views
0

Я пытаюсь показать, когда устройство выровнено по 43 и 45 градусов соответственно с обеих сторон поворота оси Y (положительное и отрицательное).Android RotationMatrix onSensorChanged y axis show toast на 43 и 45 градусов в OrientationActivity

У меня есть только до сих пор, где при повороте он показывает тост спереди и сзади на вращение оси y.

Найти соответствующий код ниже раздел:

@Override общественных недействительный onSensorChanged (SensorEvent событие) { поплавка [] rotationMatrix;

switch (event.sensor.getType()) 
    { 
     case Sensor.TYPE_GRAVITY: 
      sensorXLabel.setText(R.string.xAxisLabel); 
      sensorXValue.setText(String.valueOf(event.values[0])); 

      sensorYLabel.setText(R.string.yAxisLabel); 
      sensorYValue.setText(String.valueOf(event.values[1])); 

      sensorZLabel.setText(R.string.zAxisLabel); 
      sensorZValue.setText(String.valueOf(event.values[2])); 

      sensorYLabel.setVisibility(View.VISIBLE); 
      sensorYValue.setVisibility(View.VISIBLE); 
      sensorZLabel.setVisibility(View.VISIBLE); 
      sensorZValue.setVisibility(View.VISIBLE); 

      if (selectedSensorId == R.id.gravitySensor) 
      { 
       if (event.values[2] >= GRAVITY_THRESHOLD) 
       { 
        onFaceUp(); 
       } 
       else if (event.values[2] <= (GRAVITY_THRESHOLD * -1)) 
       { 
        onFaceDown(); 
       } 
      } 
      else 
      { 
       accelerationValues = event.values.clone(); 
       rotationMatrix = generateRotationMatrix(); 

       if (rotationMatrix != null) 
       { 
        determineOrientation(rotationMatrix); 
       } 
      } 

      break; 
     case Sensor.TYPE_ACCELEROMETER: 
      accelerationValues = event.values.clone(); 
      rotationMatrix = generateRotationMatrix(); 

      if (rotationMatrix != null) 
      { 
       determineOrientation(rotationMatrix); 
      } 
      break; 
     case Sensor.TYPE_MAGNETIC_FIELD: 
      magneticValues = event.values.clone(); 
      rotationMatrix = generateRotationMatrix(); 

      if (rotationMatrix != null) 
      { 
       determineOrientation(rotationMatrix); 
      } 
      break; 
     case Sensor.TYPE_ROTATION_VECTOR: 

      rotationMatrix = new float[16]; 
      SensorManager.getRotationMatrixFromVector(rotationMatrix, 
        event.values); 
      determineOrientation(rotationMatrix); 
      break; 
    } 
} 

@Override 
public void onAccuracyChanged(Sensor sensor, int accuracy) 
{ 
    Log.d(TAG, 
      String.format("Accuracy for sensor %s = %d", 
      sensor.getName(), accuracy)); 
} 

/** 
* Generates a rotation matrix using the member data stored in 
* accelerationValues and magneticValues. 
* 
* @return The rotation matrix returned from 
* {@link android.hardware.SensorManager#getRotationMatrix(float[], float[], float[], float[])} 
* or <code>null</code> if either <code>accelerationValues</code> or 
* <code>magneticValues</code> is null. 
*/ 
private float[] generateRotationMatrix() 
{ 
    float[] rotationMatrix = null; 

    if (accelerationValues != null && magneticValues != null) 
    { 
     rotationMatrix = new float[16]; 
     boolean rotationMatrixGenerated; 
     rotationMatrixGenerated = 
       SensorManager.getRotationMatrix(rotationMatrix, 
       null, 
       accelerationValues, 
       magneticValues); 

     if (!rotationMatrixGenerated) 
     { 
      Log.w(TAG, getString(R.string.rotationMatrixGenFailureMessage)); 

      rotationMatrix = null; 
     } 
    } 

    return rotationMatrix; 
} 

/** 
* Uses the last read accelerometer and gravity values to determine if the 
* device is face up or face down. 
* 
* @param rotationMatrix The rotation matrix to use if the orientation 
* calculation 
*/ 
private void determineOrientation(float[] rotationMatrix) 
{ 
    float[] orientationValues = new float[3]; 
    SensorManager.getOrientation(rotationMatrix, orientationValues); 

    double azimuth = Math.toDegrees(orientationValues[0]); 
    double pitch = Math.toDegrees(orientationValues[1]); 
    double roll = Math.toDegrees(orientationValues[2]); 

    sensorXLabel.setText(R.string.azimuthLabel); 
    sensorXValue.setText(String.valueOf(azimuth)); 

    sensorYLabel.setText(R.string.pitchLabel); 
    sensorYValue.setText(String.valueOf(pitch)); 

    sensorZLabel.setText(R.string.rollLabel); 
    sensorZValue.setText(String.valueOf(roll)); 

    sensorYLabel.setVisibility(View.VISIBLE); 
    sensorYValue.setVisibility(View.VISIBLE); 
    sensorZLabel.setVisibility(View.VISIBLE); 
    sensorZValue.setVisibility(View.VISIBLE); 

    if (pitch <= 10) 
    { 
     if (Math.abs(roll) >= 170) 
     { 
      onFaceDown(); 
     } 
     else if (Math.abs(roll) <= 10) 
     { 
      onFaceUp(); 
     } 
    } 
} 
+0

Любая помощь очень ценится. – user1245716

+0

Мне нужно внести коррективы в этот раздел кода. Но я не знаю, как это сделать. – user1245716

+0

Я могу получить положительный тост для тоста, чтобы подтвердить FaceDown() – user1245716

ответ

0

Изменения должны быть внесены в событие изменения датчика. Кажется, что проблема может быть связана с используемыми единицами. Может только тост на положительном вращении.