0

getRotationMatrix() を使用して、v[0]、v[1]、v[2] および対応する方位角、ピッチ、ロールの値を onSensorChanged() メソッドで計算しています。ブール値の detectAzimuth が true になったときに、最初の v[0] (または対応する方位角) の値のみを firstAzimuth に保存する方法を知りたいですか?

private boolean detectAzimuth = false;
private float firstAzimuth;

@Override
public void onSensorChanged(SensorEvent event) {

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
    accValues = event.values.clone();
}

if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
    geoValues = event.values.clone();
}

boolean success = SensorManager.getRotationMatrix(r, i, accValues,
    geoValues);

if (success) {
    SensorManager.getOrientation(r, v);

    if (detectAzimuth) {
    azimuth = v[0] * (180 / Math.PI);
    }
    pitch = v[1] * (180 / Math.PI);
    roll = v[2] * (180 / Math.PI);
}
}
4

1 に答える 1

0

初めて方位角の値を取得したかどうかを確認できます。そうでない場合は、値を取得し、detectAzimuth を true にします。次に、値を SharedPreference に保存します。

次回は、ブール変数を使用して、方位角の値を初めて取得したかどうかを確認しますdetectAzimuth。それが本当なら、それはあなたがすでにそれを取ったことを意味します。次に、Sharedpreference から取得して に割り当てfirstAzimuthます。そのため、取得した最初の値の値が常に得azimuthられます。

if (success) {
    SensorManager.getOrientation(r, v);
    if (!detectAzimuth) {  // if not taken yet
        azimuth = v[0] * (180 / Math.PI); // take the value
        detectAzimuth = true; //make the bolean true to know that you've taken the value
        //Store in SharedPreference
        SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
        editor.putFloat("firstAzimuth", azimuth);
        editor.commit();
    }else{ //if already taken value first time
        //get the first value from SharedPreference and assign it to firstAzimuth
        SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
        firstAzimuth = prefs.getFloat("firstAzimuth", 0.0);
        //take the new value
        azimuth = v[0] * (180 / Math.PI); // take the new value but don't store it
    }
    pitch = v[1] * (180 / Math.PI);
    roll = v[2] * (180 / Math.PI);
    }
}

お役に立てれば。参考までに、これにより、azimuth既に 1 つの値を受け取り、その存在を毎回チェックしているため、それ以上の値を取得できなくなります。あなたがそれを望んでいるかどうかはわかりません。そうでない場合は、可能性についてさらに話し合うことができます。

于 2013-07-12T22:04:03.747 に答える