Animation when modifying LinearLayout height

0

I'm modifying the height of a LinearLayout of WRAP_CONTENT for 0 (zero) and vice versa by listening for the event onClick of a Button .

I would like this change to occur gradually over a few milliseconds, such as 700ms , for example. How do I do this using the least amount of code possible?

Code snippet:

    LinearLayout ll = (LinearLayout) findViewById(R.id.teste);
ll.setOnClickListener(new View.onClickListener() {
    @Override
    public void onClick (View v){
        ViewGroup.LayoutParams params = v.getLayoutParams();
        params.height = 0;
        v.setLayoutParams(params);
    }
});

This changes the height to zero, but without transition.

EDIT : The effect looks like this

asked by anonymous 19.02.2017 / 16:15

2 answers

4

The answer has four steps:

  • Get the final size of your container;
  • Get the initial size;
  • Create a ValueAnimator to vary from initial size to final size by 700ms;
  • Set the height value and resize the container at each iteration of ValueAnimator .
  • Button button = ...; //Seu button
    LinearLayout ll = (LinearLayout) findViewById(R.id.teste);
    
    button.setOnClickListener(new View.onClickListener() {
        @Override
        public void onClick (View v){
            final ViewGroup.LayoutParams params = ll.getLayoutParams();
    
            int finalHeight = 0;
            int initialHeight = ll.getHeight();
    
            ValueAnimator va = ValueAnimator.ofInt(initialHeight, finalHeight);
    
            va.setDuration(700);
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    params.height = (Integer) animation.getAnimatedValue();
                    ll.requestLayout();
                }
            });
    
            va.start();
        }
    });
    
        
    19.02.2017 / 17:57
    1

    Well, as you were specific in the amount of code, the best result would be this:

    LinearLayout ll = (LinearLayout) findViewById(R.id.teste);
    ll.setOnClickListener(new View.onClickListener() {
        @Override
        public void onClick (View v){
            ViewGroup.LayoutParams params = v.getLayoutParams();
            params.height = 0;
            ll.postDelayed(new Runnable() {
                    public void run() {
                        v.setLayoutParams(params);
                    }, 700);
            ll.getLayoutParams().height = originaltamanhodoseulayout
        }
    });
    

    You need to know the original height of your "ll" to return it to the original alttura later. Explaining:
    it runs in the background for 700ms leaving the "ll" with height = 0 and when it finishes it reverts to the original height.

    I believe that with lower lines of code would be this, I already use this here with positive result, although for other purposes.

    of a confered ae

        
    20.02.2017 / 13:11