先看一下运行效果
运行效果如上
本demo是以图形方式显示加速度传感器的值的应用程序
Sensor(传感器): *告诉我:传感器是接收信号或刺激并反应的器件,能将待测物理量或化学量转换成另一对应输出的装置。
比如:重力传感器,方向传感器
Android对每个设备的传感器都进行了抽象,SensorManger类用来控制传感器,Sensor用来描述具体的传感器,SensorEventListener用来监听传感器值的改变。
第二步中可以获得如下传感器,
传感器类型列表:
Sensor.TYPE_ACCELEROMETER:
加速计传感器
Sensor.TYPE_GYROSCOPE:
回转仪传感器
Sensor.TYPE_LIGHT:
光传感器,动态控制屏幕亮度
Sensor.TYPE_MAGNETIC_FIELD:
磁场传感器
Sensor.TYPE_ORIENTATION:
方向传感器
Sensor.TYPE_PRESSURE:
压力传感器
Sensor.TYPE_PROXIMIY:
邻近距离传感器
Sensor.TYPE_TEMPERATURE:
温度传感器
有些手机不支持部分感应装置你可以判断sensor是否为null,为null时你可以提示用户。
你也可以使用下面的语句获得设备可用的的传感器列表:
List<Sensor> allSensors = sensorManger.getSensorList(Sensor.TYPE_ALL);
在第三步中,
三个参数分别为:SensorEventListener、Sensor、传感器的更新速率
前两个参数都是之前声明的。
第三个参数,有四个选择。
传感器更新速率:
SensorManager.SENSOR_DELAY_FASTEST:
指定可能最快的传感器更新速率
SensorManager.SENSOR_DELAY_GAME:
指定适合在游戏中使用的更新速率
SensorManager.SENSOR_DELAY_NORMAL:
指定默认的更新速率
SensorManager.SENSOR_DELAY_UI:
指定适合于更新UI的更新速率
第四步一般是在onResume()中进行。
第五步一般在onPause()中进行。
这样Activity是和用户交互时才使用它们。
public class Sensors extends Activity { private SensorManager mSensorManager;
private GraphView mGraphView;
private class GraphView extends View implements SensorEventListener
{
private Bitmap mBitmap;
private Paint mPaint = new Paint();
private Canvas mCanvas = new Canvas();
private Path mPath = new Path();
private RectF mRect = new RectF();
private float mLastValues[] = new float[3*2];
private float mOrientationValues[] = new float[3];
private int mColors[] = new int[3*2];
private float mLastX;
private float mScale[] = new float[2];
private float mYOffset;
private float mMaxX;
private float mSpeed = 1.0f;
private float mWidth;
private float mHeight;
public GraphView(Context context) {
super(context);
mColors[0] = Color.argb(192, 255, 64, 64);
mColors[1] = Color.argb(192, 64, 128, 64);
mColors[2] = Color.argb(192, 64, 64, 255);
mColors[3] = Color.argb(192, 64, 255, 255);
mColors[4] = Color.argb(192, 128, 64, 128);
mColors[5] = Color.argb(192, 255, 255, 64);
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mRect.set(-0.5f, -0.5f, 0.5f, 0.5f);
mPath.arcTo(mRect, 0, 180);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
mCanvas.setBitmap(mBitmap);
mCanvas.drawColor(0xFFFFFFFF);
mYOffset = h * 0.5f;
mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
mWidth = w;
mHeight = h;
if (mWidth < mHeight) {
mMaxX = w;
} else {
mMaxX = w-50;
}
mLastX = mMaxX;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
synchronized (this) {
if (mBitmap != null) {
final Paint paint = mPaint;
final Path path = mPath;
final int outer = 0xFFC0C0C0;
final int inner = 0xFFff7010;
if (mLastX >= mMaxX) {
mLastX = 0;
final Canvas cavas = mCanvas;
final float yoffset = mYOffset;
final float maxx = mMaxX;
final float oneG = SensorManager.STANDARD_GRAVITY * mScale[0];
paint.setColor(0xFFAAAAAA);
cavas.drawColor(0xFFFFFFFF);
cavas.drawLine(0, yoffset, maxx, yoffset, paint);
cavas.drawLine(0, yoffset+oneG, maxx, yoffset+oneG, paint);
cavas.drawLine(0, yoffset-oneG, maxx, yoffset-oneG, paint);
}
canvas.drawBitmap(mBitmap, 0, 0, null);
float[] values = mOrientationValues;
if (mWidth < mHeight) {
float w0 = mWidth * 0.333333f;
float w = w0 - 32;
float x = w0*0.5f;
for (int i=0 ; i<3 ; i++) {
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.translate(x, w*0.5f + 4.0f);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
paint.setColor(outer);
canvas.scale(w, w);
canvas.drawOval(mRect, paint);
canvas.restore();
canvas.scale(w-5, w-5);
paint.setColor(inner);
canvas.rotate(-values[i]);
canvas.drawPath(path, paint);
canvas.restore();
x += w0;
}
} else {
float h0 = mHeight * 0.333333f;
float h = h0 - 32;
float y = h0*0.5f;
for (int i=0 ; i<3 ; i++) {
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.translate(mWidth - (h*0.5f + 4.0f), y);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
paint.setColor(outer);
canvas.scale(h, h);
canvas.drawOval(mRect, paint);
canvas.restore();
canvas.scale(h-5, h-5);
paint.setColor(inner);
canvas.rotate(-values[i]);
canvas.drawPath(path, paint);
canvas.restore();
y += h0;
}
}
}
}
}
//第三步:继承SensorEventListener监听传感器的值改变并且做出相应的动作 //传感器的值改变调用此方法
public void onSensorChanged(SensorEvent event) {
//Log.d(TAG, "sensor: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
synchronized (this) {
if (mBitmap != null) {
final Canvas canvas = mCanvas;
final Paint paint = mPaint;
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
for (int i=0 ; i<3 ; i++) {
mOrientationValues[i] = event.values[i];
}
} else {
float deltaX = mSpeed;
float newX = mLastX + deltaX;
int j = (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) ? 1 : 0;
for (int i=0 ; i<3 ; i++) {
int k = i+j*3;
final float v = mYOffset + event.values[i] * mScale[j];
paint.setColor(mColors[k]);
canvas.drawLine(mLastX, mLastValues[k], newX, v, paint);
mLastValues[k] = v;
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mLastX += mSpeed;
}
invalidate();
}
}
}
//传感器的精确度改变调用此方法
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
/**
* Initialization of the Activity after it is first created. Must at least
* call {@link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
//第一步:获得SensorManager对象,返回的就是一个硬件设备的控制器
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mGraphView = new GraphView(this);
setContentView(mGraphView);
}
//第二步:获得特定的传感器 //第四步:注册传感器事件监听事件
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mGraphView,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mGraphView,
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mGraphView,
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_FASTEST);
}
//第五步:注销传感器事件的监听
@Override
protected void onStop() {
mSensorManager.unregisterListener(mGraphView);
super.onStop();
}
}