Android手势检测与识别
我正在尝试在开发人员拥有的简单手势识别源代码中添加“fling”手势检测(本网站)。这是代码:
public class GestureMix extends Activity implements OnGesturePerformedListener{
/** Called when the activity is first created. */
private GestureLibrary mLibrary;
private static final int LARGE_MOVE= 60;
private static final String TAG= "Debug";
private GestureDetector gestureDetector;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv= (TextView)findViewById(R.id.textView1);
gestureDetector= new GestureDetector(this, new SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
if(e1.getY()- e2.getY() > LARGE_MOVE){
tv.setText("\nFling Ke Atas dengan kecepatan" +velocityY);
//tv.append("\nFling Ke Atas dengan kecepatan" +velocityY);
Log.d(TAG, "\nFling Ke Atas dengan kecepatan" +velocityY);
return true;
} else if(e2.getY()-e1.getY() > LARGE_MOVE){
tv.setText("\nFling Ke Bawah dengan kecepatan" +velocityY);
//tv.append("\nFling Ke Bawah dengan kecepatan" +velocityY);
Log.d(TAG,"\nFling Ke Bawah dengan kecepatan" +velocityY );
return true;
} else if(e1.getX()-e2.getX() > LARGE_MOVE){
tv.setText("\nFling Ke Kiri dengan kecepatan" +velocityX);
//tv.append("\nFling Ke Kiri dengan kecepatan" +velocityX);
Log.d(TAG, "\nFling Ke Kiri dengan kecepatan" +velocityX);
return true;
} else if(e2.getX()-e1.getX() > LARGE_MOVE){
tv.setText("\nFling Ke Kanan dengan kecepatan" +velocityX);
//tv.append("\nFling Ke Kanan dengan kecepatan" +velocityX);
Log.d(TAG,"\nFling Ke Kanan dengan kecepatan" +velocityX );
return true;
}
return false;
}
});
Log.d(TAG, "d on create");
mLibrary= GestureLibraries.fromRawResource(this, R.raw.gestures);
if(!mLibrary.load()){
finish();
}
GestureOverlayView gst= (GestureOverlayView) findViewById(R.id.gesture);
gst.addOnGesturePerformedListener(this);
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// TODO Auto-generated method stub
Log.d(TAG, "d gesture perform");
ArrayList<Prediction> predictions= mLibrary.recognize(gesture);
if(predictions.size()>0){
Prediction prediction= predictions.get(0);
if(prediction.score >1.0){
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
Log.d(TAG, prediction.name);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event){
Log.d(TAG, "d on touch");
return gestureDetector.onTouchEvent(event);
}
}
手势识别进展顺利(它得到了正确的结果),但是手势检测(onfling)不会显示结果(无论是在文本视图中还是在 logCat 中,它都不会出现。看来手势检测器无法在 gestureoverlayview 上工作。
我该如何修复此代码,以便我可以看到手势识别和投掷的速度作为结果?
请询问。为了澄清和我很乐意告诉你我所尝试的具体内容,
我很抱歉我的英语不好并且编程知识较少。
I'm trying to add "fling" gesture detection in simple gesture recognition source code that developer has(on this site). and this is the code:
public class GestureMix extends Activity implements OnGesturePerformedListener{
/** Called when the activity is first created. */
private GestureLibrary mLibrary;
private static final int LARGE_MOVE= 60;
private static final String TAG= "Debug";
private GestureDetector gestureDetector;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv= (TextView)findViewById(R.id.textView1);
gestureDetector= new GestureDetector(this, new SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
if(e1.getY()- e2.getY() > LARGE_MOVE){
tv.setText("\nFling Ke Atas dengan kecepatan" +velocityY);
//tv.append("\nFling Ke Atas dengan kecepatan" +velocityY);
Log.d(TAG, "\nFling Ke Atas dengan kecepatan" +velocityY);
return true;
} else if(e2.getY()-e1.getY() > LARGE_MOVE){
tv.setText("\nFling Ke Bawah dengan kecepatan" +velocityY);
//tv.append("\nFling Ke Bawah dengan kecepatan" +velocityY);
Log.d(TAG,"\nFling Ke Bawah dengan kecepatan" +velocityY );
return true;
} else if(e1.getX()-e2.getX() > LARGE_MOVE){
tv.setText("\nFling Ke Kiri dengan kecepatan" +velocityX);
//tv.append("\nFling Ke Kiri dengan kecepatan" +velocityX);
Log.d(TAG, "\nFling Ke Kiri dengan kecepatan" +velocityX);
return true;
} else if(e2.getX()-e1.getX() > LARGE_MOVE){
tv.setText("\nFling Ke Kanan dengan kecepatan" +velocityX);
//tv.append("\nFling Ke Kanan dengan kecepatan" +velocityX);
Log.d(TAG,"\nFling Ke Kanan dengan kecepatan" +velocityX );
return true;
}
return false;
}
});
Log.d(TAG, "d on create");
mLibrary= GestureLibraries.fromRawResource(this, R.raw.gestures);
if(!mLibrary.load()){
finish();
}
GestureOverlayView gst= (GestureOverlayView) findViewById(R.id.gesture);
gst.addOnGesturePerformedListener(this);
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// TODO Auto-generated method stub
Log.d(TAG, "d gesture perform");
ArrayList<Prediction> predictions= mLibrary.recognize(gesture);
if(predictions.size()>0){
Prediction prediction= predictions.get(0);
if(prediction.score >1.0){
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
Log.d(TAG, prediction.name);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event){
Log.d(TAG, "d on touch");
return gestureDetector.onTouchEvent(event);
}
}
The gesture recognition went well (it toast the right result), but the gesture detection (onfling) won't show the result (it won't appear whether in the textview or in the logCat. It seems the gesture detector won't work on gestureoverlayview.
How could I fix this code, so I could see the gesture recognition and the velocity of the fling as the result?
Any advice would be greatly appreciated. Please ask for clarification and I'll happily tell you the specifics of what I've tried.
I'm sorry for my bad in English and less knowledge in programming. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论