在OnTouchListener中使用Action时出错

时间:2015-02-05 12:36:54

标签: android android-studio action ontouchlistener

我正在创建一个我正在实施Ontouchlistener的应用,但我没有得到正确的代码。请有人帮我写正确的代码。这是我使用各种教程为我的活动编写的代码。任何形式的帮助将不胜感激。

public class ActivitySwipeDetector
  implements OnTouchListener
{
  private static final int MIN_DISTANCE = 100;
  private static final String logTag = "SwipeDetector";
  private float downX;
  private float downY;
  public Action mSwipeDetected = Action.None;
  private float upX;
  private float upY;

  public Action getAction()
  {
    return this.mSwipeDetected;
  }

  public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
  {
    switch (paramMotionEvent.getAction())
    {
    default:
    case 0:
    case 1:
    }
    float f2;
    do {
        do {
            float f1;
            do {
                //return false;
                this.downX = paramMotionEvent.getX();
                this.downY = paramMotionEvent.getY();
                this.mSwipeDetected = Action.None;
                // return false;
                this.upX = paramMotionEvent.getX();
                this.upY = paramMotionEvent.getY();
                f1 = this.downX - this.upX;
                f2 = this.downY - this.upY;
                if (Math.abs(f1) <= 100.0F)
                    break;
                if (f1 < 0.0F) {
                    Log.i("SwipeDetector", "Swipe Left to Right");
                    this.mSwipeDetected = Action.LR;
                    return false;
                }

            }
            while (f1 <= 0.0F);
            Log.i("SwipeDetector", "Swipe Right to Left");
            this.mSwipeDetected = Action.RL;
            return false;
        }
        while (Math.abs(f2) <= 100.0F);

        if (f2 < 0.0F) {
            Log.i("SwipeDetector", "Swipe Top to Bottom");
            this.mSwipeDetected = Action.TB;
            return false;
        }
    }
    while (f2 <= 0.0F);
    Log.i("SwipeDetector", "Swipe Bottom to Top");
    this.mSwipeDetected = Action.BT;
    return false;
  }

  public boolean swipeDetected()
  {
    return this.mSwipeDetected != Action.None;
  }

  public static enum Action
  {
      LR, RL, TB, BT, None;

      static
    {

      Action[] arrayOfAction = new Action[5];
      arrayOfAction[0] = LR;
      arrayOfAction[1] = RL;
      arrayOfAction[2] = TB;
      arrayOfAction[3] = BT;
      arrayOfAction[4] = None;
    }


  }
}

1 个答案:

答案 0 :(得分:0)

  1. 这不是一个真正的问题,很难理解你遇到了什么问题。
  2. 您应该使用现有常量来提高可读性。请参阅文档here
  3. 看来你做得太多了。浏览this tutorial (from android's documentation),了解如何使用更少的代码实现更多目标。
  4. HTH!