sgccarey

Android Accelerometer app

Oct 11th, 2011
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. /*
  2.  * V0.02
  3.  * 9/10/11 - 1315
  4.  * crosshair, text added
  5.  * tried using sensor.TYPE_ORIENTATION, but causes jumps from 0 to 180, which may be   problematic in final version
  6.  * locked to portrait for satmap version.
  7.  * keeps crashing?? not sure why, ddms no help
  8.  */
  9.  
  10.  package com.saltapps.satmap;
  11.  
  12.  import android.app.Activity;
  13.  import android.content.Context;
  14.  import android.graphics.Bitmap;
  15.  import android.graphics.BitmapFactory;
  16.  import android.graphics.Canvas;
  17. import android.graphics.Color;
  18. import android.graphics.Paint;
  19.  import android.hardware.Sensor;
  20.  import android.hardware.SensorEvent;
  21.  import android.hardware.SensorEventListener;
  22.  import android.hardware.SensorManager;
  23.  import android.os.Bundle;
  24.  import android.view.SurfaceHolder;
  25. import android.view.SurfaceView;
  26.  
  27.  public class Accelerometer extends Activity  implements SensorEventListener{
  28.    
  29.     float x, y, vPitch, vRoll;
  30.     Bitmap bubble, crosshair;
  31.     SensorManager sM;
  32.     GFX2Surface ourSurfaceView;
  33.    
  34.    
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(new GFX2Surface(this));
  39.                
  40.         sM = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  41.        
  42.         if (sM.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {  // checks that phone has accelerometer by making sure SensorList is not empty (=0)
  43.             Sensor s = sM.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);  // assigns the available accelerometer to variable 's'
  44.             sM.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);    // sets up sensor listener
  45.         }   // if
  46.        
  47.         bubble = BitmapFactory.decodeResource(getResources(), R.drawable.bubble);
  48.         crosshair = BitmapFactory.decodeResource(getResources(), R.drawable.crosshair);
  49.         x = y = vPitch = vRoll = 0;
  50.         ourSurfaceView = new GFX2Surface(this);
  51.         ourSurfaceView.resume();
  52.         setContentView(ourSurfaceView);
  53.        
  54.     }   // onCreate
  55.    
  56.    
  57.     public class GFX2Surface extends SurfaceView implements Runnable{   // class within a class
  58.        
  59.         SurfaceHolder ourHolder;
  60.         Thread ourThread = null;
  61.         boolean isRunning = false;
  62.  
  63.         public GFX2Surface(Context context) {
  64.             super(context);
  65.             ourHolder = getHolder();
  66.                                
  67.         }   // MyBringBackSurface constructor
  68.        
  69.         public void pause() {      
  70.             isRunning = false;
  71.             while(true) {
  72.                 try {
  73.                     ourThread.join();   // Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies
  74.                 } catch (InterruptedException e) {
  75.                     e.printStackTrace();
  76.                 }   // try/catch
  77.                 break;
  78.             }   // while
  79.            
  80.             ourThread = null;
  81.            
  82.         }   // pause()
  83.        
  84.         public void resume() {
  85.            
  86.             isRunning = true;
  87.             ourThread = new Thread(this);   // refers to run method
  88.             ourThread.start();
  89.            
  90.         }   // resume
  91.  
  92.         public void run() {
  93.            
  94.             while(isRunning) {
  95.                
  96.                 if(!ourHolder.getSurface().isValid())   // if surface is not valid
  97.                     continue;
  98.                
  99.                 Canvas canvas = ourHolder.lockCanvas(); // locks the canvas while we set it up, so no other activities can edit it
  100.                 canvas.drawColor(Color.BLACK);          // sets color of canvas
  101.                
  102.                 float centerX = (canvas.getWidth()/2) - (bubble.getWidth()/2);
  103.                 float centerY = (canvas.getHeight()/2) - (bubble.getHeight()/2);
  104.                 float centerCrossX = (canvas.getWidth()/2) - (crosshair.getWidth()/2);
  105.                 float centerCrossY = (canvas.getHeight()/2) - (crosshair.getWidth()/2);
  106.                
  107.                 canvas.drawBitmap(crosshair, centerCrossX , centerCrossY, null);
  108.                 canvas.drawBitmap(bubble, centerX - vRoll*20, centerY + vPitch*34, null);   // places ball according to sensor amplified by 20/34 (difference due to screen ratio)
  109.                
  110.                 Paint paint = new Paint();
  111.                 paint.setStyle(Paint.Style.FILL);
  112.                 paint.setColor(Color.GREEN);
  113.                 paint.setTextSize(30);
  114.                 paint.setAntiAlias(true);
  115.            
  116.                 canvas.drawText("Pitch: " + (int) (vPitch*10) + "°", 0, 25, paint);
  117.                 canvas.drawText("Roll: " + (int) (vRoll*10) + "°", 0, 55, paint);
  118.                
  119.                 ourHolder.unlockCanvasAndPost(canvas);
  120.                                
  121.             }   // while
  122.                                
  123.         }   // run()
  124.  
  125.     }   // class within a class
  126.  
  127.     @Override
  128.     protected void onPause() {
  129.         super.onPause();
  130.         sM.unregisterListener(this);   
  131.     }   // onPause
  132.  
  133.     public void onAccuracyChanged(Sensor sensor, int accuracy) {       
  134.     }   // onAccuracyChanged
  135.  
  136.     public void onSensorChanged(SensorEvent event) {
  137.        
  138.         try {
  139.             Thread.sleep(16);   // Delays to 60 sensor checks per second
  140.         } catch (InterruptedException e1) {
  141.             e1.printStackTrace();
  142.         }   // try/catch
  143.        
  144.         vRoll = event.values[0];
  145.         vPitch = event.values[1];
  146.                
  147.     }   // onSensorChanged
  148.    
  149.    
  150.    
  151.  
  152.  }  // class
  153.  
  154.  
Advertisement
Add Comment
Please, Sign In to add comment