本文介绍了添加GestureOverlayView我SurfaceView类,如何添加,查看层次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知在后面的答案,我要补充我在code创建我的看法层次的GestureOverlayView,我不是100%,如何做到这一点。下面是完整的原来的问题。

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.

我希望我的游戏能够识别手势。我有我做了一个OnDraw中吸取我的精灵这个漂亮的SurfaceView类,我有一个线程,多数民众赞成在运行它来调用OnDraw的等等。

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 .

这一切的伟大工程。

我想给GestureOverlayView添加到这一点,它只是不工作。最后砍死它不会崩溃,但是这是我

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


}

该onGesturePerformed永远不会被调用。他们的例子在XML中GestureOverlay,我不使用,我的活动很简单:

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

所以我在有点缺少的部分这里信息的损失,它不叫onGesturePerformed和漂亮的pretty的黄色你是画一个手势一直没有出现。

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.

推荐答案

这是一个重大的痛苦,但我终于想通了解决方案。我们绝对在这个任何地方没有很好的在线文档。我终于得到它通过使用一种叫做的FrameLayout,然后添加两个surfaceview和手势观点有工作的工作。一旦你这样做,你设置的FrameLayout作为内容视图。

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.

不幸的是,RomanGuy提供的上述方案似乎并没有工作(或可能的话,我只是无法得到它的工作)的一些原因。有无.addView()函数可用于SurfaceView类像有一个常规视图。该函数存在ViewGroups和做工精细上以外的任何其他surfaceview我想加入的手势。

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.

您的活动必须实现OnGesturePerformedListener接口为此工作。

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

您可以看到我的网站在这里完整的教程和源$ C ​​$ C:的http:// scanplaygames。 COM / P = 193

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

这篇关于添加GestureOverlayView我SurfaceView类,如何添加,查看层次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:46