Mr

 Mr. Rogers' IB Computer Science -- 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.

Mr Rogers Android Project Menu

Home

Projects

2010-2011


   3D Compass

   3D Accelerometer

   Friction Tester

   Drum

   Spherical Game

   Level

 


Greenville, SC
Bubble Level
Description
This Android application is used for testing how level an object is, mimicking the appearance of an actual bubble level. This application is useful for avoiding carrying an actual bubble level around, and having a multi-purpose device for testing how level an object is and having other tools all-in-one.
This is a screen capture of the Android application in action. The green oval represents what would be the bubble in an actual bubble level. When in use, the green bubble will appear to "float" away from the center, and as the object the device is laying on is rotated, the green bubble moves closer or further from the center.
This is another screen capture of the program. This time, the device has been rotated the opposite direction of the first screen capture.




Features
  • Visually aids in making a surface level.
  • Mimics the appearance of an actual bubble level.
Version
1.0 - 25 October 2010

Future Plans
The level needs to be more sensitive, adjust accordingly.
Make the level more aesthetically pleasing.

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


CODE

Source Code
Bubble Class
package tessier.jean.com;

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

public class Bubble {
        private int x, y, radius;

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

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

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

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

        public void setR(int newR)
        {
                radius = 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);
        }
        /**
	 * Draws the level to the screen. Then draws the bubble on top.
	 */
        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.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 SensorListener
{
        private double highAccel, totalAccel;
        private SensorManagerSimulator mSensorManager;
        private static Context CONTEXT;
        private Board b;

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        b=new Board(this);
        setContentView(b);
        CONTEXT = this;
        highAccel = 0;
        totalAccel = 0;
        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));

        }

        @Override
        public void onSensorChanged(int sensor, float[] values) {
                if(sensor == SensorManager.SENSOR_ORIENTATION)
                {
                        b.getBubble().setY(215+(int)(values[1]*1.194));
                }
        }
}

Biography

Jean Tessier is a senior at Southside High School. He enjoys reading
about psychology and learning new programming languages.