gpt4 book ai didi

java - 使用 Android 加速度计确定恒定速度

转载 作者:行者123 更新时间:2023-12-02 04:22:07 25 4
gpt4 key购买 nike

我正在尝试编写一个小应用程序,它可以根据手机的速度更新屏幕。它使用加速度计计算当前速度并将其写在屏幕上。问题是移动手机后速度不会回到零速度。它将稳定在速度的最高点。我正在使用 LINEAR_ACCELEROMETER

这是代码:

public class AccelerometerUpSensor extends SensorAbstract{
private ExerciseFragment fragment;
private double v0 = 0;
private float lastX;
private float lastY;
private float lastZ;
private long interval;
private long lastEvent = System.currentTimeMillis();
public AccelerometerUpSensor(SensorManager sensorManager, ExerciseFragment fragment, int[] sensorTypes){
super(sensorManager,sensorTypes);
this.fragment = fragment;
}
@Override
public final void onSensorChanged(SensorEvent event) {
lastX = event.values[0];
lastY = event.values[1];
lastZ = event.values[2];
long now = System.currentTimeMillis();
interval = (now - lastEvent);
lastEvent = now;
double acceleration = lastX+lastY+lastZ;
double velocity = v0 + (acceleration*(interval/(double)1000));
v0 = velocity;
System.out.println(velocity);
}

最佳答案

  1. com.google.android.gms ActivityRecognition,您可以在其中跟踪不同类型的移动。

  2. 加速度计

            mGravity = event.values.clone();

    float x = mGravity[0];
    float y = mGravity[1];
    float z = mGravity[2];
    mAccelLast = mAccelCurrent;
    mAccelCurrent =(float) Math.sqrt(x * x + y * y + z * z);
    float delta = mAccelCurrent - mAccelLast;
    mAccel = mAccel * 0.9f + delta;


    // Make this higher or lower according to how much motion you want to detect
    if (Math.abs(mAccel) > 2) {

关于java - 使用 Android 加速度计确定恒定速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32652477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com