This is the accelerometer program that we developed in class on 4/29. It starts a red ball in the center of the screen, and then moves it according to the X,Y acceleration of the phone. So with the phone flat on its back, there is no acceleration in X,Y (just gravity on Z). But then when you tilt the X,Y toward the Earth, the ball rolls in that direction.
package com.example.sensor;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import java.util.List;
public class MyActivity extends Activity implements SensorEventListener {
TextView helloText, xText, yText, zText;
MyGameView gameView;
SensorManager manager;
Sensor accel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helloText = (TextView) findViewById(R.id.helloText);
xText = (TextView) findViewById(R.id.xText);
yText = (TextView) findViewById(R.id.yText);
zText = (TextView) findViewById(R.id.zText);
gameView = (MyGameView) findViewById(R.id.gameView);
manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// List<Sensor> sensors = manager.getSensorList(Sensor.TYPE_ALL);
// for(Sensor s : sensors) {
// helloText.append("\n" + s.getName() + ": " + s.toString());
// }
accel = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// manager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
manager.unregisterListener(this);
}
@Override
protected void onResume() {
super.onResume();
manager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
xText.setText("X " + event.values[0]);
yText.setText("Y " + event.values[1]);
zText.setText("Z " + event.values[2]);
gameView.adjustBallPosition(-event.values[0], event.values[1]);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
package com.example.sensor;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;
public class MyGameView extends View {
Point current = null;
public MyGameView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void adjustBallPosition(float x, float y) {
if(current != null) {
current.x += x;
current.y += y;
invalidate();
}
}
@Override
protected void onDraw(Canvas canvas) {
if(current == null) {
current = new Point();
current.x = canvas.getWidth() / 2;
current.y = canvas.getHeight() / 2;
}
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.RED);
canvas.drawCircle(current.x, current.y, 15, p);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
android:id="@+id/helloText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
android:id="@+id/xText" android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y"
android:id="@+id/yText" android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Z"
android:id="@+id/zText" android:layout_gravity="center"/>
<view android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.example.sensor.MyGameView" android:id="@+id/gameView"/>
</LinearLayout>