2016-11-21 1 views
0

Dans mon nouveau programme, j'ai besoin d'objets qui peuvent être glissés sur le côté. J'ai déjà mon animation, qui fonctionne et j'ai essayé de détecter le glissement de l'utilisateur dans une autre classe. Le problème que j'ai, c'est que je ne sais pas comment les connecter. Lorsque le mouvement de balayage est correctement reconnu, une animation spécifique doit démarrer.android - animate on swipe

Mes 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(); 
       } 

      } 

Mon 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; 
     } 
    } 
} 

Répondre

1

Vous pouvez ajouter GestureDetector à votre classe de vue comme cela, et remplacer votre code à l'intérieur 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

Est-ce que GestureDetector reconnaît différentes directions? Et puis-je appeler l'animation dans 'onSwipeRight' par exemple. ou puis-je définir des animations spécifiques dans ces méthodes? –

+0

Oui exactement, faites ce que vous voulez faire dans onSwipeRight, comme exécuter votre animation souhaitée. – Khaled

+0

Parfait, je vais essayer. À quoi sert la méthode 'invalidate();' dans la méthode de balayage? –