Advertisement
sgccarey

Android Compass App

Oct 11th, 2011
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.08 KB | None | 0 0
  1. /* V0.13
  2.  *  
  3.  * 6/10/11 - 1725
  4.  *
  5.  */
  6.  
  7. package com.saltapps.compass;
  8.  
  9. import android.app.Activity;
  10. import android.graphics.Bitmap;
  11. import android.graphics.BitmapFactory;
  12. import android.graphics.Matrix;
  13. import android.graphics.drawable.BitmapDrawable;
  14. import android.hardware.Sensor;
  15. import android.hardware.SensorEvent;
  16. import android.hardware.SensorEventListener;
  17. import android.hardware.SensorManager;
  18. import android.os.Bundle;
  19. import android.util.Log;
  20. import android.view.Display;
  21. import android.view.WindowManager;
  22. import android.widget.ImageView;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25.  
  26. public class Compass extends Activity implements SensorEventListener {
  27.    
  28.   static final String TAG = "Compass";
  29.    
  30.   SensorManager sensorManager;  // SensorManager lets you access the device's sensors
  31.   Sensor sOrientation;          // needed for determining the sensor's orientation
  32.  
  33.   WindowManager mWindowManager; // The interface that apps use to talk to the window manager
  34.   Display mDisplay;             // needed for getRotation, determining the handset's display orientation
  35.  
  36.        
  37.   ImageView img;                                    // allows us to manipulate (rotate) the compass arrow
  38.   TextView tvAzimuth, tvDirection, tvRotation;      // for displaying degrees, direction and screen rotation
  39.  
  40.   int vAzi, iRotation;  // degrees wrt mag north, screen rotation value
  41.  
  42.   Toast tstAcc;         // a toast message to indicate accuracy has changed
  43.    
  44.  
  45.   @Override
  46.   public void onCreate(Bundle savedInstanceState) { // carried out when activity launched
  47.     super.onCreate(savedInstanceState);
  48.     setContentView(R.layout.compass);   // lays the activity out according to the main.xml file in the res/layout folder
  49.    
  50.       sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);                     // get sensor service
  51.       sOrientation = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);               // sets sensor to orientation
  52.       mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);                    // get window service (for screen orientation purposes)
  53.       mDisplay = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();    // Returns default Display object
  54.    
  55.     tvAzimuth = (TextView) findViewById(R.id.tvAzimuth);        // for displaying degrees
  56.     tvDirection = (TextView) findViewById(R.id.tvDirection);    // for displaying direction
  57.     tvRotation = (TextView) findViewById(R.id.tvRotation);      // for displaying screen rotation
  58.    
  59.     img = (ImageView) findViewById(R.id.ivCompass);         // allows us to manipulate (rotate) the compass arrow
  60.      
  61.   } // onCreate
  62.  
  63.  
  64.   @Override
  65.   public void onResume() {  // actions to carry out upon returning to this activity
  66.     super.onResume();
  67.     sensorManager.registerListener(this, sOrientation, SensorManager.SENSOR_DELAY_NORMAL); // register to listen to sensors
  68.   } // onResume
  69.  
  70.  
  71.   @Override
  72.   public void onPause() {   // actions to carry out when activity isn't in focus
  73.     super.onPause();
  74.     sensorManager.unregisterListener(this, sOrientation);  // unregister on pause to conserve battery power
  75.   } // onPause
  76.  
  77.  
  78.  
  79.  
  80.   public void onSensorChanged(SensorEvent event) {           // Listens to sensor and provides output
  81.        
  82.        /* event.values[0] : yaw, rotation around z axis - Azimuth
  83.         * event.values[1] : pitch, rotation around x axis
  84.         * event.values[2] : roll, rotation around y axis */
  85.    
  86.  
  87.     iRotation = mDisplay.getRotation();
  88.    
  89.     switch(mDisplay.getRotation()) {    // determines angle to correct arrow by
  90.    
  91.         case 0: iRotation = 0;      // screen is in standard portrait mode, no rotation necessary
  92.         break;
  93.         case 1: iRotation = 90;     // screen rotated left from portrait, 90 degree rotation necessary
  94.         break;
  95.         case 2: iRotation = -180;   // screen upside down, 180 rotation necessary
  96.         break;
  97.         case 3: iRotation = -90;    // screen rotated right from portrait, -90 degree rotation necessary
  98.         break;    
  99.        
  100.     }   // switch
  101.      
  102.     tvRotation.setText("Screen Orientation: " + iRotation + "°");  // displays Orientation info
  103.  
  104.     vAzi = (int) event.values[0] + iRotation;   // changed from math.round, + iRotation to compensate for landscape mode. Makes camera view compass needle
  105.    
  106.     if (vAzi >= 360)        // 0 <= vAzi < 360
  107.         vAzi -= 360;
  108.     else if (vAzi < 0)
  109.         vAzi += 360;
  110.    
  111.     if (vAzi < 22)
  112.         tvDirection.setText("(N)");                 // determines which direction we're looking towards
  113.      else if (vAzi >= 22 && vAzi < 67)
  114.         tvDirection.setText("(NE)");
  115.      else if (vAzi >= 67 && vAzi < 112)
  116.         tvDirection.setText("(E)");
  117.      else if (vAzi >= 112 && vAzi < 157)
  118.         tvDirection.setText("(SE)");
  119.      else if (vAzi >= 157 && vAzi < 202)
  120.         tvDirection.setText("(S)");
  121.      else if (vAzi >= 202 && vAzi < 247)
  122.         tvDirection.setText("(SW)");
  123.      else if (vAzi >= 247 && vAzi < 292)
  124.         tvDirection.setText("(W)");
  125.      else if (vAzi >= 292 && vAzi < 337)
  126.         tvDirection.setText("(NW)");
  127.      else if (vAzi >= 337)
  128.         tvDirection.setText("(N)");
  129.        else
  130.           tvDirection.setText("Null");
  131.    
  132.     tvAzimuth.setText("Direction: " + vAzi +"°    ");  // outputs values to TextView
  133.  
  134.  
  135.     drawArrow(vAzi);    // sends vAzi value to drawArrow method
  136.            
  137.            
  138.   } // onSensorChanged
  139.  
  140.  
  141.   public void drawArrow(int vAzi) { // draws arrow on screen, according to vAzi value
  142.      
  143.      
  144.         img=(ImageView)findViewById(R.id.ivCompass);                    // sets our manipulatable image up using our compass imageView, which uses compass1.png
  145.         Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.compass1); // encodes our png file as a bitmap, for manipulation using BitmapFactory
  146.        
  147.         int w = bmp.getWidth(); // Getting width & height of the given image
  148.         int h = bmp.getHeight();
  149.        
  150.         Matrix mtx = new Matrix();
  151.         mtx.postRotate(-vAzi);  // Setting post rotate angle according to the value obtained from handset's digital compass. Negative to rotate back towards North
  152.                                                                            
  153.        
  154.         Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);        // Rotating Bitmap around  point we defined using half the .png file's width & height
  155.         BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);                        // sets Bitmap as drawable
  156.  
  157.         img.setImageDrawable(bmd);                                                  // finally, draws bitmap
  158.      
  159.      
  160.   } // drawArrow
  161.  
  162.  
  163.  
  164. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  165.        
  166.     if(sensor.getType()==Sensor.TYPE_ACCELEROMETER){
  167.         Log.d(TAG, "Accuracy has changed: "+ accuracy); // logcats that acc has changed
  168.        
  169.         switch(accuracy) {                              // displays toast message when accuracy changes
  170.         case 0: tstAcc.setText("Accuracy Unreliable");
  171.                 tstAcc.show();
  172.         break;
  173.         case 1: tstAcc.setText("Accuracy Low");
  174.                 tstAcc.show();
  175.         break;
  176.         case 2: tstAcc.setText("Accuracy Medium");
  177.                 tstAcc.show();
  178.         break;
  179.         case 3: tstAcc.setText("Accuracy High");
  180.                 tstAcc.show();
  181.         break;     
  182.         }   // switch
  183.  
  184.        
  185.     } // if
  186.  
  187.  
  188. }   // onAccuracyChanged
  189.  
  190.    
  191.  
  192. }   // class
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement