Android multi stroke gesture not working

旧时模样 提交于 2019-12-22 00:02:46

问题


I modified an existing app,GestureBuilder,to accept multiple strokes.Hence to accept the letter A,D, F and so on. I created a simple app which recognises if the letter is correct or not.My problem is:

1.I have successfully recorded multi stroke letter in a file called gesture with gestureBuilder. 2.My application does not accept multi stroke gesture.But i have set fadeoffset to 1000 and gesturestroketype as multiple.I do not know why this is happening.The moment I enter next xtroke to write capital A my previous stroke dissapears.I cant write multi stroke letters/symbols.

Code:

xml file:

    <LinearLayout 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=".MainActivity"

     >

        <android.gesture.GestureOverlayView
        android:id="@+id/gestures_overlay"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0"
        android:fadeOffset="5000"
        android:eventsInterceptionEnabled="true"

        android:gestureStrokeType="multiple" />



        </LinearLayout>

java file:

package com.example.gesture_letter;

         import java.util.ArrayList;

          import android.os.Bundle;
         import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity implements  OnGesturePerformedListener {
    GestureLibrary mLibrary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GestureOverlayView gestureoverlayview=new GestureOverlayView(this);

        View inflate=getLayoutInflater().inflate(R.layout.activity_main,null);
        gestureoverlayview.setGestureColor(Color.rgb(0, 255, 0));
        gestureoverlayview.setGestureVisible(true);
        gestureoverlayview.setUncertainGestureColor(Color.rgb(0,0,255));

        gestureoverlayview.addView(inflate);
        gestureoverlayview.addOnGesturePerformedListener(this);
        mLibrary=GestureLibraries.fromRawResource(this,R.raw.gestures);
        if(!mLibrary.load())
        {
            Log.i("debug","FAILED");

        }
        else
        {
            Log.i("debug","LOADED");
        }

        Log.i("debug","OnCreate");
        setContentView(gestureoverlayview);


    }

    @Override
    public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) {
        // TODO Auto-generated method stub

        ArrayList<Prediction> predictions=mLibrary.recognize(arg1);
        if(predictions.size()>0)
        {
            Prediction prediction=predictions.get(0);
            if(prediction.score>1.0)
            {
                String s=prediction.name;


                Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
            }
            else
                if(prediction.score<1.0)
                {
                    Toast.makeText(getApplicationContext(), "None",Toast.LENGTH_LONG).show();
                }
                }

        }
    }

Thank you.

I also would like a good resource for gesture application building.thank you


回答1:


I do not know why, android:GestureStrokeType="multiple" did not work. It struck me to try it from the java side. I set :

gestureoverlayview.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
gestureoverlayview is an object of GestureOverlayView(you can name it anything)

(in main_activity.java file for newbies) Then you'll see it works.

I think its a bug, I'm not sure.I spent weeks hovering on this :(




回答2:


The problem is that you create a new GestureOverlayView :

GestureOverlayView gestureoverlayview=new GestureOverlayView(this);

You should call the view defined in the xml with findViewById(R.id.gesture_overlay);



来源:https://stackoverflow.com/questions/18165847/android-multi-stroke-gesture-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!