Android-加速下降(粉碎) [英] Android - Acceleration down (smash)

查看:102
本文介绍了Android-加速下降(粉碎)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相同的问题,但可以更好地解释

Same question but better explained

这个问题被认为是正确的,但是根本没有,我用旧设备中兴通讯尝试了一下,并且大部分时间都可以正常工作,但是现在我有了Samsung Galazy A5 2016,它不起作用,而且都没有LG G3.

This questions was accepted as a correct but it is not at all, I tried it with my old device ZTE and it worked most of time, but now I have a Samsung Galazy A5 2016 and it doesn't work, neither on a LG G3.

事情正在尝试使用Accelerometer,有些Sensors我必须能够检测到我在视频上制作的这两个动作中的任何一个.

The thing is trying using Accelerometer and some Sensors I have to be able to detect any of those two moviments that I made on the Video.

有两个动作:

  • 以一点速度砸碎它
  • 自由落体

我让您决定并说服我什么是更好的选择,而更容易则是更好的选择,我的意思是说它可以在大多数设备上使用.

I let you to decide and convince me what's the better option and EASIER one to do, with better I mean that works on most of devices.

推荐答案

固定设备的重力值+9.81,对应于设备的加速度(0 m/s2减去重力,即-9.81 m/s2).因此,如果设备从静止状态向下移动,则重力将小于9.81.自由落体装置的重力等于0.

A stationary device will have an gravity value of +9.81, which corresponds to the acceleration of the device (0 m/s2 minus the force of gravity, which is -9.81 m/s2). Thus if the device is moving downward from stationary then the gravity will be less than 9.81. A free fall device will have gravity equals 0.

下面是如何确定设备是否开始向下移动.如果设备已经以恒定速度向下移动,则将无法确定设备是否向下移动,因为在这种情况下,没有向下的加速度,并且重力范数应该在9.81附近.

Below is how to determine if the device starts moving downward. It will not be able to determine whether the device is moving downward if the device is already moving downward with constant speed, since in this case there is no acceleration downward and the gravity norm should be around 9.81.

您需要使用TYPE_GRAVITY.如果设备没有TYPE_GRAVITY,则使用低通滤波器TYPE_ACCELEROMETER来获取重力矢量.

You need to use TYPE_GRAVITY. If the device does not have TYPE_GRAVITY, then low pass filter TYPE_ACCELEROMETER to get the gravity vector.

如上所述,固定设备的重力矢量的范数等于9.81.但是,此值将随设备而略有不同.因此,您首先需要确定该静态重力范数.您可以通过注册TYPE_GRAVITY或TYPE_ACCELEROMETER并要求用户将设备放平,然后按一个按钮来执行此操作.按下按钮后,应用程序将在onSensorChanged中计算重力的范数.

As above a stationary device will have a gravity vector with norm equal 9.81. However, this value will vary slightly with devices. Thus you need first to determine this stationary gravity norm. You can do this by register for TYPE_GRAVITY or TYPE_ACCELEROMETER and ask the user to lay the device flat and then press a button. Once the button is pressed the app will calculate the norm of the gravity in onSensorChanged.

private float mStationaryGravityNorm;
private float mDeviation = 0.01;
private float mCount;
private boolean mIsCalculatingStationGravityNorm = true;

Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener( {
    @Override
    public void onClick(View v) {
        // register sensor
    }
    });

@Override
public void onSensorChanged(SensorEvent event) {
// Will average out 100 gravity values.
if (mIsCalculatingStationGravityNorm) {
    if (mCount++ < 100) {
        mStationaryGravityNorm += Math.sqrt(event.values[0] * event.values[0] + event.values[1] * event.values[1] + event.values[2] * event.values[2]);
    } else {
        mStationaryGravityNorm /= 100;
        mIsCalculatingStationGravityNorm = false;
} else {
    float gravityNorm = Math.sqrt(event.values[0] * event.values[0] + event.values[1] * event.values[1] + event.values[2] * event.values[2]);
    if (gravityNorm < mStationaryGravityNorm - mDeviation) {
    // moving down
    }
}

PS 对于向下移动,您确实要计算重力.当设备静止时,重力范数约为9.81(取决于设备).现在,如果设备向下移动,则会有向下的加速度,因此重力规范将小于9.81,如果设备向上移动,则重力规范将大于9.81.因此,通过将重力规范与此固定重力规范进行比较,您将知道设备是向上移动还是向下移动.这与设备方向无关. TYPE_GRAVITY将提供更好的精度,但是如果设备没有此类型,则低通滤波器TYPE_ACCELERATOR将为您提供重力矢量.

PS For moving up an down you do want to calculate gravity. When the device is stationary, the gravity norm is approximately 9.81 (depending on device). Now if the device is moving down, there is an acceleration downward, thus the gravity norm will be less than 9.81 and if the device is moving up the gravity norm will be more than 9.81. So by comparing the gravity norm against this stationary gravity norm, you will know if the device moving up or down. This is independent of the device orientation. TYPE_GRAVITY will give better accuracy but if the device does not have this type then low pass filter TYPE_ACCELERATOR will give you the gravity vector.

这篇关于Android-加速下降(粉碎)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆