Compass

April 10, 2010 9:36:14 PM PDT (3 years ago). Seen 18,545 times. 3 replies.
Photo Marko Gargenta
@MarkoGargenta
Marakana, Inc.
Member since Jan 19, 2007
Location: San Francisco
Forum Posts: 227
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.java

This 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


Source
http://marakana.com/static/tutorials/Compass.zip
http://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).
June 13, 2011 12:01:46 AM PDT (one year ago)
Photo Ziyauddin Azmi
Slktechlabs
Member since Jun 12, 2011
Forum Posts: 2
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.
June 14, 2011 11:54:52 AM PDT (one year ago)
Photo Naves T
Android
Member since Jun 14, 2011
Forum Posts: 1
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
July 11, 2011 8:51:26 AM PDT (one year ago)
Photo Marc Bernardo
Studant
Studant
Member since Jul 11, 2011
Forum Posts: 1
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).