Android手势检测和识别

时间:2012-01-14 03:26:39

标签: android view gesture-recognition gesturedetector swipe-gesture

我正在尝试在开发人员具有的简单手势识别源代码中添加“fling”手势检测(on this site)。这是代码:

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)不会显示结果(无论是在textview还是在logCat中都不会出现。似乎手势检测器赢了不能使用 gestureoverlayview

我怎么能修复这段代码,所以我可以看到手势识别和结果的速度?

任何建议都将不胜感激。请求澄清,我很乐意告诉你我尝试过的具体细节。

我很抱歉我的英语不好,编程知识也不多。感谢。

0 个答案:

没有答案