首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android java开发加速到

Android java开发加速到
EN

Stack Overflow用户
提问于 2011-09-28 14:30:32
回答 1查看 317关注 0票数 0

如何计算加速到100公里/小时的时间?当!location.hasSpeed()为真时,我注册了一个位置侦听器,将位置时间存储到一个变量中。当速度达到给定速度时,在这种情况下,100 of /h (27.77 m/s) i减去位置的speed,结果i除以1000。

这是“伪代码”

代码语言:javascript
运行
复制
    @Override
    public void onLocationChanged(Location currentLoc) {

        // when stop reseted, when start reset again
        if (isAccelerationLoggingStarted) {
            if (currentLoc.hasSpeed() && currentLoc.getSpeed() > 0.0) {
                // dismiss the time between reset to start to move  
                startAccelerationToTime = (double) currentLoc.getTime();
            }
        }

        if (!currentLoc.hasSpeed()) {
            isAccelerationLoggingStarted = true;
            startAccelerationToTime = (double) currentLoc.getTime();
            acceleration100 = 0.0;
        }

        if (isAccelerationLoggingStarted) {
            if (currentLoc.getSpeed() >= 27.77) {
                acceleration100 = (currentLoc.getTime() - startAccelerationToTime) / 1000;
                isAccelerationLoggingStarted = false;
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-28 16:29:07

我在这里看到的主要问题是,只要设备在移动,startAccelerationToTime就会重置。(第一个if只检查是否有移动;它不检查是否已经记录了开始时间。

我根本看不出哪里需要isAccelerationLoggingStarted --速度和变量本身可以稍微清理一下,以明确下一步应该是什么。

你的伪代码可能应该看起来像这样:

代码语言:javascript
运行
复制
if speed is 0
    clear start time
else if no start time yet
    start time = current time
    clear acceleration time
else if no acceleration time yet, and if speed >= 100 mph 
    acceleration time = current time - start time

在Java中,这看起来像...

代码语言:javascript
运行
复制
long startTime = 0;
double accelerationTime = 0.0;

@Override
public void onLocationChanged(Location currentLoc) {

    // when stopped (or so slow we might as well be), reset start time
    if (!currentLoc.hasSpeed() || currentLoc.getSpeed() < 0.005) {
        startTime = 0;
    }

    // We're moving, but is there a start time yet?
    // if not, set it and clear the acceleration time
    else if (startTime == 0) {
        startTime = currentLoc.getTime();
        accelerationTime = 0.0;
    }

    // There's a start time, but are we going over 100 km/h?
    // if so, and we don't have an acceleration time yet, set it
    else if (accelerationTime == 0.0 && currentLoc.getSpeed() >= 27.77) {
        accelerationTime = (double)(currentLoc.getTime() - startTime) / 1000.0;
    }
}

现在,我不确定位置监听器是如何工作的,或者当你移动时它们通知你的频率有多高。因此,这可能只会起到半个作用。特别是,当您不移动时,onLocationChanged可能不会被调用;您可能需要请求更新(可能通过“重置”按钮或其他方式)或设置某些参数,以便触发速度为== 0时发生的事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7579165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档