enrico
enrico

Reputation: 67

How to move button after translate animation?

I'm moving a centered horizontally button to the left (Relative Layout). After playing the translate animation (onAnimationEnd) I set the layout params to remove the CENTER_HORIZONTAL rule. Now there are two possibilities:

1) If I set fillAfter(true) the button plays the animation, then goes off screen for a half;

2) If I set fillAfter(false) the button plays the animation (so it gets to the left), then it flashes for a millisecond. That's because it comes back to its original position until I call the setLayoutParams and it sets correctly to the left. But it flashes and that's not nice.

How can I avoid the flash?

Code

TranslateAnimation translateAnimation = new TranslateAnimation(0, LEFT_TRANSLATION, 0, 0);

translateAnimation.setDuration(1000);
translateAnimation.setFillAfter(true);

button1.startAnimation(translateAnimation);

translateAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) button1.getLayoutParams();
        lp.removeRule(RelativeLayout.CENTER_HORIZONTAL);
        button1.setLayoutParams(lp);

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }
});

Upvotes: 1

Views: 949

Answers (1)

enrico
enrico

Reputation: 67

SOLUTION FOUND!

Use

button.clearAnimation();

in onAnimationEnd() like this:

@Override
public void onAnimationEnd(Animation animation) {

    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) button1.getLayoutParams();
    lp.removeRule(RelativeLayout.CENTER_HORIZONTAL);
    button1.setLayoutParams(lp);

    button1.clearAnimation();
}

Source: http://www.helloandroid.com/tutorials/solving-advanced-animation-problems

Upvotes: 2

Related Questions