gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:20:48 26 4
gpt4 key购买 nike

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

对于手势创建,我有如下:

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) {
}
}
}

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

公共(public)类 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();
}
}
}
}

这是我负责读取手势的代码。

最佳答案

您的代码看起来不错。恐怕 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);

确保在这两个 Activity 中都使用它。

另外,看看如何 sms templates with gestures适用于 CyanogenMod 短信应用。

关于android - 在手势库中存储和检索手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17150578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com