A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 老衲玩IT 中级黑马   /  2013-8-27 13:03  /  1318 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*各种传感器实例
  2. */
  3. public class Sensors extends Activity {
  4.     private SensorManager mSensorManager;
  5.     private GraphView mGraphView;
  6. /*View的子类实现SensorEventListener接口*/
  7.     private class GraphView extends View implements SensorEventListener
  8.     {
  9. /*画笔,画布,路径,矩形,颜色,接收传感器数据的数组变量*/
  10.         private Bitmap  mBitmap;
  11.         private Paint   mPaint = new Paint();
  12.         private Canvas  mCanvas = new Canvas();
  13.         private Path    mPath = new Path();
  14.         private RectF   mRect = new RectF();
  15.         private float   mLastValues[] = new float[3*2];
  16.         private float   mOrientationValues[] = new float[3];
  17.         private int     mColors[] = new int[3*2];
  18.         private float   mLastX;
  19.         private float   mScale[] = new float[2];
  20.         private float   mYOffset;
  21.         private float   mMaxX;
  22.         private float   mSpeed = 1.0f;
  23.         private float   mWidth;
  24.         private float   mHeight;
  25.         
  26.         public GraphView(Context context) {
  27.             super(context);
  28.             mColors[0] = Color.argb(192, 255, 64, 64);
  29.             mColors[1] = Color.argb(192, 64, 128, 64);
  30.             mColors[2] = Color.argb(192, 64, 64, 255);
  31.             mColors[3] = Color.argb(192, 64, 255, 255);
  32.             mColors[4] = Color.argb(192, 128, 64, 128);
  33.             mColors[5] = Color.argb(192, 255, 255, 64);

  34.             mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
  35.             mRect.set(-0.5f, -0.5f, 0.5f, 0.5f);
  36.             mPath.arcTo(mRect, 0, 180);
  37.         }
  38. //view尺寸改变后的响应      
  39.         @Override
  40.         protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  41.             mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
  42.             mCanvas.setBitmap(mBitmap);
  43.             mCanvas.drawColor(0xFFFFFFFF);
  44.             mYOffset = h * 0.5f;
  45.             mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
  46.             mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
  47.             mWidth = w;
  48.             mHeight = h;
  49.             if (mWidth < mHeight) {
  50.                 mMaxX = w;
  51.             } else {
  52.                 mMaxX = w-50;
  53.             }
  54.             mLastX = mMaxX;
  55.             super.onSizeChanged(w, h, oldw, oldh);
  56.         }
  57. //自动重绘函数
  58.         @Override
  59.         protected void onDraw(Canvas canvas) {
  60.             synchronized (this) {
  61.                 if (mBitmap != null) {
  62.                     final Paint paint = mPaint;
  63.                     final Path path = mPath;
  64.                     final int outer = 0xFFC0C0C0;
  65.                     final int inner = 0xFFff7010;

  66.                     if (mLastX >= mMaxX) {
  67.                         mLastX = 0;
  68.                         final Canvas cavas = mCanvas;
  69.                         final float yoffset = mYOffset;
  70.                         final float maxx = mMaxX;
  71.                         final float oneG = SensorManager.STANDARD_GRAVITY * mScale[0];
  72.                         paint.setColor(0xFFAAAAAA);
  73.                         cavas.drawColor(0xFFFFFFFF);
  74.                         cavas.drawLine(0, yoffset,      maxx, yoffset,      paint);
  75.                         cavas.drawLine(0, yoffset+oneG, maxx, yoffset+oneG, paint);
  76.                         cavas.drawLine(0, yoffset-oneG, maxx, yoffset-oneG, paint);
  77.                     }
  78.                     canvas.drawBitmap(mBitmap, 0, 0, null);

  79.                     float[] values = mOrientationValues;
  80.                     if (mWidth < mHeight) {
  81.                         float w0 = mWidth * 0.333333f;
  82.                         float w  = w0 - 32;
  83.                         float x = w0*0.5f;
  84.                         for (int i=0 ; i<3 ; i++) {
  85.                             canvas.save(Canvas.MATRIX_SAVE_FLAG);
  86.                             canvas.translate(x, w*0.5f + 4.0f);
  87.                             canvas.save(Canvas.MATRIX_SAVE_FLAG);
  88.                             paint.setColor(outer);
  89.                             canvas.scale(w, w);
  90.                             canvas.drawOval(mRect, paint);
  91.                             canvas.restore();
  92.                             canvas.scale(w-5, w-5);
  93.                             paint.setColor(inner);
  94.                             canvas.rotate(-values[i]);
  95.                             canvas.drawPath(path, paint);
  96.                             canvas.restore();
  97.                             x += w0;
  98.                         }
  99.                     } else {
  100.                         float h0 = mHeight * 0.333333f;
  101.                         float h  = h0 - 32;
  102.                         float y = h0*0.5f;
  103.                         for (int i=0 ; i<3 ; i++) {
  104.                             canvas.save(Canvas.MATRIX_SAVE_FLAG);
  105.                             canvas.translate(mWidth - (h*0.5f + 4.0f), y);
  106.                             canvas.save(Canvas.MATRIX_SAVE_FLAG);
  107.                             paint.setColor(outer);
  108.                             canvas.scale(h, h);
  109.                             canvas.drawOval(mRect, paint);
  110.                             canvas.restore();
  111.                             canvas.scale(h-5, h-5);
  112.                             paint.setColor(inner);
  113.                             canvas.rotate(-values[i]);
  114.                             canvas.drawPath(path, paint);
  115.                             canvas.restore();
  116.                             y += h0;
  117.                         }
  118.                     }

  119.                 }
  120.             }
  121.         }
  122. /*传感器数据变化时的响应函数*/
  123.         public void onSensorChanged(SensorEvent event) {
  124.             //Log.d(TAG, "sensor: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
  125.             synchronized (this) {
  126.                 if (mBitmap != null) {
  127.                     final Canvas canvas = mCanvas;
  128.                     final Paint paint = mPaint;
  129. //手机屏幕方向传感
  130.                     if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
  131.                         for (int i=0 ; i<3 ; i++) {
  132.                             mOrientationValues[i] = event.values[i];
  133.                         }
  134.                     } else {
  135.                         float deltaX = mSpeed;
  136.                         float newX = mLastX + deltaX;

  137.                         int j = (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) ? 1 : 0;
  138.                         for (int i=0 ; i<3 ; i++) {
  139.                             int k = i+j*3;
  140.                             final float v = mYOffset + event.values[i] * mScale[j];
  141.                             paint.setColor(mColors[k]);
  142.                             canvas.drawLine(mLastX, mLastValues[k], newX, v, paint);
  143.                             mLastValues[k] = v;
  144.                         }
  145. //地磁场方向传感
  146.                         if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
  147.                             mLastX += mSpeed;
  148.                     }
  149.                     invalidate();
  150.                 }
  151.             }
  152.         }

  153.         public void onAccuracyChanged(Sensor sensor, int accuracy) {
  154.         }
  155.     }
  156.    
  157.     /**
  158.      *初始化程序
  159.      */
  160.     @Override
  161.     protected void onCreate(Bundle savedInstanceState) {
  162.         // Be sure to call the super class.
  163.         super.onCreate(savedInstanceState);

  164.         mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  165.         mGraphView = new GraphView(this);
  166.         setContentView(mGraphView);
  167.     }

  168.     @Override
  169.     protected void onResume() {
  170.         super.onResume();
  171. //注册加速度传感器
  172.         mSensorManager.registerListener(mGraphView,
  173.                 mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  174.                 SensorManager.SENSOR_DELAY_FASTEST);
  175. //注册地磁场传感器
  176.         mSensorManager.registerListener(mGraphView,
  177.                 mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
  178.                 SensorManager.SENSOR_DELAY_FASTEST);
  179. //注册屏幕方向传感器
  180.         mSensorManager.registerListener(mGraphView,
  181.                 mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
  182.                 SensorManager.SENSOR_DELAY_FASTEST);
  183.     }
  184.    
  185.     @Override
  186.     protected void onStop() {
  187.         mSensorManager.unregisterListener(mGraphView);
  188.         super.onStop();
  189.     }
  190. }
复制代码

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马