1. MagneticActivity.java
import android.app.Activity;
import android.content.Context;
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 MagneticActivity extends Activity implements SensorEventListener {
// private SensorManager sensorManager;
SensorManager sensorManager;
private MySurfaceView view;
private float max;
private Object[] magnetic = { "X-axis Maganetic Field",
"Y-axis Maganetic Field", "Z-axis Maganetic Field", };
// MagneticFieldDemo 主程式
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) this
.getSystemService(Context.SENSOR_SERVICE);
view = new MySurfaceView(this);
setContentView(view);
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
view.onValueChanged(event.values);
}
class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
public MySurfaceView(Context context) {
super(context);
getHolder().addCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
onValueChanged(new float[3]);
}
public void surfaceCreated(SurfaceHolder holder) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
void onValueChanged(float[] values) {
Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setTextSize(24);
canvas.drawColor(Color.WHITE);
for (int i = 0; i < values.length; i++) {
canvas.drawText(magnetic[i] + ": " + values[i], 0,
paint.getTextSize() * (i + 1), paint);
}
canvas.drawText("max: " + max, 0, paint.getTextSize() * 5,
paint);
getHolder().unlockCanvasAndPost(canvas);
}
}
}
}
沒有留言:
張貼留言