标签:
一、简介
二、代码
1.java
(1)MainActivity.java
1 import android.app.Activity; 2 import android.content.Context; 3 import android.hardware.Sensor; 4 import android.hardware.SensorEvent; 5 import android.hardware.SensorEventListener; 6 import android.hardware.SensorManager; 7 import android.os.Bundle; 8 9 public class MainActivity extends Activity { 10 /** Called when the activity is first created. */ 11 12 private SensorManager sensorManager; 13 private Sensor accelerometerSensor; 14 private float gravity [] = new float[3]; 15 private float linear_acceleration [] = new float[3]; 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 22 accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 23 sensorManager.registerListener(new SensorEventListener() { 24 25 public void onSensorChanged(SensorEvent event) { 26 27 /*System.out.println("x---" + event.values[0]); 28 System.out.println("y---" + event.values[1]); 29 System.out.println("z---" + event.values[2]);*/ 30 final float alpha = 0.8f; 31 32 //用滤波器过滤掉杂质 33 gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0]; 34 gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1]; 35 gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2]; 36 37 linear_acceleration[0] = event.values[0] - gravity[0]; 38 linear_acceleration[1] = event.values[1] - gravity[1]; 39 linear_acceleration[2] = event.values[2] - gravity[2]; 40 41 System.out.println(linear_acceleration[0]); 42 System.out.println(linear_acceleration[1]); 43 System.out.println(linear_acceleration[2]); 44 // System.out.println(gravity[0]); 45 // System.out.println(gravity[1]); 46 // System.out.println(gravity[2]); 47 } 48 49 public void onAccuracyChanged(Sensor sensor, int accuracy) { 50 51 } 52 }, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL); 53 } 54 }
ANDROID_MARS学习笔记_S05_004_过滤杂质,得到真正的加速度
标签:
原文地址:http://www.cnblogs.com/shamgod/p/5207549.html