Android屏幕上的传感器坐标

时间:2014-06-06 20:32:32

标签: java android

我有一个小应用程序,它向我展示Eclipse上LogCat中传感器的坐标,我想在手机屏幕上看到同样的东西。你能给我一些提示我应该怎么做吗?它不一定是代码,只是提示。

我不知道如何通过mySensors.java中的坐标更新MainActivity中的TextView。我只收到静态“x = 0 y = 0 z = 0”。不能再这样了......

我的MainActivity.java:

public class MainActivity extends Activity {

SensorManager mSensorManager;
mySensors mSensors;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSensors = new mySensors();
}

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

    mSensorManager.unregisterListener(mSensors);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    initSensors();
}

private void initSensors() {
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    List<Sensor> accel_sensor_list = mSensorManager
            .getSensorList(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener((SensorEventListener) mSensors,
            accel_sensor_list.get(0), SensorManager.SENSOR_DELAY_UI);
    List<Sensor> magn_sensor_list = mSensorManager
            .getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
    mSensorManager.registerListener((SensorEventListener) mSensors,
            magn_sensor_list.get(0), SensorManager.SENSOR_DELAY_UI);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

我的mySensors.java:

public class mySensors implements SensorEventListener {

private static int VECTOR_SIZE=3;
private static int MATRIX_SIZE=16;

int type, heading_deg, pitch_deg, roll_deg;
boolean isReady;
float[] vals = new float[VECTOR_SIZE];
float[] accels = new float[VECTOR_SIZE];
float[] mags = new float[VECTOR_SIZE];
float[] orientation_rad = new float[VECTOR_SIZE];

float[] R_orig = new float[MATRIX_SIZE];
float[] I_orig = new float[MATRIX_SIZE];
float[] R_remapped = new float[MATRIX_SIZE];

public void onSensorChanged(SensorEvent se) {
    type = se.sensor.getType();
    vals = se.values.clone();
    switch (type) {
    case Sensor.TYPE_ACCELEROMETER:
        accels = vals.clone();
        break;
    case Sensor.TYPE_MAGNETIC_FIELD:
        mags = vals.clone();
        isReady = true;
    }
    if (isReady && mags != null && accels != null) {
        isReady = false;
        SensorManager.getRotationMatrix(R_orig, I_orig, accels, mags);
        // zmiana układ współrzednych odniesienia (!)
        // SensorManager.remapCoordinateSystem(R_orig,
        // SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X, R_remapped);
        SensorManager.getOrientation(R_orig, orientation_rad);
        heading_deg = (int) (orientation_rad[0] * 57.2957795f); // radians
                                                                // to
                                                                // degrees
        pitch_deg = (int) (orientation_rad[1] * 57.2957795f);
        roll_deg = (int) (orientation_rad[2] * 57.2957795f);

        Log.w("Sensors:",
                "x=" + heading_deg +
                " y=" + pitch_deg +
                " z=" + roll_deg
        );
    }
}

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

}
 }

1 个答案:

答案 0 :(得分:0)

您可以创建自己的侦听器界面(example how to do it )来侦听onSensorChanged结果,并调用主Activity,您可以将textText设置为TextView。