Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding GestureOverlayView to my SurfaceView class, how to add to view hierarchy?

I was informed in a later answer that I have to add the GestureOverlayView I create in code to my view hierarchy, and I am not 100% how to do that. Below is the original question for completeness.

I want my game to be able to recognize gestures. I have this nice SurfaceView class that I do an onDraw to draw my sprites, and I have a thread thats running it to call the onDraw etc .

This all works great.

I am trying to add the GestureOverlayView to this and it just isn't working. Finally hacked to where it doesn't crash but this is what i have

public class Panel extends SurfaceView implements SurfaceHolder.Callback,         OnGesturePerformedListener  
{ 
public Panel(Context context)
{
    theContext=context;
    mLibrary = GestureLibraries.fromRawResource(context, R.raw.myspells);
    GestureOverlayView gestures = new GestureOverlayView(theContext);       
    gestures.setOrientation(gestures.ORIENTATION_VERTICAL);
    gestures.setEventsInterceptionEnabled(true);
    gestures.setGestureStrokeType(gestures.GESTURE_STROKE_TYPE_MULTIPLE);

    gestures.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));

    //GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(this);
 }
 ...
 ...
 onDraw...
surfaceCreated(..);
...
...


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

  // We want at least one prediction
  if (predictions.size() > 0) {
   Prediction prediction = predictions.get(0);
   // We want at least some confidence in the result
   if (prediction.score > 1.0) {
    // Show the spell    
     Toast.makeText(theContext, prediction.name, Toast.LENGTH_SHORT).show();
   }
  }
 }


}

The onGesturePerformed is never called. Their example has the GestureOverlay in the xml, I am not using that, my activity is simple:

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    Panel p = new Panel(this);
    setContentView(p);
}

So I am at a bit of a loss of the missing piece of information here, it doesn't call the onGesturePerformed and the nice pretty yellow "you are drawing a gesture" never shows up.

like image 920
Codejoy Avatar asked Dec 28 '22 09:12

Codejoy


1 Answers

This was a major pain but I finally figured out the solution. There is absolutely no good documentation on this anywhere online. I finally got it work work by using something called a "FrameLayout" and then adding both the surfaceview and the gesture view there. Once you do that, you set the FrameLayout as your content view.

Unfortunately, the above solution offered by RomanGuy didn't seem to work (or possibly, I just couldn't get it to work) for some reason. There is no .addView() function available for the SurfaceView class like there is for a regular view. That function exists for ViewGroups and will work fine for adding gestures on anything other than a surfaceview I think.

Your activity must implement the OnGesturePerformedListener interface for this to work.

You can see the full tutorial and source code on my website here: http://scanplaygames.com/?p=193

   package view.stack;

import game.core.GameView;

import java.util.ArrayList;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.Toast;

public class GestureActivity extends Activity implements OnGesturePerformedListener 
{
    protected GameView surfaceView;
    protected GestureOverlayView gestureOverlayView;
    protected GestureLibrary mLibrary;
    protected FrameLayout frameLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        gestureOverlayView = new GestureOverlayView(this); 
        surfaceView        = new GameView(this);            
        frameLayout        = new FrameLayout(this);

        //gestureOverlayView.addView(surfaceView);    
        gestureOverlayView.setOrientation(gestureOverlayView.ORIENTATION_VERTICAL);
        gestureOverlayView.setEventsInterceptionEnabled(true);
        gestureOverlayView.setGestureStrokeType(gestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);

        mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        gestureOverlayView.addOnGesturePerformedListener(this);

        frameLayout.addView(surfaceView, 0);
        frameLayout.addView(gestureOverlayView,1);

        setContentView(frameLayout);
    }

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) 
    {
        // TODO Auto-generated method stub
        ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

        // one prediction needed
        if (predictions.size() > 0)
        {
            Prediction prediction = predictions.get(0);

            // checking prediction
            if (prediction.score > 1.0) 
            {
                // and action
                Toast.makeText(GestureActivity.this, prediction.name,
                        Toast.LENGTH_SHORT).show();
            }
        }
    }    
}
like image 122
scanplaygames Avatar answered May 10 '23 22:05

scanplaygames