android – 将GestureOverlayView添加到我的SurfaceView类,如何添加到视图层次结构?
作者:互联网
我在后来的回答中被告知我必须将我在代码中创建的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,漂亮的黄色“你正在画一个手势”从未出现过.
解决方法:
这是一个很大的痛苦,但我终于找到了解决方案.在网上任何地方都没有完整的文档.我终于通过使用一个名为“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();
}
}
}
}
标签:gesture,android,surfaceview 来源: https://codeday.me/bug/20190723/1518213.html