A Compass Application in Android

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

https://www.protechtraining.com/static/tutorials/Compass.zip
https://www.protechtraining.com/static/tutorials/Compass.apk

Copyright © 2018 ProTech. All Rights Reserved.

Sign In Create Account

Navigation

Social Media