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

南笙酒味 提交于 2019-11-30 05:31:05
scanplaygames

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();
            }
        }
    }    
}

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

alf

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

Here is working answer for this question,

please also see on example source code.

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

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