Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 347
Tevin J Avatar asked Aug 22 '13 02:08

Tevin J


People also ask

How to animate a layout in Android?

Android offers pre-loaded animation that the system runs each time you make a change to the layout. All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you.

How do you animate a view?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.

How do you fade animation on Android?

Create XML File to Define Animation To use Fade In or Fade Out animations in our android applications, we need to define a new XML file with <alpha> tag like as shown below. For Fade In animation, we need to increase the alpha value from 0 to 1 like as shown below.


1 Answers

Use ValueAnimator, its really simple and unclutter

say, we have to change right padding to _20dp where as left, top and bottom padding are _6dp, _6dp and 0 respectively.

ofInt() is varagrs type. the value we have to animate is pass in it as KeyValue pair (arg1 = current value, arg2 = target value,............)

Here we go,

ValueAnimator animator = ValueAnimator.ofInt(view.getPaddingRight(), _20dp);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator){
        view.setPadding(_6dp, _6dp, (Integer) valueAnimator.getAnimatedValue(), 0);
    }
});
animator.setDuration(200);
animator.start();
like image 96
Amit Yadav Avatar answered Nov 15 '22 20:11

Amit Yadav