【问题标题】:How to get a class to implement this gestureRecogniser?如何让一个类来实现这个手势识别器?
【发布时间】:2012-03-18 01:52:47
【问题描述】:

我对 Java 中的多态性和接口有点困惑。

基本上我想创建一个名为 gestureRecogniser 的类,它具有以下方法:

-onLeftwardsSwipe();

-onRightwardsSwipe();

-onUpwardsSwipe();

-onDownwardsSwipe();

gestureRecogniser 会根据需要调用这些方法。第二个类实现了gestureRecogniser 并指定必须对这些事件执行什么操作。这在 Java 中可行吗?

这是我写的课程:

    public abstract class GestureRecogniser implements OnTouchListener {

    Activity activity;
    private float XWherePressed;
    private float XWhereReleased;
    private float YWherePressed;
    private float YWhereReleased;
    private static final int MINIMUM_DISTANCE_FOR_SWIPE = 100;


    public GestureRecogniser(Activity activity)
    {
        this.activity = activity;
    }

    public abstract void onLeftwardsSwipe();
    public abstract void onRightwardsSwipe();
    public abstract void onUpwardsSwipe();
    public abstract void onDownwardsSwipe();


    public boolean onTouch(View v, MotionEvent event)
    {
        switch(event.getAction())
        {
        case MotionEvent.ACTION_DOWN:

            this.XWherePressed = event.getX();
            this.YWherePressed = event.getY();

            break;

        case MotionEvent.ACTION_UP:

            this.XWhereReleased = event.getX();
            this.YWhereReleased = event.getY();

            float deltaX = XWhereReleased - XWherePressed;
            float deltaY = YWhereReleased - YWherePressed;

            if (Math.abs( deltaX ) > this.MINIMUM_DISTANCE_FOR_SWIPE)
            {
                //HORIZONTAL SWIPE
                if(deltaX > 0 )
                {
                    this.onRightwardsSwipe();
                    return true;
                }

                if(deltaX < 0)
                {
                    this.onLeftwardsSwipe();
                    return true;
                }

            }else return false;

            if (Math.abs( deltaY) > this.MINIMUM_DISTANCE_FOR_SWIPE)
            {
                //VERTICAL SWIPE
                if( deltaY < 0 )
                {
                    this.onDownwardsSwipe();
                    return true;
                }

                if ( deltaY > 0 )
                {
                    this.onUpwardsSwipe();
                    return true;
                }

            }else return false;

            break;

        }

        return false;
    }

}

【问题讨论】:

    标签: java android interface polymorphism abstract-class


    【解决方案1】:

    我通过执行以下操作实现了预期的结果:

    1 - 使用方法创建一个公共接口:

    public interface OnSwipeListener 
    {
        public void onUpwardsSwipe();
        public void onDownwardsSwipe();
        public void onLeftwardsSwipe();
        public void onRightwardsSwipe();
    }
    

    2 - 创建一个实现 onTouchListener 的类。该类的构造函数采用 OnSwipeListener 对象。 onTouch() 方法根据需要调用方法:

    public class SwipeRecogniser implements OnTouchListener {
    
        private OnSwipeListener onSwipeListener;
        private float XWherePressed;
        private float XWhereReleased;
        private float YWherePressed;
        private float YWhereReleased;
        private static final int MINIMUM_DISTANCE_FOR_SWIPE = 100;
    
    
        public SwipeRecogniser(OnSwipeListener onSwipeListener)
        {
            this.onSwipeListener = onSwipeListener;
        }
    
        public boolean onTouch(View v, MotionEvent event)
        {
            switch(event.getAction())
            {
    
            case MotionEvent.ACTION_DOWN:
                this.XWherePressed = event.getX();
                this.YWherePressed = event.getY();
    
                break;
    
            case MotionEvent.ACTION_UP:
                this.XWhereReleased = event.getX();
                this.YWhereReleased = event.getY();
    
                float deltaX = XWhereReleased - XWherePressed;
                float deltaY = YWhereReleased - YWherePressed;
    
                if (Math.abs( deltaX ) > this.MINIMUM_DISTANCE_FOR_SWIPE)
                {
                    //HORIZONTAL SWIPE
                    if(deltaX > 0 )
                    {
                        onSwipeListener.onRightwardsSwipe();
                        return true;
                    }
    
                    if(deltaX < 0)
                    {
                        onSwipeListener.onLeftwardsSwipe();
                        return true;
                    }
    
                }
                else return true;
    
                if (Math.abs( deltaY) > this.MINIMUM_DISTANCE_FOR_SWIPE)
                {
                    //VERTICAL SWIPE
                    if( deltaY < 0 )
                    {
                        onSwipeListener.onDownwardsSwipe();
                        return true;
                    }
    
                    if ( deltaY > 0 )
                    {
                        onSwipeListener.onUpwardsSwipe();
                        return true;
                    }
    
                }
                else return true;
    
                break;  
            }   
            return true;
        }
    }
    

    3 - 在自定义类中

    public class MyClass extends Activity implements OnSwipeListener
    {
     protected void onCreate(Bundle stuff)
     {
      //
      this.setOnTouchListener(new SwipeRecogniser(this));
     }
    
     public void onUpwardsSwipe(){/*methods*/}
     public void onDownwardsSwipe(){/*methods*/}
     public void onLeftwardsSwipe(){/*methods*/}
     public void onRightwardsSwipe(){/*methods*/}
    }
    

    【讨论】:

      【解决方案2】:
      //Declare below inner class
      class MyGestureDetector extends SimpleOnGestureListener {
          @Override
          public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
              try {
                  if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                      return false;
                  // right to left swipe
                  if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                      ////swapped left
                  }  
                  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                          ////swapped right
                  }
              } catch (Exception e) {
                  // nothing
              }
              return false;
          }
      }
      
      
      //in your activtiy do below stuff and override onTouchEvent
      
      private static final int SWIPE_MIN_DISTANCE = 50;
      private static final int SWIPE_MAX_OFF_PATH = 400;
      private static final int SWIPE_THRESHOLD_VELOCITY = 250;
      private GestureDetector gestureDetector;
      View.OnTouchListener gestureListener;
          @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          //Do your stuff
          gestureDetector = new GestureDetector(new MyGestureDetector());
          gestureListener = new View.OnTouchListener() {
              public boolean onTouch(View v, MotionEvent event) {
                  if (gestureDetector.onTouchEvent(event)) {
                      return true;
                  }
                  return false;
                }
          };
         }
          @Override
      public boolean onTouchEvent(MotionEvent event) {
          if (gestureDetector.onTouchEvent(event))
             return true;
           else
                 return false;
      }
      

      【讨论】:

      • 这与问题无关。我知道 SimpleGestureListener。我想知道是否可以做我在问题中概述的事情。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多