手势探测器问题

时间:2010-12-17 09:22:42

标签: android

我正在尝试从左到右,从右到左实现在android中滑动。它不适用于模拟器。有时它有效。在我的代码中,有人能告诉我问题是什么吗? 如果有人有工作代码,你能给我吗?

package com.gesture;


import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.app.Activity;
import android.os.Bundle;

import android.view.GestureDetector;

import android.view.MotionEvent;

import android.view.GestureDetector.OnGestureListener;
import android.widget.Toast;



 public class SampleGestureActivity extends Activity implements OnGestureListener  {
     public final static int SWIPE_UP    = 1; 
        public final static int SWIPE_DOWN  = 2; 
        public final static int SWIPE_LEFT  = 3; 
        public final static int SWIPE_RIGHT = 4;  
        public final static int MODE_TRANSPARENT = 0; 
        public final static int MODE_SOLID = 1; 
        public final static int MODE_DYNAMIC = 2;  
        private final static int ACTION_FAKE = -13;     
        private int swipe_Min_Distance = 100; 
        private int swipe_Max_Distance = 350; 
        private int swipe_Min_Velocity = 100;  
        private int mode = MODE_DYNAMIC; 
     private GestureDetector gestureDetector;

     @Override

     public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.main);

        gestureDetector = new GestureDetector(this);

     }



     @Override

     public boolean onTouchEvent(MotionEvent event) {

         return gestureDetector.onTouchEvent(event);

     }



     @Override

     public boolean onDown(MotionEvent e) {

         // Do something

         return true;

     }



     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
             float velocityY) {

            final float xDistance=Math.abs(e1.getX()-e2.getX());
            final float yDistance=Math.abs(e1.getY()-e2.getY());
            if(xDistance>this.swipe_Max_Distance || yDistance> this.swipe_Max_Distance)

                return false;
            velocityX = Math.abs(velocityX);
            velocityY = Math.abs(velocityY);
            boolean result=false;
            if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance)
            {
                if(e1.getX() > e2.getX())   // right to left Move

                    Toast.makeText(SampleGestureActivity.this, "left", Toast.LENGTH_SHORT).show();

                    //this.listener.onSwipe(SWIPE_LEFT);
                else
                    Toast.makeText(SampleGestureActivity.this, "right", Toast.LENGTH_SHORT).show();

                    //this.listener.onSwipe(SWIPE_RIGHT);
                result=true;
            }
            else if(velocityY > this.swipe_Min_Velocity && yDistance > this.swipe_Min_Distance)
            {
                if(e1.getY() > e2.getY())   // bottom to top Move
                    Toast.makeText(SampleGestureActivity.this, "up", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(SampleGestureActivity.this, "down", Toast.LENGTH_SHORT).show();
                result=true;
            }
            return result;
     }

     @Override

     public void onLongPress(MotionEvent e) { 

         // Do something

     }



     @Override

     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

             float distanceY) {

         // Do something

         return false;

     }



     @Override

     public void onShowPress(MotionEvent e) { 

        // Do something

     }


     @Override

     public boolean onSingleTapUp(MotionEvent e) { 

         // Do something 

         Toast t = Toast.makeText(SampleGestureActivity.this, "single", Toast.LENGTH_SHORT);
          t.show();

         return true;

     }

 }


Thanks

1 个答案:

答案 0 :(得分:1)

可能是你的代码很好,因为它有时会起作用。试试真实的设备,模拟器对此不太好。