1. BallMoveActivity.java
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.SurfaceView;
public class BallMoveActivity extends Activity implements SensorEventListener {
private BallDrawableView ballDrawableView;
private SensorManager sensorManager;
private int screenX, screenY;
private int xPosition, yPosition;
private String[] accelerometer = { "X-axis Accelerometer",
"Y-axis Accelerometer", "Z-axis Accelerometer" };
private float[] values;
private boolean visible;
private boolean setScreen;
private boolean setSensorXY;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ballDrawableView = new BallDrawableView(this);
this.setContentView(ballDrawableView);
}
@Override
protected void onResume() {
super.onResume();
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onDestroy() {
super.onDestroy();
sensorManager.unregisterListener(this);
}
public void onSensorChanged(SensorEvent sensorEvent) {
visible = true;
values = sensorEvent.values;
if (setSensorXY) {
updateBall(sensorEvent.values);
}
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
private void updateBall(float[] sensor) {
float x = sensor[0] * 10;
float y = sensor[1] * 10;
if (x <= 0) {
xPosition += Math.abs(x);
if (xPosition >= screenX) {
xPosition = screenX;
}
} else if (x >= 0) {
xPosition -= Math.abs(x);
if (xPosition <= 0) {
xPosition = 0;
}
}
if (y <= 0) {
yPosition -= Math.abs(y);
if (yPosition <= 0) {
yPosition = 0;
}
} else if (y >= 0) {
yPosition += Math.abs(y);
if (yPosition >= screenY) {
yPosition = screenY;
}
}
}
public class BallDrawableView extends SurfaceView {
private Bitmap bitmap;
private Context context;
public BallDrawableView(Context context) {
super(context);
this.context = context;
bitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.ball));
setScreen = true;
this.setBackgroundColor(Color.BLACK);
}
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.YELLOW);
paint.setTextSize(25);
canvas.drawBitmap(bitmap, xPosition, yPosition, null);
if (visible) {
for (int i = 0; i < values.length; i++) {
canvas.drawText(accelerometer[i] + " : " + values[i], 0,
paint.getTextSize() * (i + 1), paint);
}
}
if (setScreen) {
setScreen_XY(canvas);
setScreen = false;
setSensorXY = true;
}
invalidate();
}
public void setScreen_XY(Canvas canvas) {
screenX = canvas.getWidth()
- BitmapFactory.decodeResource(this.getResources(),
R.drawable.ball).getWidth();
screenY = canvas.getHeight()
- BitmapFactory.decodeResource(this.getResources(),
R.drawable.ball).getHeight();
}
}
}
2. 球體圖片請自備
沒有留言:
張貼留言