
Marko Gargenta
@MarkoGargenta
Marakana, Inc.
This is a little example on how to write a simple compass application in Android. It illustrates two points:
- How to use Sensors
- How to create custom Views (widgets)
The Activity: src/com/marakana/Compass.javaThis Activity is using the SensorManager to register for sensor updates. Notice that we're using an older API. This is on purpose so that it runs on my 1.5 HTC phone.
Code:
package com.marakana;
import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
// implement SensorListener
public class Compass extends Activity implements SensorListener {
SensorManager sensorManager;
static final int sensor = SensorManager.SENSOR_ORIENTATION;
Rose rose;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
rose = new Rose(this);
setContentView(rose);
// get sensor manager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
// register to listen to sensors
@Override
public void onResume() {
super.onResume();
sensorManager.registerListener(this, sensor);
}
// unregister
@Override
public void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
// Ignore for now
public void onAccuracyChanged(int sensor, int accuracy) {
}
// Listen to sensor and provide output
public void onSensorChanged(int sensor, float[] values) {
if (sensor != Compass.sensor)
return;
int orientation = (int) values[0];
rose.setDirection(orientation);
}
}
The Custom View src/com/marakana/Rose.java
This is the custom view that we create to implement the compass rose. We simply subclass ImageView, override the onDraw() method, and provide some logic to draw the image and rotate it based on what orientation sensor tells us to.
Code:
package com.marakana;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.widget.ImageView;
public class Rose extends ImageView {
Paint paint;
int direction = 0;
public Rose(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(2);
paint.setStyle(Style.STROKE);
this.setImageResource(R.drawable.compassrose);
}
@Override
public void onDraw(Canvas canvas) {
int height = this.getHeight();
int width = this.getWidth();
canvas.rotate(direction, width / 2, height / 2);
super.onDraw(canvas);
}
public void setDirection(int direction) {
this.direction = direction;
this.invalidate();
}
}
The Output
Sourcehttp://marakana.com/static/tutorials/Compass.ziphttp://marakana.com/static/tutorials/Compass.apk
Edited one time. Last edit by Marko Gargenta on Jun 15, 2010 at 1:17:27 AM (about one year ago).

Ziyauddin Azmi
Slktechlabs
I m quite new to Android and still learning things. I have a doubt regarding the sensors. Is there any relation between sensors and views in Android? Can anyone please clear my doubt Thanks a lot.

Naves T
Android
Hey thank you for the source...I am new to developing android apps and I was wondering can I run this source code as a .html file in chrome to test it out?
thank you

Marc Bernardo
Studant
Studant
This work is deprecated...Beacuse of the SensorEvent, now it is used the SensorEventListener*... Can someone help me changing this code for the new Sensor?
Thanks
Edited one time. Last edit by Marc Bernardo on Jul 11, 2011 at 9:23:55 AM (about one year ago).