ANDROID_MARS学习笔记_S05_004_过滤杂质,得到真正的加速度

一、简介

二、代码
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 }
时间: 2024-10-18 01:23:34

ANDROID_MARS学习笔记_S05_004_过滤杂质,得到真正的加速度的相关文章

ANDROID_MARS学习笔记_S03_007_GoogleMap1

一.简介 二.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fi

ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求

一.代码 1.xml(1)activity_main.xml 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="

ANDROID_MARS学习笔记_S02_012_ANIMATION_利用AnimationListener在动画结束时删除或添加组件

一.代码 1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/layoutId" 4 android:orientation="

ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_012_广播机制一

一.简介 二.代码1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width

ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一.运行结果 一.代码1.xml(1)activity_main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_heig

ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S02_004_ExpandableListActivity

1.main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"

ANDROID_MARS学习笔记_S02_005_AppWidget1

一.AppWidget介绍 1.Widget的定义创建流程 (1)在res新建xml文件夹,新建appwidget-provider.xml,为widget提供元数据 (2)在res/layout文件夹下定义widget的样式 (3)在src新建类,extends AppWidgetProvider,自定义widget (4)在AndroidManifest.xml中把自定义的widget添加为receiver,接收APPWIDGET_UPDATE广播 2. 二.代码 1.res/xml/exa