Search code examples
androidanimationgesture

Limit doubletap gesture to just one double tap


I have this onDoubleTap method implemented, it is really simple, it just pops up a photo, what I like is that if I do the doubleTap it just animate it one time and when I do it again it dosn't do it anymore

Here is my code

@Override
public boolean onDoubleTap(MotionEvent motionEvent) {
    animateHeart(myImageView);
    return false;
}

public void animateHeart(final ImageView view) {
    ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            prepareAnimation(scaleAnimation);

    AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 3.0f);
    prepareAnimation(alphaAnimation);

    AnimationSet animation = new AnimationSet(true);
    animation.addAnimation(alphaAnimation);
    animation.addAnimation(scaleAnimation);
    animation.setDuration(400);
    animation.setFillAfter(true);

    view.startAnimation(animation);    
}

private Animation prepareAnimation(Animation animation){
    animation.setRepeatCount(1);
    animation.setRepeatMode(Animation.REVERSE);
    return animation;
}

Thanks


Solution

  • First of all, you need to return true if the event is consumed. Then, why don't you have a flag to check if you already handled it?

    private boolean isConsumed;
    
    @Override
    public boolean onDoubleTap(MotionEvent motionEvent) {
        if (!isConsumed) {
            animateHeart(myImageView);
            isConsumed = true;
        }
        return true;
    }