现在我正试图让手机旋转。我设法编写代码,这样做,但不幸的是它只在按钮点击时给出了值。每次值更改时,如何更新TextView
?换句话说 - 这有什么EventListener
吗?如果是,如何实施呢?
public class MainActivity extends Activity {
SensorManager _sensorManager;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
final float[] mValuesMagnet = new float[3];
final float[] mValuesAccel = new float[3];
final float[] mValuesOrientation = new float[3];
final float[] mRotationMatrix = new float[9];
final Button btn_valider = (Button) findViewById(R.id.btn1);
final TextView txt1 = (TextView) findViewById(R.id.textView1);
final SensorEventListener mEventListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
// Handle the events for which we registered
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
break;
}
};
};
// You have set the event lisetner up, now just need to register this with the
// sensor manager along with the sensor wanted.
setListners(sensorManager, mEventListener);
btn_valider.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
final CharSequence test;
float XAxis = (float) (mValuesOrientation[0] * 180/Math.PI);
float YAxis = (float) (mValuesOrientation[1] * 180/Math.PI);
float ZAxis = (float) (mValuesOrientation[2] * 180/Math.PI);
test = "results: " + XAxis +" "+ YAxis+ " "+ ZAxis;
txt1.setText(test);
}
});
}
// 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);
}
}
答案 0 :(得分:1)
使用单独的方法移动方向计算部分:
private void calculateOrientation(){
SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
final CharSequence test;
float XAxis = (float) (mValuesOrientation[0] * 180/Math.PI);
float YAxis = (float) (mValuesOrientation[1] * 180/Math.PI);
float ZAxis = (float) (mValuesOrientation[2] * 180/Math.PI);
test = "results: " + XAxis +" "+ YAxis+ " "+ ZAxis;
txt1.setText(test);
}
请注意,您已经通过调用" txt1.setText(test);"来更新TextView的值。计算方向后。因此,现在您必须确保每次更改传感器数据时都调用此方向计算方法。
那么,只需从onSensorChanges()方法中调用此方法:
public void onSensorChanged(SensorEvent event) {
// Handle the events for which we registered
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
break;
}
//Call the orientation calculation method
calculateOrientation();
};
每次有新的传感器数据时,都应该更新TextView。