Android识别手势

发布于 2024-11-27 02:34:43 字数 1146 浏览 0 评论 0原文

我导入了手势示例并创建了自己的应用程序。布局中有一个按钮和gestureoverlayview。该按钮启动 GestureBuilderActivity.class,我可以在其中添加或删除手势(这是示例)。在按钮下,在 GestureOverlayView 中我可以绘制手势。布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Click to see gestures"
    android:id="@+id/Button01"
    />
    <android.gesture.GestureOverlayView
    android:id="@+id/gestures"
    android:layout_width="fill_parent" 
    android:layout_height="0dip"
    android:layout_weight="1.0" />
</LinearLayout>

从示例中我知道这是我找到手势的地方:

 final String path = new File(Environment.getExternalStorageDirectory(),
                    "gestures").getAbsolutePath();

并且 toast 消息显示(仍在示例中)手势保存在 /mnt/sdcard/gestures 中:

Toast.makeText(this, getString(R.string.save_success, path), Toast.LENGTH_LONG).show();

如何使应用程序识别我的手势在吐司消息中绘制并显示它的名字?

I imported the gesture example and created my own app. There is a button and the gestureoverlayview in the layout. The button starts the GestureBuilderActivity.class, where I can add or remove gestures (this is the example). Under the button, in the GestureOverlayView I can draw gestures. Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Click to see gestures"
    android:id="@+id/Button01"
    />
    <android.gesture.GestureOverlayView
    android:id="@+id/gestures"
    android:layout_width="fill_parent" 
    android:layout_height="0dip"
    android:layout_weight="1.0" />
</LinearLayout>

From the example I know that this is where I find the gestures:

 final String path = new File(Environment.getExternalStorageDirectory(),
                    "gestures").getAbsolutePath();

and the toast msg shows (still in the example) that the gesture is saved in /mnt/sdcard/gestures:

Toast.makeText(this, getString(R.string.save_success, path), Toast.LENGTH_LONG).show();

How can I make the app recognize the gesture I draw and show me its name in a toast msg?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏佲洛 2024-12-04 02:34:43

首先,我会阅读这篇解释如何使用手势的短文

要识别手势,您需要向 GestureOverlayView 提供 OnGesturePerformedListener。在此示例中,我只是让 Activity 实现 OnGesturePerformedListener。

public class MyActivity extends Activity implements OnGesturePerformedListener {

private GestureLibrary mGestures;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // I use resources but you may want to use GestureLibraries.fromFile(path)
    mGestures = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!mGestures.load()) {
        showDialog(DIALOG_LOAD_FAIL);
        finish();
    }

    // register the OnGesturePerformedListener (i.e. this activity)
    GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures);
    gesturesView.addOnGesturePerformedListener(this);
}

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = mGestures.recognize(gesture);

        // Determine if a gesture was performed.  
        // This can be tricky, here is a simple approach.
    Prediction prediction = (predictions.size() != 0) ? predictions.get(0) : null;
    prediction = (prediction.score >= 1.0) ? prediction: null;

    if (prediction != null) {
           // do something with the prediction
           Toast.makeText(this, prediction.name + "(" + prediction.score + ")", Toast.LENGTH_LONG).show();
    }
}

First, I would read this short article that explains how to use gestures.

To recognize a gesture you need to provide an OnGesturePerformedListener to GestureOverlayView. In this example, I just had the Activity implement the OnGesturePerformedListener.

public class MyActivity extends Activity implements OnGesturePerformedListener {

private GestureLibrary mGestures;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // I use resources but you may want to use GestureLibraries.fromFile(path)
    mGestures = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!mGestures.load()) {
        showDialog(DIALOG_LOAD_FAIL);
        finish();
    }

    // register the OnGesturePerformedListener (i.e. this activity)
    GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures);
    gesturesView.addOnGesturePerformedListener(this);
}

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = mGestures.recognize(gesture);

        // Determine if a gesture was performed.  
        // This can be tricky, here is a simple approach.
    Prediction prediction = (predictions.size() != 0) ? predictions.get(0) : null;
    prediction = (prediction.score >= 1.0) ? prediction: null;

    if (prediction != null) {
           // do something with the prediction
           Toast.makeText(this, prediction.name + "(" + prediction.score + ")", Toast.LENGTH_LONG).show();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文