Mr
 Mr. Rogers' IB Computer Science - Google Android SmartPhone Programming Project Help boost K-12 science, math, and computer education. Donate your obsolete Android phone to us (see below)! We promise to put it to good use in a classroom.

 Home Projects 2010-2011    Level  Greenville, SC
Quick Summary

Title
Bubble Level

Status
Working

Purpose
Aids a user in determining how level a surface is.

Features
Visually represents how level a surface is.

Instrictons
Place the phone on a flat object and adjust the object to center
the bubble drawn on the device.

To Do
Make the bubble more sensitive to make the level
easier to use.
 Key Classes and Fields

Screenshots

CODE

 Bubble Class

package tessier.jean.com;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Bubble {

public Bubble(int xP, int yP, int r)
{
x = xP;
y = yP;
}

public void draw(Canvas c, Paint g)
{
g.setColor(Color.GREEN);
}

public void setY(int newY)
{
y = newY;
}

public void setX(int newX)
{
x = newX;
}

public void setR(int newR)
{
}
}

 Board Class

package tessier.jean.com;

import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class Board extends View
{
int _height, _width;
int cX, cY1, cY2, r;

Bubble bubble;

Bitmap _bitmap;
Canvas _canvas;
Paint g;

public Board(Context context)
{
super(context);
g = new Paint();
g.setColor(Color.WHITE);
g.setStyle(Paint.Style.FILL);

bubble = new Bubble(10,50000,0);
}

private void drawBoard()
{
g.setColor(Color.BLACK);
_canvas.drawRect(0,0,500,500,g);
g.setColor(Color.WHITE);
_canvas.drawRect(cX-r, cY1, cX+r, cY2, g);//left, top, right, bottom, paint
_canvas.drawCircle(cX, cY1, r, g); //centerX, centerY, radius, paint
_canvas.drawCircle(cX, cY2, r, g);
//bubble.setY((int)(Math.random()*100));
bubble.draw(_canvas, g);
}

public Bubble getBubble()
{
return bubble;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
_height = View.MeasureSpec.getSize(heightMeasureSpec);
_width = View.MeasureSpec.getSize(widthMeasureSpec);

setMeasuredDimension(_width, _height);

_bitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);
_canvas = new Canvas(_bitmap);

cX = _width/2;
cY1 = _height/4;
cY2 = (_height*3)/4;
r = _width/10;
bubble.setR(r);
bubble.setX(cX);
drawBoard();
}
@Override
protected void onDraw(Canvas canvas) {
drawBoard();
canvas.drawBitmap(_bitmap, 0, 0, g);
invalidate();
}
}

 SensorProgram Class

package tessier.jean.com;

import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;

import android.app.Activity;
import android.content.Context;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class SensorProgram extends Activity implements /*AccelerometerListener*/ SensorListener
{
private double highAccel, totalAccel;
private SensorManagerSimulator mSensorManager;
//private SensorManager mSensorManager;
private static Context CONTEXT;
private Board b;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
b=new Board(this);
setContentView(b);
CONTEXT = this;
highAccel = 0;
totalAccel = 0;
//mSensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);

mSensorManager.connectSimulator();
mSensorManager.registerListener(this, SensorManager.SENSOR_ACCELEROMETER
| SensorManager.SENSOR_MAGNETIC_FIELD
| SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_FASTEST);
}

public static Context getContext()
{
return CONTEXT;
}

public void onAccelerationChanged(float x, float y, float z) {
((TextView)findViewById(R.id.x)).setText("X: "+String.valueOf(x));
((TextView)findViewById(R.id.y)).setText("Y: "+String.valueOf(y));
((TextView)findViewById(R.id.z)).setText("Z: "+String.valueOf(z));
}

public void onShake(float force) {
// TODO Auto-generated method stub
}

@Override
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
}

@Override
public void onSensorChanged(int sensor, float[] values) {
// TODO Auto-generated method stub
// if(sensor == SensorManager.SENSOR_ORIENTATION)
// {
// ((TextView)findViewById(R.id.x)).setText("X: "+String.valueOf(values[0]));
// ((TextView)findViewById(R.id.y)).setText("Y: "+String.valueOf(values[1]));
// ((TextView)findViewById(R.id.z)).setText("Z: "+String.valueOf(values[2]));
// if(values[2]>Math.abs(highAccel)){
// ((TextView)findViewById(R.id.maxZ)).setText("Max Z: "+String.valueOf(values[2]));
// highAccel = values[2];
// }
// }
if(sensor == SensorManager.SENSOR_ORIENTATION)
{
b.getBubble().setY(215+(int)(values[1]*1.194));
}
}
}