【问题标题】:Android SDK - Using Gesture API, would like gesture to stay on screen after being recognizedAndroid SDK - 使用 Gesture API,希望手势在被识别后留在屏幕上
【发布时间】:2016-08-05 21:13:38
【问题描述】:

我将 Gesture API 与我创建的手势库一起使用,它运行良好。问题是我希望在 OnGesturePerformedListener 退出后手势在屏幕上可见,但手势被删除。我在想也许在 OnGesturePerformedListener 之后有一个事件——我可以将手势保存在 OnGesturePerformedListener 中,然后在以后的事件中再次显示它。有谁知道有没有这样的活动?这是代码:

   private OnGesturePerformedListener handleGestureListener = new OnGesturePerformedListener() {
        @Override
        public void onGesturePerformed(GestureOverlayView gestureView,
                                       Gesture gesture) {
            if (gesture.getStrokesCount() != 2){
                setWonderEmoticon();
                return;
            }
            ArrayList<Prediction> predictions = gLib.recognize(gesture);
            // one prediction needed
            if (predictions.size() > 0) {
                Prediction prediction = predictions.get(0);
                // checking prediction
                if (prediction.score > 20.0) {
                    setHappyEmoticon();
                }
                else {
                    setWonderEmoticon();
                }
            }
        }
    };

顺便说一句,当从代码中删除 setWonderEmoticon() 和 setHappyEmoticon() 时,也会发生同样的事情。

【问题讨论】:

    标签: android gesture-recognition


    【解决方案1】:

    这是一个与OnGesturePerformedListener 配合使用的示例:

    MainActivity.java

    // ...
    
    public class MainActivity extends AppCompatActivity implements GestureOverlayView.OnGesturePerformedListener, GestureOverlayView.OnGestureListener {
    
        GestureOverlayView mGestureView;
        ImageView mImageView;
        TextView mText;
        Paint mPaint;
        Bitmap mBitmap;
        Canvas mCanvas;
        GestureLibrary mGestureLib;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mImageView = (ImageView) findViewById(R.id.image_view);
            mGestureView = (GestureOverlayView) findViewById(R.id.gesture_overlay);
            mGestureView.addOnGesturePerformedListener(this);
            mGestureView.addOnGestureListener(this);
    
            mGestureView.setGestureColor(Color.BLACK);
            mGestureView.setUncertainGestureColor(Color.BLACK);
            mGestureView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onGlobalLayout() {
                    mGestureView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    mBitmap = Bitmap.createBitmap(mGestureView.getWidth(), mGestureView.getHeight(), Bitmap.Config.ARGB_8888);
                    mCanvas = new Canvas(mBitmap);
                }
            });
    
            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(12.0f);
            mPaint.setColor(Color.GRAY);
    
            mText = (TextView) findViewById(R.id.text);
    
            mGestureLib = GestureLibraries.fromRawResource(getApplicationContext(), R.raw.gesture);
            mGestureLib.load();
        }
    
        @Override
        public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {
            if (gesture.getStrokesCount() > 0) {
                ArrayList<Prediction> predictions = mGestureLib.recognize(gesture);
                for (GestureStroke stroke : gesture.getStrokes()) {
                    Path path = stroke.getPath();
                    mCanvas.drawPath(path, mPaint);
                }
                mImageView.setImageBitmap(mBitmap);
                if (!predictions.isEmpty()) {
                    Prediction best = predictions.get(0);
                    mText.setText(String.format("Gesture: %s", best.name));
                }
            }
        }
    
        // ...
    }
    

    activity_main.xml (snapshot):

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="onl.nok.gestures.MainActivity">
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Waiting ..."
            android:textSize="25sp" />
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/image_view" />
    
        <android.gesture.GestureOverlayView
            android:id="@+id/gesture_overlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gestureStrokeType="single"
            android:fadeEnabled="false" />
    </RelativeLayout>
    

    如何应用新手势?

    1. 使用 Android 应用程序跟踪手势Gesture Builder
    2. 在您的项目中创建资源文件夹raw&lt;Project&gt;/app/src/main/res/raw/
    3. 从您的设备复制生成的文件gesture.txt/Android/data/pack.GestureApp/files/gesture.txt
    4. 最后粘贴到创建的文件夹raw&lt;Project&gt;/app/src/main/res/raw/gesture.txt

    最后我把源码推送到了GitHub:https://github.com/nok/android-gesture-libraries


    第二次编辑

    你是对的。所以我使用一个简单的ImageView 来绘制执行的手势。此外,您可以更改mPaint 类型Paint 的样式。您可以在 GitHub 上的推送提交 82eabfb5ce427d 中找到所有更改。

    【讨论】:

    • 这和我的代码完全一样——没有附加价值。手势从屏幕上消失,但我希望它保留在屏幕上
    • 我希望在执行手势时但在它消失之前发生另一个事件。
    • 好的,我编辑了我的答案,也许这些更改可以回答你的问题。
    • 使用这些属性,手势在屏幕上停留的时间更长,但这只是延迟了 onGesturePerformed 的启动。当 onGesturePerformed 结束时,我需要它留在屏幕上。
    • 你是对的。抱歉,我没有注意到这一点。在最后一次编辑中,我使用一个简单的画布来绘制执行的手势。看看吧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多