Slamit
Slamit

Reputation: 543

Animation working only once

I'm trying to animate an ImageButton so that it would move a little each time it's clicked. For some reasons though, the animation work only once.

Code:

public void ballClicked(View v) {

        imgBtn.clearAnimation();
        imgBtn.animate().translationX(50).setDuration(500).start();
        imgBtn.animate().translationY(50).setDuration(500).start();

        oldX = oldX+50;
        oldY = oldY+50;
        imgBtn.setClickable(false);

        // Using this as Animation Listener doesn't trigger when the animation finish
        new Handler().postDelayed(new Runnable() {
            public void run() {
                imgBtn.setX((float)oldX+50);
                imgBtn.setY((float)oldY+50);
                imgBtn.setClickable(true);
            }
        }, 500);

    }`

Upvotes: 1

Views: 1025

Answers (1)

Charuka Silva
Charuka Silva

Reputation: 13153

This will work!

public void ballClicked(View v) {

    imgBtn.clearAnimation();

    oldX = oldX + 50;
    oldY = oldY + 50;

    imgBtn.animate().translationX(oldX).setDuration(500).start();
    imgBtn.animate().translationY(oldY).setDuration(500).start();

    new Handler().postDelayed(new Runnable() {
        public void run() {
            imgBtn.setX((float) oldX + 50);
            imgBtn.setY((float) oldY + 50);

        }
    }, 500);


}

Upvotes: 1

Related Questions