1

我正在开发一个使用俯仰/滚动值的应用程序,并且我还想实现“摇动”功能。使用下面的代码,我为每一个想要的动作干杯,但是,当我摇晃手机时,它有时会将此动作捕捉为“摇晃”短滚/俯仰。我想将“摇晃”与俯仰/滚动动作分开。应用程序应该在 Android 2.2 上运行,因此这也是对可用传感器的限制。

我试图用布尔变量阻止方向传感器accelerometer,但它不起作用。有什么建议么?

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    synchronized (this) {
        switch (event.sensor.getType()){

        case Sensor.TYPE_ACCELEROMETER:
            float[] values = event.values;
            float x = values[0];
            float y = values[1];
            float z = values[2];

            accelerometer = true;
            mAccelLast = mAccelCurrent;
            mAccelCurrent = (float) FloatMath.sqrt((float) (x*x + y*y + z*z));
            float delta = mAccelCurrent - mAccelLast;
            mAccel = mAccel * 0.9f + delta; 

            if (mAccel>2.5){
                Toast.makeText(this, "SHAKE!!!!", Toast.LENGTH_SHORT).show();
            } else {
                accelerometer=false;
            }


        break;

        case Sensor.TYPE_ORIENTATION:
            if (!accelerometer){
            pitch = event.values[1];
            roll = event.values[2];

            if (pitch > Constants.PITCH_ANGLE && pitchUpAction == false && roll < Constants.BASE_ANGLE && roll > - Constants.BASE_ANGLE) {
                oriStartTime = System.currentTimeMillis();
                pitchUpAction = true;
            } else if (pitch < - Constants.PITCH_ANGLE && pitchDownAction == false && roll < Constants.BASE_ANGLE && roll > - Constants.BASE_ANGLE){
                oriStartTime = System.currentTimeMillis();
                pitchDownAction = true;
            } else if (roll > Constants.ROLL_ANGLE && rollLeftAction == false && pitch < Constants.BASE_ANGLE && pitch > - Constants.BASE_ANGLE){
                oriStartTime = System.currentTimeMillis();
                rollLeftAction = true;
            } else if (roll < - Constants.ROLL_ANGLE && rollRightAction == false && pitch < Constants.BASE_ANGLE && pitch > - Constants.BASE_ANGLE){
                oriStartTime = System.currentTimeMillis();
                rollRightAction = true;
            }

            if ((pitchUpAction || pitchDownAction) && pitch < Constants.BASE_ANGLE && pitch > -Constants.BASE_ANGLE){
                oriElapsedTime = System.currentTimeMillis() - oriStartTime; 

                if(oriElapsedTime>(2*1000)) {
                    if(pitchUpAction){
                        Toast.makeText(getBaseContext(), "Pitch up 2s", Toast.LENGTH_SHORT).show();
                    } else if(pitchDownAction){
                        Toast.makeText(getBaseContext(), "Pitch down 2s", Toast.LENGTH_SHORT).show();
                    } 
                } else {
                    if(pitchUpAction){
                        Toast.makeText(getBaseContext(), "Pitch up short", Toast.LENGTH_SHORT).show();
                    } else if(pitchDownAction){
                        Toast.makeText(getBaseContext(), "Pitch down short", Toast.LENGTH_SHORT).show();
                    } 
                }
                pitchUpAction = pitchDownAction = false;
            } else if ((rollLeftAction || rollRightAction) && roll < Constants.BASE_ANGLE && roll > -Constants.BASE_ANGLE){
                oriElapsedTime = System.currentTimeMillis() - oriStartTime; 

                if(oriElapsedTime>(2*1000)) {
                    if(rollLeftAction){
                        Toast.makeText(getBaseContext(), "Roll Left 2s", Toast.LENGTH_SHORT).show();
                    } else if(rollRightAction){
                        Toast.makeText(getBaseContext(), "Roll Right 2s", Toast.LENGTH_SHORT).show();
                    } 
                } else {
                    if(rollLeftAction){
                        Toast.makeText(getBaseContext(), "Roll left short", Toast.LENGTH_SHORT).show();
                    } else if(rollRightAction){
                        Toast.makeText(getBaseContext(), "Roll right short", Toast.LENGTH_SHORT).show();
                    } 
                }
                rollLeftAction = rollRightAction = false;
            }
            }


        break; 
        }
    }
}
4

0 回答 0