Compass App

Quickly share your Android Code Snippets here...

Compass App

Postby ashwani » Thu Mar 10, 2011 1:05 pm

Hey, i had just created an Compass App so i would like to share it with you. here is the code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.exercise.AndroidCompass;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Color;
  8. import android.graphics.Matrix;
  9. import android.graphics.Paint;
  10. import android.util.AttributeSet;
  11. import android.view.View;
  12.  
  13. public class MyCompassView extends View {
  14.  
  15. private float direction = 0;
  16. private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  17. private boolean firstDraw;
  18. private Bitmap arrow;
  19. private int xpos;
  20. private int ypos;
  21.  
  22. public MyCompassView(Context context) {
  23. super(context);
  24. // TODO Auto-generated constructor stub
  25. init();
  26. }
  27.  
  28. public MyCompassView(Context context, AttributeSet attrs) {
  29. super(context, attrs);
  30. // TODO Auto-generated constructor stub
  31. init();
  32. }
  33.  
  34. public MyCompassView(Context context, AttributeSet attrs, int defStyle) {
  35. super(context, attrs, defStyle);
  36. // TODO Auto-generated constructor stub
  37. init();
  38. }
  39.  
  40. private void init(){
  41.  
  42. paint.setStyle(Paint.Style.STROKE);
  43. paint.setStrokeWidth(3);
  44. paint.setColor(Color.RED);
  45. paint.setTextSize(30);
  46. arrow = BitmapFactory
  47. .decodeResource(getResources(), R.drawable.arrow);
  48. xpos = arrow.getWidth() / 2;
  49. ypos = arrow.getHeight() / 2;
  50. firstDraw = true;
  51. }
  52.  
  53. @Override
  54. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  55. // TODO Auto-generated method stub
  56. setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
  57. }
  58.  
  59. @Override
  60. protected void onDraw(Canvas canvas) {
  61. // TODO Auto-generated method stub
  62. int cxCompass = getMeasuredWidth()/2;
  63. int cyCompass = getMeasuredHeight()/2;
  64. float radiusCompass;
  65.  
  66. if(cxCompass > cyCompass){
  67.  radiusCompass = (float) (cyCompass * 0.9);
  68. }
  69. else{
  70.  radiusCompass = (float) (cxCompass * 0.9);
  71. }
  72. //canvas.drawCircle(cxCompass, cyCompass, radiusCompass, paint);
  73. //canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
  74. if(!firstDraw){
  75.  /*canvas.drawLine(cxCompass, cyCompass,
  76.    (float)(cxCompass + radiusCompass * Math.sin((double)(-direction) * 3.14/180)),
  77.    (float)(cyCompass - radiusCompass * Math.cos((double)(-direction) * 3.14/180)),
  78.    paint);*/
  79.         Matrix transform = new Matrix();
  80.     transform.setTranslate(cxCompass-xpos, cyCompass-ypos);
  81.     transform.preRotate(direction,xpos, ypos);
  82.     canvas.drawBitmap(arrow, transform, null);
  83.  //canvas.drawText(String.valueOf(direction), cxCompass, cyCompass, paint);
  84. }
  85.  
  86. }
  87.  
  88. public void updateDirection(float dir)
  89. {
  90. firstDraw = false;
  91. direction = dir;
  92. invalidate();
  93. }
  94.  
  95. }
  96.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

the Class that communicates with hardware and main activity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.exercise.AndroidCompass;
  2.  
  3. import java.util.List;
  4.  
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.hardware.Sensor;
  8. import android.hardware.SensorEvent;
  9. import android.hardware.SensorEventListener;
  10. import android.hardware.SensorManager;
  11. import android.os.Bundle;
  12. import android.widget.Toast;
  13.  
  14. public class AndroidCompass extends Activity {
  15.  
  16. private static SensorManager mySensorManager;
  17. private boolean sersorrunning;
  18. private MyCompassView myCompassView;
  19.  
  20.  /** Called when the activity is first created. */
  21.  @Override
  22.  public void onCreate(Bundle savedInstanceState) {
  23.      super.onCreate(savedInstanceState);
  24.      setContentView(R.layout.main);
  25.  
  26.      myCompassView = (MyCompassView)findViewById(R.id.mycompassview);
  27.  
  28.      mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  29.      List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
  30.  
  31.      if(mySensors.size() > 0){
  32.       mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
  33.       sersorrunning = true;
  34.       Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show();
  35.    
  36.      }
  37.      else{
  38.       Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
  39.       sersorrunning = false;
  40.       finish();
  41.      }
  42.  }
  43.  
  44.  private SensorEventListener mySensorEventListener = new SensorEventListener(){
  45.  
  46. @Override
  47. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  48.  // TODO Auto-generated method stub
  49.  
  50. }
  51.  
  52. @Override
  53. public void onSensorChanged(SensorEvent event) {
  54.  // TODO Auto-generated method stub
  55.  myCompassView.updateDirection((float)event.values[0]);
  56. }
  57.  };
  58.  
  59. @Override
  60. protected void onDestroy() {
  61. // TODO Auto-generated method stub
  62. super.onDestroy();
  63.  
  64. if(sersorrunning){
  65.  mySensorManager.unregisterListener(mySensorEventListener);
  66. }
  67. }
  68.  
  69. }
  70.  
  71.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


output:
compass.png
Out Put of the emulator version is here
compass.png (172 KiB) Viewed 1463 times

source code:
compass.zip
compass_try is for devices and compass_P is for emulator it works with sensorsimulator from Open Intent
(1.1 MiB) Downloaded 465 times
ashwani
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 10, 2011 12:43 pm

Top

Re: Compass App

Postby ashwani » Tue Mar 15, 2011 4:22 am

please let me know if you have made any changes or want any improvements in this app... i will be very happy to read your comments. :)
ashwani
Freshman
Freshman
 
Posts: 8
Joined: Thu Mar 10, 2011 12:43 pm

Re: Compass App

Postby ruelblapid » Tue Mar 15, 2011 4:26 am

Nice one dude, I will give it a try and send you feedback after :)

Cheers,
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests