如何在Android应用程序中实现手势支持?

发布于 2024-10-26 13:24:04 字数 3287 浏览 2 评论 0原文

尝试在我的应用程序中实现左右手势。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout 
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingRight="10sp">
        <TextView android:id="@+id/movie_title" 
                  android:layout_width="fill_parent" 
                  android:layout_height="wrap_content"
                  android:gravity="center_horizontal"
                  android:text="text"
                  android:paddingTop="8sp"
                  android:paddingBottom="16sp"
                  android:textSize="16sp"
                  android:textColor="#FFFFFF" 
                  android:textStyle="bold"/>
        <ImageView android:id="@+id/movie_poster" 
                   android:layout_width="fill_parent" 
                   android:layout_height="wrap_content"
                   android:gravity="center_horizontal"/>
        <TextView android:text="" 
                  android:id="@+id/movie_description" 
                  android:layout_height="wrap_content"
                  android:textSize="16sp"
                  android:paddingTop="16sp"
                  android:paddingBottom="16sp"
                  android:layout_width="fill_parent"/>

        <Button android:id="@+id/confirm" 
            android:text="@string/confirm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    <android.gesture.GestureOverlayView
        android:id="@+id/movie_gestures"
        android:layout_width="fill_parent" 
        android:layout_height="0dip"
        android:layout_weight="1.0" />          
    </LinearLayout>
</ScrollView>

正确吗?应用程序不对手势做出反应。 正在根据原始 tutorial 编写代码:

public class MovieView extends Activity implements OnGesturePerformedListener {
private GestureLibrary mLibrary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
                ...
        // gestures 
        mLibrary = GestureLibraries.fromRawResource(this, R.raw.spells);
        if (!mLibrary.load()) {
            finish();
        }

        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.movie_gestures);
        gestures.addOnGesturePerformedListener(this);
    }

    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
        if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
            String action = predictions.get(0).name;
            Toast.makeText(this, predictions.get(0).name, Toast.LENGTH_SHORT).show();

            if ("left".equals(action)) {
                    Log.i("", "left");
                }
            else if  ("right".equals(action)) {
                    Log.i("", "right");
                }

            }
        }

但 onGesturePerformed 从未被调用。

Try to implement left and right gestures in my application.

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout 
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingRight="10sp">
        <TextView android:id="@+id/movie_title" 
                  android:layout_width="fill_parent" 
                  android:layout_height="wrap_content"
                  android:gravity="center_horizontal"
                  android:text="text"
                  android:paddingTop="8sp"
                  android:paddingBottom="16sp"
                  android:textSize="16sp"
                  android:textColor="#FFFFFF" 
                  android:textStyle="bold"/>
        <ImageView android:id="@+id/movie_poster" 
                   android:layout_width="fill_parent" 
                   android:layout_height="wrap_content"
                   android:gravity="center_horizontal"/>
        <TextView android:text="" 
                  android:id="@+id/movie_description" 
                  android:layout_height="wrap_content"
                  android:textSize="16sp"
                  android:paddingTop="16sp"
                  android:paddingBottom="16sp"
                  android:layout_width="fill_parent"/>

        <Button android:id="@+id/confirm" 
            android:text="@string/confirm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    <android.gesture.GestureOverlayView
        android:id="@+id/movie_gestures"
        android:layout_width="fill_parent" 
        android:layout_height="0dip"
        android:layout_weight="1.0" />          
    </LinearLayout>
</ScrollView>

Is it correct? Application doesn't react on gestures.
Was writing my code based on original tutorial:

public class MovieView extends Activity implements OnGesturePerformedListener {
private GestureLibrary mLibrary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
                ...
        // gestures 
        mLibrary = GestureLibraries.fromRawResource(this, R.raw.spells);
        if (!mLibrary.load()) {
            finish();
        }

        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.movie_gestures);
        gestures.addOnGesturePerformedListener(this);
    }

    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
        if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
            String action = predictions.get(0).name;
            Toast.makeText(this, predictions.get(0).name, Toast.LENGTH_SHORT).show();

            if ("left".equals(action)) {
                    Log.i("", "left");
                }
            else if  ("right".equals(action)) {
                    Log.i("", "right");
                }

            }
        }

but onGesturePerformed is never called.

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

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

发布评论

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

评论(1

等往事风中吹 2024-11-02 13:24:04

我改用了 onFling 。与 ScrollView 配合良好。

I've used onFling instead. Works well with ScrollView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文