【问题标题】:Adding GestureOverlayView to my SurfaceView class, how to add to view hierarchy?将 GestureOverlayView 添加到我的 SurfaceView 类,如何添加到视图层次结构?
【发布时间】:2010-07-30 23:01:53
【问题描述】:

我在稍后的回答中被告知我必须将我在代码中创建的 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 并且漂亮漂亮的黄色“你正在画一个手势”永远不会出现。

【问题讨论】:

    标签: android gesture surfaceview


    【解决方案1】:

    这是一个很大的痛苦,但我终于找到了解决方案。在网上任何地方都绝对没有很好的文档。我终于通过使用一种叫做“FrameLayout”的东西让它工作了,然后在那里添加了表面视图和手势视图。一旦你这样做了,你将 FrameLayout 设置为你的内容视图。

    不幸的是,由于某种原因,RomanGuy 提供的上述解决方案似乎不起作用(或者可能,我只是无法让它起作用)。 SurfaceView 类有no .addView() 函数,就像普通视图一样。该功能适用​​于 ViewGroups,并且可以很好地在我认为的 SurfaceView 以外的任何东西上添加手势。

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

    【讨论】:

    • 哇,很久以前我就已经修改过这个了。不过,这些新信息会让我重新了解它。
    【解决方案2】:

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

    【讨论】:

    • 好吧,讨厌听起来像一个下降,但我如何将它添加到视图层次结构中。 ?我认为它存在于 .xml 文件中吗?我怎样才能将它添加到层次结构而不是在 xml 文件中?
    【解决方案3】:

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

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

    然后

    this.setContentView(gestureOverlayView);
    

    gestureOverlayView 已经是一个框架布局

    【讨论】:

      【解决方案4】:

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

      请参见示例源代码。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多