Android传感器之陀螺仪传感器

发布时间:2017-2-24 22:58:16 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Android传感器之陀螺仪传感器",主要涉及到Android传感器之陀螺仪传感器方面的内容,对于Android传感器之陀螺仪传感器感兴趣的同学可以参考一下。

代码片段,双击复制 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 package com.example.gyro_sensor; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.widget.TextView; import android.widget.Toast; /** * 陀螺仪传感器 * * @author pengxiaolong * */ public class MainActivity extends Activity {                  SensorManager sensorManager;//管理器对象                  Sensor gyroSensor;//传感器对象                  TextView tv_X;                  TextView tv_Y;                  TextView tv_Z;         //         TextView tv_info;                         @Override         protected void onCreate(Bundle savedInstanceState) {                 super.onCreate(savedInstanceState);                 setContentView(R.layout.main);                 View_init();                 projectinit();         }         @Override         public boolean onCreateOptionsMenu(Menu menu) {                 // Inflate the menu; this adds items to the action bar if it is present.                 getMenuInflater().inflate(R.menu.main, menu);                 return true;         }                         /**          * 对象的初始化          */         private void projectinit(){                                 sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);                 gyroSensor=sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);                 if(gyroSensor==null){                         Toast.makeText(this, "您的设备不支持该功能!", 1).show();                 }else{ //                String str="\n名字:"+gyroSensor.getName()+"\n电池:"+gyroSensor.getPower()+"\n类型:"+gyroSensor.getType()+"\nVendor:"+gyroSensor.getVendor()+"\n版本:"+gyroSensor.getVersion()+"\n幅度:"+gyroSensor.getMaximumRange(); //            //                tv_info.setText(str); //                                        /**                  * 注册监听器                  */                 sensorManager.registerListener(sensoreventlistener, gyroSensor, SensorManager.SENSOR_DELAY_NORMAL);                 }         }                 /**          * 小组件的初始化          */         private void View_init(){                 //                tv_info=(TextView) this.findViewById(R.id.info);                 tv_X=(TextView) this.findViewById(R.id.tvX);                 tv_Y=(TextView) this.findViewById(R.id.tvY);                 tv_Z=(TextView) this.findViewById(R.id.tvZ);                         }                     @Override         protected void onDestroy() {                 // TODO Auto-generated method stub                 super.onDestroy();                 /**                  * 解除注册                  */                 sensorManager.unregisterListener(sensoreventlistener);         }         private Handler handler=new Handler(){                 @Override                 public void handleMessage(Message msg) {                         // TODO Auto-generated method stub                                                 switch(msg.what){                                                 case CHANGE:                                                                 float[] valuse=(float[]) msg.obj;                                                                         tv_X.setText("沿X轴旋转的角度为:"+Float.toString(valuse[0]));                                 tv_Y.setText("沿Y轴旋转的角度为:"+Float.toString(valuse[1]));                                 tv_Z.setText("沿Z轴旋转的角度为:"+Float.toString(valuse[2]));                                                                 break;                         }                         super.handleMessage(msg);                 }                                                         };         /**          * 传感器的监听          */         private SensorEventListener sensoreventlistener=new SensorEventListener() {                                 @Override                 public void onSensorChanged(SensorEvent event) {                         // TODO Auto-generated method stub                         System.out.println("===========ll---???");                         float[] valuse=event.values; //                        for (int i = 0; i < valuse.length; i++) { //                                System.out.println(valuse+"\n"); //                        } //                        Message message=new Message(); //                        message.obj=valuse; //                        message.what=CHANGE; //                        handler.sendMessage(message);                         tv_X.setText("沿X轴旋转的角度为:"+Float.toString(valuse[0]));                         tv_Y.setText("沿Y轴旋转的角度为:"+Float.toString(valuse[1]));                         tv_Z.setText("沿Z轴旋转的角度为:"+Float.toString(valuse[2]));                 }                                 @Override                 public void onAccuracyChanged(Sensor sensor, int accuracy) {                         // TODO Auto-generated method stub                                                                 }         };                 private static final byte CHANGE=0x1; }

上一篇:移植Linux操作系统时候遇到的问题
下一篇:hadoop学习过程-2013.08.29--爬虫larbin安装、配置、使用

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。