Android - Event after detecting 'x' number of rolls around local Z axis

帅比萌擦擦* 提交于 2019-12-12 03:22:43

问题


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


来源:https://stackoverflow.com/questions/32907233/android-event-after-detecting-x-number-of-rolls-around-local-z-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!