Using orientation sensors: simple compass sample

Source: Internet
Author: User
Tags drawtext

In this post we will show how to get the sensor informatió N of the device to do a simple compass application.

Getting the magnetic orientation with the type_orientation sensor is deprecated, And now you must subscribe to type_accelerometer and type_magnetic_field to get the correct orientation. this way is a little tricky, but with this sample you will get it very
Easy.

This is the code for an activity that shows in a canvas a simple compass:

package com.samplecompass;import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Paint.Style;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 CompassActivity extends Activity implements SensorEventListener {   Float azimut;  // View to draw a compass   public class CustomDrawableView extends View {    Paint paint = new Paint();    public CustomDrawableView(Context context) {      super(context);      paint.setColor(0xff00ff00);      paint.setStyle(Style.STROKE);      paint.setStrokeWidth(2);      paint.setAntiAlias(true);    };     protected void onDraw(Canvas canvas) {      int width = getWidth();      int height = getHeight();      int centerx = width/2;      int centery = height/2;      canvas.drawLine(centerx, 0, centerx, height, paint);      canvas.drawLine(0, centery, width, centery, paint);      // Rotate the canvas with the azimut           if (azimut != null)        canvas.rotate(-azimut*360/(2*3.14159f), centerx, centery);      paint.setColor(0xff0000ff);      canvas.drawLine(centerx, -1000, centerx, +1000, paint);      canvas.drawLine(-1000, centery, 1000, centery, paint);      canvas.drawText("N", centerx+5, centery-10, paint);      canvas.drawText("S", centerx-10, centery+15, paint);      paint.setColor(0xff00ff00);    }  }   CustomDrawableView mCustomDrawableView;  private SensorManager mSensorManager;  Sensor accelerometer;  Sensor magnetometer;   protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    mCustomDrawableView = new CustomDrawableView(this);    setContentView(mCustomDrawableView);    // Register the sensor listeners    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);      accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);    magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);  }   protected void onResume() {    super.onResume();    mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);    mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);  }   protected void onPause() {    super.onPause();    mSensorManager.unregisterListener(this);  }   public void onAccuracyChanged(Sensor sensor, int accuracy) {  }   float[] mGravity;  float[] mGeomagnetic;  public void onSensorChanged(SensorEvent event) {    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)      mGravity = event.values;    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)      mGeomagnetic = event.values;    if (mGravity != null && mGeomagnetic != null) {      float R[] = new float[9];      float I[] = new float[9];      boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);      if (success) {        float orientation[] = new float[3];        SensorManager.getOrientation(R, orientation);        azimut = orientation[0]; // orientation contains: azimut, pitch and roll      }    }    mCustomDrawableView.invalidate();  }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.