【问题标题】:How to shake on android 2.2如何在 android 2.2 上摇一摇
【发布时间】:2010-11-06 07:49:49
【问题描述】:

有人知道android 2.2 的摇码是什么吗?我想在我的应用程序上引起轰动。谢谢。

【问题讨论】:

    标签: android shake


    【解决方案1】:

    试试这个:

      /* put this into your activity class */
      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();
      }
    

    Source

    【讨论】:

    • 嗨,马克.. 它对我真的很有用.. 但我在这里遇到了一个小问题.. 从这里我可以找到摇晃开始的时间,但我试图找到摇晃停止的时间..如果可以请帮助我..我想在哪里检查 mAccel 值以进行 noshaking...谢谢
    • @MarkByers 你能解释一下 onSensorChanged 构造函数中的数学原理吗?非常感谢,只是对发生的事情很感兴趣
    【解决方案2】:

    这是我的抖动手势检测代码:

    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    
    
    /**
     * Listener that detects shake gesture.
     */
    public class ShakeEventListener implements SensorEventListener {
    
    
      /** Minimum movement force to consider. */
      private static final int MIN_FORCE = 10;
    
      /**
       * Minimum times in a shake gesture that the direction of movement needs to
       * change.
       */
      private static final int MIN_DIRECTION_CHANGE = 3;
    
      /** Maximum pause between movements. */
      private static final int MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE = 200;
    
      /** Maximum allowed time for shake gesture. */
      private static final int MAX_TOTAL_DURATION_OF_SHAKE = 400;
    
      /** Time when the gesture started. */
      private long mFirstDirectionChangeTime = 0;
    
      /** Time when the last movement started. */
      private long mLastDirectionChangeTime;
    
      /** How many movements are considered so far. */
      private int mDirectionChangeCount = 0;
    
      /** The last x position. */
      private float lastX = 0;
    
      /** The last y position. */
      private float lastY = 0;
    
      /** The last z position. */
      private float lastZ = 0;
    
      /** OnShakeListener that is called when shake is detected. */
      private OnShakeListener mShakeListener;
    
      /**
       * Interface for shake gesture.
       */
      public interface OnShakeListener {
    
        /**
         * Called when shake gesture is detected.
         */
        void onShake();
      }
    
      public void setOnShakeListener(OnShakeListener listener) {
        mShakeListener = listener;
      }
    
      @Override
      public void onSensorChanged(SensorEvent se) {
        // get sensor data
        float x = se.values[SensorManager.DATA_X];
        float y = se.values[SensorManager.DATA_Y];
        float z = se.values[SensorManager.DATA_Z];
    
        // calculate movement
        float totalMovement = Math.abs(x + y + z - lastX - lastY - lastZ);
    
        if (totalMovement > MIN_FORCE) {
    
          // get time
          long now = System.currentTimeMillis();
    
          // store first movement time
          if (mFirstDirectionChangeTime == 0) {
            mFirstDirectionChangeTime = now;
            mLastDirectionChangeTime = now;
          }
    
          // check if the last movement was not long ago
          long lastChangeWasAgo = now - mLastDirectionChangeTime;
          if (lastChangeWasAgo < MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE) {
    
            // store movement data
            mLastDirectionChangeTime = now;
            mDirectionChangeCount++;
    
            // store last sensor data 
            lastX = x;
            lastY = y;
            lastZ = z;
    
            // check how many movements are so far
            if (mDirectionChangeCount >= MIN_DIRECTION_CHANGE) {
    
              // check total duration
              long totalDuration = now - mFirstDirectionChangeTime;
              if (totalDuration < MAX_TOTAL_DURATION_OF_SHAKE) {
                mShakeListener.onShake();
                resetShakeParameters();
              }
            }
    
          } else {
            resetShakeParameters();
          }
        }
      }
    
      /**
       * Resets the shake parameters to their default values.
       */
      private void resetShakeParameters() {
        mFirstDirectionChangeTime = 0;
        mDirectionChangeCount = 0;
        mLastDirectionChangeTime = 0;
        lastX = 0;
        lastY = 0;
        lastZ = 0;
      }
    
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
      }
    
    }
    

    将此添加到您的活动中:

      private SensorManager mSensorManager;
    
      private ShakeEventListener mSensorListener;
    

    ...

    在 onCreate() 中添加:

    mSensorListener = new ShakeEventListener();
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);
    
    
        mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
    
          public void onShake() {
            Toast.makeText(Activity.this, "Shake!", Toast.LENGTH_SHORT).show();
          }
        });
    

    和:

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

    【讨论】:

    • 什么是 KPBActivityImpl?它的定义在哪里。
    • 它是使用检测的活动的名称。我将其重命名为“活动”。
    • 感谢您的回答.. 我只是对如何检测震动停止时感到困惑.. 请您提出一些想法
    【解决方案3】:

    它被称为“振动”,因为所讨论的类是振动器。

    http://developer.android.com/reference/android/os/Vibrator.html

    【讨论】:

      【解决方案4】:

      如果你想检测人摇手机,你需要实现SensorEventListener接口并寻找加速度计事件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多