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: |
package com.acc.activities; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.View; public class AccelometerActivity extends Activity implements SensorEventListener { CustomView mCustomDrawableView = null; ShapeDrawable mDrawable = new ShapeDrawable(); public static int x; public static int y; private SensorManager sensorManager = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); // Get a reference to a SensorManager. mCustomDrawableView = new CustomView(this); // our custom view to handle drawing on device movement. setContentView(mCustomDrawableView); } // This method will update the UI on new sensor events public void onSensorChanged(SensorEvent sensorEvent) { { if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { x = (int) Math.pow(sensorEvent.values[1], 2); y = (int) Math.pow(sensorEvent.values[2], 2); } if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) { } } } public void onAccuracyChanged(Sensor arg0, int arg1) { // TODO Auto-generated method stub } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); // Registering this class as a listener for the accelerometer sensor sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL); // Registering the orientation sensor } @Override protected void onStop() { sensorManager.unregisterListener(this); // Unregistering the listener super.onStop(); } public class CustomView extends View { static final int width = 50; static final int height = 50; public CustomView(Context context) { super(context); mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint().setColor(0xf044AC67); mDrawable.setBounds(x, y, x + width, y + height); } protected void onDraw(Canvas canvas) { RectF oval = new RectF(AccelometerActivity.x, AccelometerActivity.y, AccelometerActivity.x + width, AccelometerActivity.y + height); // setting bounds of rectangle Paint p = new Paint(); // setting paint options p.setColor(Color.BLUE); canvas.drawOval(oval, p); invalidate(); } } }
You need to Register your class as a listner for the accelerometer sensor in the “onResume()” activity lifecycle method.