Android - Event after detecting 'x' number

2019-09-12 02:04发布

问题:

I'm currently struggling with the use of detectors in Android. I'm working on an app, and I'd like it to perform certain operations after the user make their phone 'roll' in their hand.

To be more precise, I'd like them to roll their phone 720° around the local Z Axis (if I'm not mistaken, the 'height' of the phone) and then, have the app return something.

I really don't know how to do this. So far, the best I've came up with was to declare 8 boolean variables accounting each for a quarter of a roll made, set to true when roll values gets within a certain range. Yeah. I'm that desperate.

Any help is appreciated!

回答1:

You could accumulate the change in roll angles, taking into account for when the angle crosses the 2*pi boundary. Notify the user when the total roll angle is above 4*pi or less than -4*pi.

void updateTotalRoll(float currentAngle) {
    if ((currentAngle < (pi/2)) && (prevAngle > (3*pi/2))) {
        totalRoll += (currentAngle+2*pi-prevAngle);
    } else if ((currentAngle > (3*pi/2)) && (prevAngle < (pi/2))) {
        totalRoll -= (prevAngle+2*pi-currentAngle);
    } else {
        totalRoll += currentAngle-prevAngle;
    }
}