android 加速度 重力,android-在没有重力的情况下获得加速度

我是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];

}

标签:gravity,acceleration,android

来源: https://codeday.me/bug/20191201/2082339.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值