限制触摸事件一次

时间:2014-03-08 07:40:51

标签: android touch motion

如何将Touchevent限制为一次/两次,这意味着用户只能进行一次touchevent。

以下是Touchevent的代码,我不知道应该如何编辑它,touchevent只反应一两次。

   @Override
 public boolean onTouchEvent(MotionEvent event) {
    float currentX = event.getX();
    float currentY = event.getY();
    float deltaX, deltaY;
    float scalingFactor = 15.0f / ((box.xMax > box.yMax) ? box.yMax : box.xMax);
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    deltaX = currentX - previousX; //vorherig(previous)  - aktuell
          deltaY = currentY - previousY;
          ball.speedX += deltaX * scalingFactor;
          ball.speedY += deltaY * scalingFactor;
      break;              
    case MotionEvent.ACTION_UP:
        // Modify rotational angles according to movement

    }
    // Save current x, y
    previousX = currentX;
    previousY = currentY;
    return true;  // Event handled
 }

1 个答案:

答案 0 :(得分:1)

维护boolean变量,如下所示......

boolean touchCounter = false;

@Override
 public boolean onTouchEvent(MotionEvent event) {
    float currentX = event.getX();
    float currentY = event.getY();
    float deltaX, deltaY;
    float scalingFactor = 15.0f / ((box.xMax > box.yMax) ? box.yMax : box.xMax);

    if (touchCounter == false) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

             touchCounter = true;

             deltaX = currentX - previousX; //vorherig(previous)  - aktuell
             deltaY = currentY - previousY;
             ball.speedX += deltaX * scalingFactor;
             ball.speedY += deltaY * scalingFactor;
             break;              
        case MotionEvent.ACTION_UP:

             touchCounter = true;

             // Modify rotational angles according to movement
             break;

        }
    }

    // Save current x, y
    previousX = currentX;
    previousY = currentY;
    return true;  // Event handled
 }