【问题标题】:"shake to do something" code explanation“摇一摇做某事”代码解释
【发布时间】:2012-06-12 10:54:06
【问题描述】:

我找到了这段代码,它的功能是当设备足够强大时做一些事情,但我还没有完全理解它。有人请帮帮我

public class ShakeActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        mAccel = 0.00f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;
    }
      private SensorManager mSensorManager;
      private float mAccel; // acceleration apart from gravity
      private float mAccelCurrent; // current acceleration including gravity
      private float mAccelLast; // last acceleration including gravity

      private final SensorEventListener mSensorListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent se) {
          float x = se.values[0];
          float y = se.values[1];
          float z = se.values[2];
          mAccelLast = mAccelCurrent;
          mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
          float delta = mAccelCurrent - mAccelLast;
          mAccel = mAccel * 0.9f + delta; // perform low-cut filter

        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
      };

      @Override
      protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
      }

      @Override
      protected void onStop() {
        mSensorManager.unregisterListener(mSensorListener);
        super.onStop();
      }

}

请帮我理解这两行

mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));//I guess this is for computing the value of the acceleration

这行我不明白

 mAccel = mAccel * 0.9f + delta;

提前致谢。

【问题讨论】:

    标签: android acceleration


    【解决方案1】:

    传感器将返回三个值,用于沿三个轴方向的加速度;这些位于您的代码示例中的xyz。想象三个相互成直角的弹簧上的质量;当您四处移动设备并且弹簧拉伸和收缩时,xyz 包含它们的长度。

    mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));

    这是计算加速度的大小。想象一下,如果你只有一个,而不是弹簧上的三个质量,总是准确地指向设备加速的方向。实际上可以从我们拥有的值中计算出该系统的外观,这就是在这里完成:mAccelCurrent 这样的弹簧会被拉伸多少。 This 是正在执行的计算。

    mAccel = mAccel * 0.9f + delta;

    这是输入上的high pass filter。在这里,它具有使加速度的突然变化给出更大值的效果。仅从您发布的代码尚不清楚为什么要这样做;我猜想是让其他地方的代码最终检查mAccel 对摇晃设备时每次摇晃的极端力量更加敏感。

    【讨论】:

    • 是的,您对 mAccel 的看法是正确的。我想知道的还有一件事是为什么我们必须乘以“0.9f”以及“0.9f”是什么意思(我的意思是字母f)。并感谢您的精彩回答
    • 字母 f 告诉编译器您打算将该值设为单精度浮点数(例如,参见 cplusplus.com/doc/tutorial/constants )。乘法是高通滤波器的一部分,请参阅链接文章;该值控制输出信号在 delta 峰值后衰减的速率。
    猜你喜欢
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    相关资源
    最近更新 更多