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

后端 未结 4 1724
醉梦人生
醉梦人生 2020-12-29 16:52

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 qu

相关标签:
4条回答
  • 2020-12-29 17:11

    Here is working answer for this question,

    please also see on example source code.

    http://scanplaygames.com/?p=168

    0 讨论(0)
  • 2020-12-29 17:14

    You could simply add your view to to gestureOverlayView:

    gestureOverlayView.addView(new yourview (this) , 600, 800);
    

    then

    this.setContentView(gestureOverlayView);
    

    gestureOverlayView is already a framelayout

    0 讨论(0)
  • 2020-12-29 17:21

    You create the gestures overlay but you never add it to the view hierarchy. It won't work if it doesn't exist :)

    0 讨论(0)
  • 2020-12-29 17:22

    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();
                }
            }
        }    
    }
    
    0 讨论(0)
提交回复
热议问题