Accelerometer is a hardware sensor used to detect a shake movement in device.
This tip is about using accelerometer to move a “drawable image” on screen.
To access device sensor, we need to use “SensorManager” class. We will need to implement “SensorEventListener” listner to listen accelerometer movement and move the images accordingly.
When a movement is detected by this listner, control goes to “onSensorChanged()” method of the listner. The accelerometer reports three values: an X-axis, a Y-axis, and a Z-axis which can be accessed from “SensorEvent” object received by this method. These values are then can be used to decide the view movement.
I will use custom view “CustomDrawableView” to paint “image” and redraw it when device is moved.
Below is the code for Accelerometer Activity: