Game Development Reference
In-Depth Information
builder.append(event.values[1]);
builder.append(", z: ");
builder.append(event.values[2]);
textView.setText(builder.toString());
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// nothing to do here
}
}
We start by checking whether an accelerometer sensor is available. If it is, we fetch it from
the SensorManager and try to register our activity, which implements the SensorEventListener
interface. If any of this fails, we set the TextView to display a proper error message.
The onSensorChanged() method simply reads the axis values from the SensorEvent that are
passed to it and updates the TextView text accordingly.
The onAccuracyChanged() method is there so that we fully implement the SensorEventListener
interface. It serves no real other purpose.
Figure 4-9 shows what values the axes take on in portrait and landscape modes when the
device is held perpendicular to the ground.
Figure 4-9. Accelerometer axes values in portrait mode (left) and landscape mode (right) when the device is held
perpendicular to the ground
One thing that's a gotcha for Android accelerometer handling is the fact that the accelerometer
values are relative to the default orientation of the device. This means that if your game is run
only in landscape, a device where the default orientation is portrait will have values 90 degrees
different from those of a device where the default orientation is landscape! This is the case on
 
Search WWH ::




Custom Search