Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* V0.13
- *
- * 6/10/11 - 1725
- *
- */
- package com.saltapps.compass;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Matrix;
- import android.graphics.drawable.BitmapDrawable;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Display;
- import android.view.WindowManager;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Compass extends Activity implements SensorEventListener {
- static final String TAG = "Compass";
- SensorManager sensorManager; // SensorManager lets you access the device's sensors
- Sensor sOrientation; // needed for determining the sensor's orientation
- WindowManager mWindowManager; // The interface that apps use to talk to the window manager
- Display mDisplay; // needed for getRotation, determining the handset's display orientation
- ImageView img; // allows us to manipulate (rotate) the compass arrow
- TextView tvAzimuth, tvDirection, tvRotation; // for displaying degrees, direction and screen rotation
- int vAzi, iRotation; // degrees wrt mag north, screen rotation value
- Toast tstAcc; // a toast message to indicate accuracy has changed
- @Override
- public void onCreate(Bundle savedInstanceState) { // carried out when activity launched
- super.onCreate(savedInstanceState);
- setContentView(R.layout.compass); // lays the activity out according to the main.xml file in the res/layout folder
- sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); // get sensor service
- sOrientation = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); // sets sensor to orientation
- mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); // get window service (for screen orientation purposes)
- mDisplay = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); // Returns default Display object
- tvAzimuth = (TextView) findViewById(R.id.tvAzimuth); // for displaying degrees
- tvDirection = (TextView) findViewById(R.id.tvDirection); // for displaying direction
- tvRotation = (TextView) findViewById(R.id.tvRotation); // for displaying screen rotation
- img = (ImageView) findViewById(R.id.ivCompass); // allows us to manipulate (rotate) the compass arrow
- } // onCreate
- @Override
- public void onResume() { // actions to carry out upon returning to this activity
- super.onResume();
- sensorManager.registerListener(this, sOrientation, SensorManager.SENSOR_DELAY_NORMAL); // register to listen to sensors
- } // onResume
- @Override
- public void onPause() { // actions to carry out when activity isn't in focus
- super.onPause();
- sensorManager.unregisterListener(this, sOrientation); // unregister on pause to conserve battery power
- } // onPause
- public void onSensorChanged(SensorEvent event) { // Listens to sensor and provides output
- /* event.values[0] : yaw, rotation around z axis - Azimuth
- * event.values[1] : pitch, rotation around x axis
- * event.values[2] : roll, rotation around y axis */
- iRotation = mDisplay.getRotation();
- switch(mDisplay.getRotation()) { // determines angle to correct arrow by
- case 0: iRotation = 0; // screen is in standard portrait mode, no rotation necessary
- break;
- case 1: iRotation = 90; // screen rotated left from portrait, 90 degree rotation necessary
- break;
- case 2: iRotation = -180; // screen upside down, 180 rotation necessary
- break;
- case 3: iRotation = -90; // screen rotated right from portrait, -90 degree rotation necessary
- break;
- } // switch
- tvRotation.setText("Screen Orientation: " + iRotation + "°"); // displays Orientation info
- vAzi = (int) event.values[0] + iRotation; // changed from math.round, + iRotation to compensate for landscape mode. Makes camera view compass needle
- if (vAzi >= 360) // 0 <= vAzi < 360
- vAzi -= 360;
- else if (vAzi < 0)
- vAzi += 360;
- if (vAzi < 22)
- tvDirection.setText("(N)"); // determines which direction we're looking towards
- else if (vAzi >= 22 && vAzi < 67)
- tvDirection.setText("(NE)");
- else if (vAzi >= 67 && vAzi < 112)
- tvDirection.setText("(E)");
- else if (vAzi >= 112 && vAzi < 157)
- tvDirection.setText("(SE)");
- else if (vAzi >= 157 && vAzi < 202)
- tvDirection.setText("(S)");
- else if (vAzi >= 202 && vAzi < 247)
- tvDirection.setText("(SW)");
- else if (vAzi >= 247 && vAzi < 292)
- tvDirection.setText("(W)");
- else if (vAzi >= 292 && vAzi < 337)
- tvDirection.setText("(NW)");
- else if (vAzi >= 337)
- tvDirection.setText("(N)");
- else
- tvDirection.setText("Null");
- tvAzimuth.setText("Direction: " + vAzi +"° "); // outputs values to TextView
- drawArrow(vAzi); // sends vAzi value to drawArrow method
- } // onSensorChanged
- public void drawArrow(int vAzi) { // draws arrow on screen, according to vAzi value
- img=(ImageView)findViewById(R.id.ivCompass); // sets our manipulatable image up using our compass imageView, which uses compass1.png
- Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.compass1); // encodes our png file as a bitmap, for manipulation using BitmapFactory
- int w = bmp.getWidth(); // Getting width & height of the given image
- int h = bmp.getHeight();
- Matrix mtx = new Matrix();
- mtx.postRotate(-vAzi); // Setting post rotate angle according to the value obtained from handset's digital compass. Negative to rotate back towards North
- 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
- BitmapDrawable bmd = new BitmapDrawable(rotatedBMP); // sets Bitmap as drawable
- img.setImageDrawable(bmd); // finally, draws bitmap
- } // drawArrow
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- if(sensor.getType()==Sensor.TYPE_ACCELEROMETER){
- Log.d(TAG, "Accuracy has changed: "+ accuracy); // logcats that acc has changed
- switch(accuracy) { // displays toast message when accuracy changes
- case 0: tstAcc.setText("Accuracy Unreliable");
- tstAcc.show();
- break;
- case 1: tstAcc.setText("Accuracy Low");
- tstAcc.show();
- break;
- case 2: tstAcc.setText("Accuracy Medium");
- tstAcc.show();
- break;
- case 3: tstAcc.setText("Accuracy High");
- tstAcc.show();
- break;
- } // switch
- } // if
- } // onAccuracyChanged
- } // class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement