Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * V0.02
- * 9/10/11 - 1315
- * crosshair, text added
- * tried using sensor.TYPE_ORIENTATION, but causes jumps from 0 to 180, which may be problematic in final version
- * locked to portrait for satmap version.
- * keeps crashing?? not sure why, ddms no help
- */
- package com.saltapps.satmap;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.os.Bundle;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- public class Accelerometer extends Activity implements SensorEventListener{
- float x, y, vPitch, vRoll;
- Bitmap bubble, crosshair;
- SensorManager sM;
- GFX2Surface ourSurfaceView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new GFX2Surface(this));
- sM = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- if (sM.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) { // checks that phone has accelerometer by making sure SensorList is not empty (=0)
- Sensor s = sM.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); // assigns the available accelerometer to variable 's'
- sM.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL); // sets up sensor listener
- } // if
- bubble = BitmapFactory.decodeResource(getResources(), R.drawable.bubble);
- crosshair = BitmapFactory.decodeResource(getResources(), R.drawable.crosshair);
- x = y = vPitch = vRoll = 0;
- ourSurfaceView = new GFX2Surface(this);
- ourSurfaceView.resume();
- setContentView(ourSurfaceView);
- } // onCreate
- public class GFX2Surface extends SurfaceView implements Runnable{ // class within a class
- SurfaceHolder ourHolder;
- Thread ourThread = null;
- boolean isRunning = false;
- public GFX2Surface(Context context) {
- super(context);
- ourHolder = getHolder();
- } // MyBringBackSurface constructor
- public void pause() {
- isRunning = false;
- while(true) {
- try {
- ourThread.join(); // Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies
- } catch (InterruptedException e) {
- e.printStackTrace();
- } // try/catch
- break;
- } // while
- ourThread = null;
- } // pause()
- public void resume() {
- isRunning = true;
- ourThread = new Thread(this); // refers to run method
- ourThread.start();
- } // resume
- public void run() {
- while(isRunning) {
- if(!ourHolder.getSurface().isValid()) // if surface is not valid
- continue;
- Canvas canvas = ourHolder.lockCanvas(); // locks the canvas while we set it up, so no other activities can edit it
- canvas.drawColor(Color.BLACK); // sets color of canvas
- float centerX = (canvas.getWidth()/2) - (bubble.getWidth()/2);
- float centerY = (canvas.getHeight()/2) - (bubble.getHeight()/2);
- float centerCrossX = (canvas.getWidth()/2) - (crosshair.getWidth()/2);
- float centerCrossY = (canvas.getHeight()/2) - (crosshair.getWidth()/2);
- canvas.drawBitmap(crosshair, centerCrossX , centerCrossY, null);
- canvas.drawBitmap(bubble, centerX - vRoll*20, centerY + vPitch*34, null); // places ball according to sensor amplified by 20/34 (difference due to screen ratio)
- Paint paint = new Paint();
- paint.setStyle(Paint.Style.FILL);
- paint.setColor(Color.GREEN);
- paint.setTextSize(30);
- paint.setAntiAlias(true);
- canvas.drawText("Pitch: " + (int) (vPitch*10) + "°", 0, 25, paint);
- canvas.drawText("Roll: " + (int) (vRoll*10) + "°", 0, 55, paint);
- ourHolder.unlockCanvasAndPost(canvas);
- } // while
- } // run()
- } // class within a class
- @Override
- protected void onPause() {
- super.onPause();
- sM.unregisterListener(this);
- } // onPause
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- } // onAccuracyChanged
- public void onSensorChanged(SensorEvent event) {
- try {
- Thread.sleep(16); // Delays to 60 sensor checks per second
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- } // try/catch
- vRoll = event.values[0];
- vPitch = event.values[1];
- } // onSensorChanged
- } // class
Advertisement
Add Comment
Please, Sign In to add comment