如何在“摇动”操作期间禁用方向传感器

时间:2012-08-25 17:18:52

标签: android android-sensors shake

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

我尝试使用布尔变量accelerometer阻止Orientation传感器,但它无效。有什么建议吗?

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; 
        }
    }
}

0 个答案:

没有答案