将 GestureOverlayView 添加到我的 SurfaceView 类,如何添加到视图层次结构?

发布于 2024-09-12 05:13:25 字数 1971 浏览 3 评论 0原文

在后来的回答中我被告知我必须将我在代码中创建的 GestureOverlayView 添加到我的视图层次结构中,但我并不 100% 知道如何做到这一点。以下是为了完整性而提出的原始问题。

我希望我的游戏能够识别手势。我有一个很好的 SurfaceView 类,我使用 onDraw 来绘制我的精灵,并且我有一个线程运行它来调用 onDraw 等。

这一切都很好。

我试图将 GestureOverlayView 添加到此,但它不起作用。最后被黑到不会崩溃的地方,但这就是我所拥有的

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


}

onGesturePerformed 从未被调用。他们的示例在 xml 中有 GestureOverlay,我没有使用它,我的活动很简单:

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

所以我对这里缺少的信息有点迷失,它没有调用 onGesturePerformed 和漂亮的黄色“你正在画一个手势”永远不会出现。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁人与我共长歌 2024-09-19 05:13:26

这是这个问题的有效答案,

另请参阅示例源代码。

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

Here is working answer for this question,

please also see on example source code.

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

云胡 2024-09-19 05:13:25

这是一个很大的痛苦,但我终于找到了解决方案。网上绝对没有关于此的良好文档。我终于通过使用称为“FrameLayout”的东西,然后在其中添加表面视图和手势视图来使其工作。完成此操作后,即可将 FrameLayout 设置为内容视图。

不幸的是,由于某种原因,RomanGuy 提供的上述解决方案似乎不起作用(或者可能,我只是无法让它起作用)。 SurfaceView 类没有像常规视图那样的 .addView() 函数。该函数存在于 ViewGroups 中,并且可以很好地在除表面视图之外的任何内容上添加手势(我认为)。

您的 Activity 必须实现 OnGesturePerformedListener 接口才能正常工作。

您可以在我的网站上查看完整的教程和源代码: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();
            }
        }
    }    
}

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();
            }
        }
    }    
}
淡笑忘祈一世凡恋 2024-09-19 05:13:25

您创建了手势叠加层,但从未将其添加到视图层次结构中。如果它不存在,它将无法工作:)

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

流殇 2024-09-19 05:13:25

您可以简单地将视图添加到 gestureOverlayView 中:

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

然后

this.setContentView(gestureOverlayView);

gestureOverlayView 已经是一个框架布局

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文