Android Tilting-Mind吹

时间:2013-07-03 12:58:47

标签: android rotation orientation

所以这是我的代码,如果有人能够帮助你。

我希望显示方向值,但我得到的是0.0 0.0 0.0所以我不知道发生了什么我不认为它是我的手机因为它相当现代(HTC One V)

 package ple.x75;


 import android.app.Activity;
 import android.content.Context;
 import android.content.pm.ActivityInfo;
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
 import android.os.Bundle;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.widget.Button;
 import android.view.View;




 public class MainActivity extends Activity
{


private ImageView img;
private SensorManager mSensorManager;
private float[] mAccelerometerReading;
private float[] mMagneticFieldReading;
private float[] mRotationMatrix = new float[16];
private float[] mRemapedRotationMatrix = new float[16];
private float[] mOrientation = new float[3];

// 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);
}


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

    SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
    final Button btn_valider = (Button) findViewById(R.id.button);
    final TextView txt1 = (TextView) findViewById(R.id.textView);

    final SensorEventListener mEventListener = new SensorEventListener() {
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
        public void onSensorChanged(SensorEvent event) {
            switch (event.sensor.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    mAccelerometerReading = event.values.clone();
                    break;

                case Sensor.TYPE_MAGNETIC_FIELD:
                    mMagneticFieldReading = event.values.clone();
                    break;
            }
            if(mAccelerometerReading != null && mMagneticFieldReading != null &&
                    SensorManager.getRotationMatrix(mRotationMatrix, null, mAccelerometerReading, mMagneticFieldReading))
            {
                SensorManager.remapCoordinateSystem(mRotationMatrix,
                        SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, mRemapedRotationMatrix);
                SensorManager.getOrientation(mRemapedRotationMatrix, mOrientation);
            }

        };
    };
    setListners(sensorManager, mEventListener);
    btn_valider.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            if(mOrientation != null)
            {
                final CharSequence test;
                test="Results: "+mOrientation[0]+" "+mOrientation[1]+" "+mOrientation[2];
                txt1.setText(test);
            }
            else{
                final CharSequence test;
                test="Results: Apparently Nothing...";
                txt1.setText(test);
            }
}
 });
}
}

0 个答案:

没有答案