androidanimationviewviewgroup

How do you animate a change in a view's padding?


I want to animate the change in the padding of a view. The resting place of the translation animation is the same as the padding I want to apply.

TranslateAnimation moveleft = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
                    Animation.ABSOLUTE, PADDING, Animation.ABSOLUTE,
                    0.0f, Animation.ABSOLUTE, 0.0f);

moveLeft.setDuration(500);
moveLeft.setFillAfter(true);

This starts the view's animation then sets the padding. This doesn't exactly work because it cause a graphical glitch.

v.startAnimation(moveleft);   
v.setPadding(PADDING, 0, 0,0);

Solution

  • Instead of setting your padding right away, why not try an animation listener to set the padding after the animation has completed?

    v.setAnimationListener(new Animation.AnimationListener() {
        ...
        @Override
        public void onAnimationEnd(){
            v.setPadding(PADDING, 0, 0,0);
        }
        ...
    });
    v.startAnimation(moveleft);