2016-11-21 6 views
0

В моей новой программе мне нужны объекты, которые можно вытолкнуть в сторону. У меня уже есть моя анимация, которая работает, и я пытался обнаружить прокрутку пользователя в другом классе. Проблема в том, что я не знаю, как их подключить. Когда жестом салфетки правильно распознан, следует запустить определенную анимацию.android - animate on swipe

Мои AnimatedViewClass:

private Runnable r = new Runnable() { 
     @Override 
     public void run() { 
      if(continueAnimation) { 
      invalidate(); 
      } 
     } 
    }; 


    protected void onDraw(Canvas c) { 

       if (x<0) { 
        x = this.getWidth()/2-100; 
        y = this.getHeight()/2-100; 
       } 

        else { 
         x += xVelocity; 

          if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) { 
           boolean continueAnimation = false; 
          } 
        } 

       c.drawBitmap(ball.getBitmap(), x, y, null); 

       if(continueAnimation) 
       { 
        h.postDelayed(r, FRAME_RATE); 
       } 

       else { 
         x = this.getWidth()-ball.getBitmap().getWidth(); 
       } 

      } 

Мой SwipeTouchListener:

public class OnSwipeTouchListener implements OnTouchListener { 

    private final GestureDetector gestureDetector; 

    public OnSwipeTouchListener (Context ctx){ 
     gestureDetector = new GestureDetector(ctx, new GestureListener()); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return gestureDetector.onTouchEvent(event); 
    } 

    private final class GestureListener extends SimpleOnGestureListener { 

     private static final int SWIPE_THRESHOLD = 100; 
     private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      boolean result = false; 
      try { 
       float diffY = e2.getY() - e1.getY(); 
       float diffX = e2.getX() - e1.getX(); 
       if (Math.abs(diffX) > Math.abs(diffY)) { 
        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffX > 0) { 
          onSwipeRight(); 
         } else { 
          onSwipeLeft(); 
         } 
        } 
        result = true; 
       } 
       else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
         if (diffY > 0) { 
          onSwipeBottom(); 
         } else { 
          onSwipeTop(); 
         } 
        } 
        result = true; 

      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 
      return result; 
     } 
    } 
} 

ответ

1

Вы можете добавить GestureDetector к классу зрения так же, как это, и заменить код внутри onFling()

public class AnimatedViewClass extends View { 

GestureDetector gestureDetector; 

public AnimatedViewClass(Context context) { 
    super(context); 
    gestureDetector = new GestureDetector(getContext(), new GestureDetectorListener()); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    gestureDetector.onTouchEvent(event); 
    return super.onTouchEvent(event); 
} 

private void onSwipeRight(){ 
    // swipe right detected 
    // do stuff 
    invalidate(); 
} 


private void onSwipeLeft(){ 
    // swipe left detected 
    // do stuff 
    invalidate(); 
} 

private class GestureDetectorListener extends 
     GestureDetector.SimpleOnGestureListener { 

    private static final int SWIPE_THRESHOLD = 100; 
    private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

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

     // onSwipeRight() 
     // onSwipeLeft() 

     return super.onFling(e1, e2, velocityX, velocityY); 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     return super.onDown(e); 
    } 
} 


private Runnable r = new Runnable() { 
    @Override 
    public void run() { 
     if(continueAnimation) { 
      invalidate(); 
     } 
    } 
}; 


protected void onDraw(Canvas c) { 

    if (x < 0) { 
     x = this.getWidth()/2 - 100; 
     y = this.getHeight()/2 - 100; 
    } else { 
     x += xVelocity; 

     if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) { 
      boolean continueAnimation = false; 
     } 
    } 

    c.drawBitmap(ball.getBitmap(), x, y, null); 

    if (continueAnimation) { 
     h.postDelayed(r, FRAME_RATE); 
    } else { 
     x = this.getWidth() - ball.getBitmap().getWidth(); 
    } 

} 


} 
+0

ли GestureDetector переписывает разные направления? И я вызываю анимацию в 'onSwipeRight', например. или я могу настроить определенные анимации в этих методах? –

+0

Да, точно, сделайте то, что вы хотите сделать внутри onSwipeRight, например, запустив нужную анимацию. – Khaled

+0

Отлично, я попробую. Что такое метод 'invalidate();' метода swipe? –