在手势库中存储和检索手势

时间:2013-06-17 14:55:05

标签: android gesture gesture-recognition

我正在写一个密码保护类型的东西,基本上应用程序会提示你创建一个手势,将它存储在某个地方(这是我遇到问题的地方)然后检索它并将它与你输入的内容相匹配“密码”。

对于手势创建,我所拥有的内容如下:

public class GestureUnlock extends Activity {
private static final float LENGTH_THRESHOLD = 120.0f;
private Gesture mGesture;
private View mConfirmButton;
private final File mStoreFile = new File(Environment.getExternalStorageDirectory(), "gestures");

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gesture_unlock);

    mConfirmButton = findViewById(R.id.confirmButton);

    GestureOverlayView overlay = (GestureOverlayView)findViewById(R.id.gestureOverlayView);
    overlay.addOnGestureListener(new GesturesProcessor());
}

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (mGesture != null) {
        outState.putParcelable("gesture", mGesture);
    }
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    mGesture = savedInstanceState.getParcelable("gesture");
    if (mGesture != null) {
        final GestureOverlayView overlay =
                (GestureOverlayView) findViewById(R.id.gestureOverlayView);
        overlay.post(new Runnable() {
            public void run() {
                overlay.setGesture(mGesture);
            }
        });
        mConfirmButton.setEnabled(true);
    }
}

public void confirm(View v) {
    if (mGesture != null) {

        GestureLibrary store = GestureLibraries.fromFile(mStoreFile);
        store.addGesture("Gesture Password", mGesture);
        store.save();

        setResult(RESULT_OK);

        Intent intent = new Intent(this, GestureConfirm.class);
        startActivity(intent);

    } else {
        setResult(RESULT_CANCELED);
    }
}

public void clear(View v) {
    GestureOverlayView overlay = (GestureOverlayView)findViewById(R.id.gestureOverlayView);
    overlay.cancelClearAnimation();
    overlay.clear(true);
}

private class GesturesProcessor implements GestureOverlayView.OnGestureListener {
    public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
        mConfirmButton.setEnabled(false);
        mGesture = null;
    }
    public void onGesture(GestureOverlayView overlay, MotionEvent event) {
    }

    public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
        mGesture = overlay.getGesture();
        if (mGesture.getLength() < LENGTH_THRESHOLD) {
            overlay.clear(false);
        }
        mConfirmButton.setEnabled(true);
    }
    public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
    }
}
}

这是第一个代码,负责首先创建手势的代码。注意事项:布局是手势叠加,两个按钮,一个重置手势,另一个与确认方法相关联。

公共类GestureConfirm扩展了Activity实现OnGesturePerformedListener {

private final File mStoreFile = new File(Environment.getExternalStorageDirectory(), "gestures");
private GestureLibrary store = GestureLibraries.fromFile(mStoreFile);

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gesture_confirm);

    GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestureOverlayViewConfirm);
    gesturesView.addOnGesturePerformedListener(this);
    store.load();

}

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = store.recognize(gesture);
    for (Prediction prediction : predictions) {
        if (prediction.score > 1.0) {
            Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
        }
    }
}
}

以下是我负责阅读手势的代码。

1 个答案:

答案 0 :(得分:1)

您的代码看起来很好。我担心save()需要一些时间,而当您在新Activity中加载它时,它还没有保存。 您可以使用单例类来解决:

import java.io.File;

import android.content.Context;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;

public class PasswordGesturesLibrary {

    private static GestureLibrary sStore;

    public static GestureLibrary getStore(Context c) {

        if (sStore == null) {
            File storeFile = new File(c.getFilesDir(), "gestures");
            sStore = GestureLibraries.fromFile(storeFile);
            sStore.load();
        }

        return sStore;
    }

}

当你想要参考那个只是致电:

GestureLibrary store = PasswordGesturesLibrary.getStore(this);

确保在两项活动中都使用它。

另外,请看看sms templates with gestures在CyanogenMod短信应用中的运作方式。