Android应用开发提高篇(3)-----传感器(Sensor)编程

链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/29/2373420.html

一、概述

Android支持的传感器种类越来越多了,这确实是一件可喜的事情,两年前在单片机上玩过加速度传感器模块,当时做的是一个摔倒检测的项目,利用相关的算法对三轴数据进行采集、处理、判断来实现检测,既然是摔倒检测,那肯定是一个便携式设备吧,没错,当时做出来的设备是小到可以放进口袋里的。庆幸的是,今天这些东西完全可以在手机上完成了,而且不用涉及到硬件设计的问题,不过今天不是讲如何去移植那个项目,而是讲讲Android上的传感器编程。Android,再一次让我看到你的强大!再一次坚定了我学习你的信心!

二、要求

了解、熟悉传感器的编程方法。

三、实现

新建工程MySensor,修改/res/layout/main.xml文件,在里面添加一个SurfaceView,完整的main.xml文件如下:

Android应用开发提高篇(3)-----传感器(Sensor)编程
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <SurfaceView
8 android:id="@+id/surfaceview"
9 android:layout_width="fill_parent"
10 android:layout_height="180dip"
11 />
12
13
14 </LinearLayout>
Android应用开发提高篇(3)-----传感器(Sensor)编程

接着,修改MySensorActivity.java文件,代码中有详细注释,完整的MySensorActivity.java文件如下:

Android应用开发提高篇(3)-----传感器(Sensor)编程
  1 package com.nan.sensor;
2
3 import android.app.Activity;
4 import android.graphics.Canvas;
5 import android.graphics.Color;
6 import android.graphics.Paint;
7 import android.graphics.Rect;
8 import android.hardware.Sensor;
9 import android.hardware.SensorEvent;
10 import android.hardware.SensorEventListener;
11 import android.hardware.SensorManager;
12 import android.os.Bundle;
13 import android.view.SurfaceHolder;
14 import android.view.SurfaceView;
15
16 public class MySensorActivity extends Activity
17 {
18 private float acceValusX = 0f;
19 private float acceValusY = 0f;
20 private float acceValusZ = 0f;
21 private int x = 0;
22
23 private SurfaceView mSurfaceView = null;
24 private SurfaceHolder mSurfaceHolder = null;
25 private Paint mPaint = null;
26
27 private SensorManager mSensorManager;
28 private Sensor mAccelerometer;
29 private OnSensorEventListener mOnSensorEventListener = new OnSensorEventListener();
30
31 /** Called when the activity is first created. */
32 @Override
33 public void onCreate(Bundle savedInstanceState)
34
35 {
36 super.onCreate(savedInstanceState);
37 setContentView(R.layout.main);
38
39 mSurfaceView = (SurfaceView)findViewById(R.id.surfaceview);
40 mSurfaceHolder = mSurfaceView.getHolder();
41 mSurfaceHolder.addCallback(new MyHolder());
42
43 mPaint = new Paint();
44 //画笔的粗细
45 mPaint.setStrokeWidth(1.0f);
46 //从系统服务中获得传感器管服务
47 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
48 mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
49 //注册传感器监听
50 mSensorManager.registerListener(mOnSensorEventListener, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
51 }
52
53
54 private class OnSensorEventListener implements SensorEventListener
55 {
56
57 @Override
58 public void onAccuracyChanged(Sensor sensor, int accuracy)
59 {
60 // TODO Auto-generated method stub
61
62 }
63
64 @Override
65 public void onSensorChanged(SensorEvent event)
66 {
67 // TODO Auto-generated method stub
68
69 //获得x轴的值
70 acceValusX = event.values[0];
71 //获得y轴的值
72 acceValusY = event.values[1];
73 //获得z轴的值
74 acceValusZ = event.values[2];
75
76 //锁定整个SurfaceView
77 Canvas mCanvas = mSurfaceHolder.lockCanvas();
78 try
79 {
80 if(mCanvas!=null)
81 {
82 //画笔的颜色(红)
83 mPaint.setColor(Color.RED);
84 //画X轴的点
85 mCanvas.drawPoint(x, (int)(30 + acceValusX), mPaint);
86 //画笔的颜色(绿)
87 mPaint.setColor(Color.GREEN);
88 //画Y轴的点
89 mCanvas.drawPoint(x, (int)(60 + acceValusY), mPaint);
90 //画笔的颜色(蓝)
91 mPaint.setColor(Color.BLUE);
92 //画Z轴的点
93 mCanvas.drawPoint(x, (int)(90 + acceValusZ), mPaint);
94 //横坐标+1
95 x++;
96 //如果已经画到了屏幕的最右边
97 if(x > getWindowManager().getDefaultDisplay().getWidth())
98 {
99 x = 0;
100 //清屏
101 mCanvas.drawColor(Color.BLACK);
102 }
103 //绘制完成,提交修改
104 mSurfaceHolder.unlockCanvasAndPost(mCanvas);
105 }
106 }
107 catch(Exception e)
108 {
109 e.printStackTrace();
110 }
111 finally
112 {
113 if(mCanvas!=null)
114 {
115 //重新锁一次
116 mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));
117 mSurfaceHolder.unlockCanvasAndPost(mCanvas);
118 }
119 }
120
121 }
122
123 }
124
125
126 //定义一个类,实现Callback接口
127 public class MyHolder implements SurfaceHolder.Callback
128 {
129
130 public void surfaceChanged(SurfaceHolder holder, int format, int width,
131 int height)
132 {
133 // TODO Auto-generated method stub
134 //add your code
135 }
136
137 public void surfaceCreated(SurfaceHolder holder)
138 {
139 // TODO Auto-generated method stub
140 //add your code
141 }
142
143 public void surfaceDestroyed(SurfaceHolder holder)
144 {
145 // TODO Auto-generated method stub
146 //add your code
147 }
148
149 }
150
151 @Override
152 public void onDestroy()
153 {
154 super.onDestroy();
155 //注销传感器监听
156 mSensorManager.unregisterListener(mOnSensorEventListener, mAccelerometer);
157 }
158
159 }
Android应用开发提高篇(3)-----传感器(Sensor)编程

好了,在真机上运行它,效果如下:

Android应用开发提高篇(3)-----传感器(Sensor)编程

晃动手机几下,效果如下:

Android应用开发提高篇(3)-----传感器(Sensor)编程

完成。

上一篇:mysql 必知必会总结


下一篇:Android应用开发提高篇(5)-----Camera使用