如何检测Android上的摆动动作?

时间:2015-10-14 15:41:30

标签: android android-sensors gyroscope

我需要一种可以在Android上检测“摆动”手势的算法,我的意思是手机以纵向模式保持,手腕左右扭曲,就像打开圆形门把手的动作一样。所以我需要围绕z轴来回捕捉旋转一段时间。

(原始)图片说明我需要捕捉的动作:

Rotation around the z axis

我已经尝试了许多捕捉震动的不同算法,例如在Github上看到的{Shooogle For Yer Photos'解决方案here,它运行得很好,但是使用加速度计在每个轴上捕获抖动。如何将这样的算法限制为仅检测Z轴上的旋转,“摇摆”至少4秒?

以上是上面发布的“Shoogle For Yer Photos”代码的摘录,其中显示了它们如何处理震动检测:

private static final long KEEP_DATA_POINTS_FOR = 1500;
private static final long MINIMUM_EACH_DIRECTION = 7;
private static final float POSITIVE_COUNTER_THRESHHOLD = (float) 2.0;
private static final float NEGATIVE_COUNTER_THRESHHOLD = (float) -2.0;

public void checkForShake() {
    long curTime = System.currentTimeMillis();
    long cutOffTime = curTime - KEEP_DATA_POINTS_FOR;
    while(dataPoints.size() > 0 && dataPoints.get(0).atTimeMilliseconds < cutOffTime) dataPoints.remove(0);

    int x_pos =0, x_neg=0, x_dir = 0, y_pos=0, y_neg=0, y_dir=0, z_pos=0, z_neg = 0, z_dir = 0;
    for(DataPoint dp: dataPoints){
        if (dp.x > POSITIVE_COUNTER_THRESHHOLD && x_dir < 1) {
            ++x_pos;
            x_dir = 1;
        }
        if (dp.x < NEGATIVE_COUNTER_THRESHHOLD && x_dir > -1) {
            ++x_neg;
            x_dir = -1;
        }
        if (dp.y > POSITIVE_COUNTER_THRESHHOLD && y_dir < 1) {
            ++y_pos;
            y_dir = 1;
        }
        if (dp.y < NEGATIVE_COUNTER_THRESHHOLD && y_dir > -1) {
            ++y_neg;
            y_dir = -1;
        }
        if (dp.z > POSITIVE_COUNTER_THRESHHOLD && z_dir < 1) {
            ++z_pos;
            z_dir = 1;
        }
        if (dp.z < NEGATIVE_COUNTER_THRESHHOLD && z_dir > -1) {
            ++z_neg;
            z_dir = -1;
        }
    }

    if ((x_pos >= MINIMUM_EACH_DIRECTION && x_neg >= MINIMUM_EACH_DIRECTION) ||
            (y_pos >= MINIMUM_EACH_DIRECTION && y_neg >= MINIMUM_EACH_DIRECTION) ||
            (z_pos >= MINIMUM_EACH_DIRECTION && z_neg >= MINIMUM_EACH_DIRECTION) ) {
        lastShake = System.currentTimeMillis();
        last_x = 0; last_y=0; last_z=0;
        dataPoints.clear();
        triggerShakeDetected();
        return;
    }
}

非常感谢任何帮助,谢谢。

0 个答案:

没有答案