我在后来的回答中被告知我必须将我在代码中创建的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,我没有使用它,我的活动很简单:
@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestwindowFeature(Window.FEATURE_NO_Title); Panel p = new Panel(this); setContentVIEw(p);}
所以我在这里丢失了一些丢失的信息,它没有调用onGesturePerformed,漂亮的黄色“你正在画一个手势”从未出现过.
解决方法:
这是一个很大的痛苦,但我终于找到了解决方案.在网上任何地方都没有完整的文档.我终于通过使用一个名为“FrameLayout”的东西来完成工作,然后在那里添加了surfacevIEw和手势视图.完成后,将FrameLayout设置为内容视图.
不幸的是,RomanGuy提供的上述解决方案由于某种原因似乎不起作用(或者可能,我根本无法使其工作).没有.addVIEw()函数可用于SurfaceVIEw类,就像常规视图一样.该功能对于VIEwGroups是存在的,并且可以在我认为的除了surfacevIEw之外的任何东西上添加手势.
您的活动必须实现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(); } } } }
总结 以上是内存溢出为你收集整理的android – 将GestureOverlayView添加到我的SurfaceView类,如何添加到视图层次结构?全部内容,希望文章能够帮你解决android – 将GestureOverlayView添加到我的SurfaceView类,如何添加到视图层次结构?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)