Graphics Programs Reference
In-Depth Information
ivity
fields are used to make the rotations smooth. You can modify these fields to
adjust the sensitivity of touch.
Rotation Using Android Sensors
Now we discuss the use of Android sensors as UI to update the 3D rendering on an
OpenGL surface (
Figures 2-17
to
2-21
)
. Because of the varying support for sensors
across various Android devices, we restrict the use of sensors to:
▪
Accelerometer
(Motion sensor)
▪
Gravity sensor
(Motion sensor)
▪
Magnetometer
(Position sensor)
We access the sensors available on an Android device using the Android sensor API.
This API helps us perform important sensor-related tasks on an Android handheld,
such as determining the availability of a sensor, requesting raw sensor data, and re-
gistering sensor event listeners.
To get started with this API, create an instance of the sensor service, as shown in
Listing
2-13.
SENSOR ROTATION/src/com/apress/android/sensorrotation/
Main.java
SensorManager sm = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
To create an instance of a specific sensor, use the class
Sensor
. The
SensorMan-
ager
class provides a method
getDefaultSensor(int type)
to access
Sensor
objects specified by the
int
type. However, we start receiving notifica-
tions for sensor events by registering a
SensorEventListener
. As in the ap-
plication discussed in the previous section, the
Main
class for sensor application
also implements a listener, but this time a
SensorEventListener
(
an-
droid.hardware.SensorEventListener
).
The interface
SensorEventListener
contains an important callback method
onSensorChanged
that provides the raw sensor data through the argument
Search WWH ::
Custom Search