Android - 手势不想工作
我运行了 Gestures Builder 应用程序,为向左/向右滑动创建了手势文件,并编写了以下代码:
public class MainActivity extends Activity implements OnGesturePerformedListener {
private GestureLibrary mGestureLibrary;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
gestureOverlayView.addView(inflate);
gestureOverlayView.addOnGesturePerformedListener(this);
mGestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (mGestureLibrary == null) {
finish();
}
setContentView(gestureOverlayView);
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mGestureLibrary.recognize(gesture);
for (Prediction prediction : predictions) {
if (prediction.score > 1.0) {
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
}
}
}
}
手势位于 /raw/ 中,但当我尝试测试它时,应用程序什么也没说(它成功加载手势,调用事件 onGesturePerformed,但是手势无法识别)。手势在 Gestures Buileder 中完美运行,那么我的错误在哪里?
I ran Gestures Builder app, created gestures file for slide left/right and wrote this code:
public class MainActivity extends Activity implements OnGesturePerformedListener {
private GestureLibrary mGestureLibrary;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
gestureOverlayView.addView(inflate);
gestureOverlayView.addOnGesturePerformedListener(this);
mGestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (mGestureLibrary == null) {
finish();
}
setContentView(gestureOverlayView);
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mGestureLibrary.recognize(gesture);
for (Prediction prediction : predictions) {
if (prediction.score > 1.0) {
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
}
}
}
}
gestures are in /raw/, but the app says nothing when I try to test it (it loads gesture successfully, the event onGesturePerformed is called, but the gestures are not recognized). The gestures work perfectly in Gestures Buileder, so where is my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仍然需要检查预测的名称,该名称应与您的操作之一的名称相匹配。测试相等性,然后执行您的逻辑:
You need to still check the name of the prediction which should match the name of one of your actions. Test for equality and then perform your logic:
在使用 mGestureLibrary 之前,您可能需要调用 load() 。根本没有记录它,但这就是 Lars 在本示例中所做的,并且它对我有用: http://www.vogella.com/articles/AndroidGestures/article.html
在 IDE 中,您应该能够看到 GestureStore HashMap (mNamedGestures) 中的条目。
You may need to call load() on mGestureLibrary before using it. Not that it's documented at all, but that's what Lars does in this example and it works for me: http://www.vogella.com/articles/AndroidGestures/article.html
In the IDE you should be able to see entries in the GestureStore HashMap (mNamedGestures).