我是android开发的新手.我想获得手机的实际加速度.我找到了获得加速度的代码,但是它提供了重力加速度.请任何人帮助我找到一种在没有重力的情况下获得实际加速度的方法.
这是我找到的代码,请使用此代码帮助我.谢谢
package com.SensorTest; import androID.app.Activity; import androID.harDWare.Sensor; import androID.harDWare.SensorEvent; import androID.harDWare.SensorEventListener; import androID.harDWare.SensorManager; import androID.os.Bundle; import androID.Widget.TextVIEw; public class SensorTestActivity extends Activity implements SensorEventListener { SensorManager sensorManager = null; //for accelerometer values TextVIEw outputX; TextVIEw outputY; TextVIEw outputZ; //for orIEntation values TextVIEw outputX2; TextVIEw outputY2; TextVIEw outputZ2; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); setContentVIEw(R.layout.main); //just some textvIEws, for data output outputX = (TextVIEw) findVIEwByID(R.ID.TextVIEw01); outputY = (TextVIEw) findVIEwByID(R.ID.TextVIEw02); outputZ = (TextVIEw) findVIEwByID(R.ID.TextVIEw03); outputX2 = (TextVIEw) findVIEwByID(R.ID.TextVIEw04); outputY2 = (TextVIEw) findVIEwByID(R.ID.TextVIEw05); outputZ2 = (TextVIEw) findVIEwByID(R.ID.TextVIEw06); } @OverrIDe public voID onAccuracyChanged(Sensor arg0, int arg1) { // Todo auto-generated method stub } @OverrIDe public voID onSensorChanged(SensorEvent event) {synchronized (this) { switch (event.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER: outputX.setText("acclaration x:"+float.toString(event.values[0])); outputY.setText("acclaration y:"+float.toString(event.values[1])); outputZ.setText("acclaration z:"+float.toString(event.values[2])); break; case Sensor.TYPE_ORIENTATION: outputX2.setText("orIEntation x:"+float.toString(event.values[0])); outputY2.setText("orIEntation y:"+float.toString(event.values[1])); outputZ2.setText("orIEntation z:"+float.toString(event.values[2])); break; } } } @OverrIDe protected voID onResume() { super.onResume(); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sensorManager.SENSOR_DELAY_GAME); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), sensorManager.SENSOR_DELAY_GAME); } }
解决方法:
没有重力就不可能直接获得加速度.
您可以在Sensor.TYPE_ACCELEROMETER部分中使用高通滤波器,例如在Android Reference Page上:
public voID onSensorChanged(SensorEvent event) { // Alpha is calculated as t / (t + dT) // with t, the low-pass filter's time-constant // and dT, the event delivery rate final float Alpha = 0.8; gravity[0] = Alpha * gravity[0] + (1 - Alpha) * event.values[0]; gravity[1] = Alpha * gravity[1] + (1 - Alpha) * event.values[1]; gravity[2] = Alpha * gravity[2] + (1 - Alpha) * event.values[2]; linear_acceleration[0] = event.values[0] - gravity[0]; linear_acceleration[1] = event.values[1] - gravity[1]; linear_acceleration[2] = event.values[2] - gravity[2]; }
总结 以上是内存溢出为你收集整理的android-在没有重力的情况下获得加速度全部内容,希望文章能够帮你解决android-在没有重力的情况下获得加速度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)