2

enter image description here

I have 3 textviews in a linear layout. On a button press I have to hide 2 of them, as show in figure above. Right now it suddenly flashes and doesn't look good at all. I want to have a 'collapse' effect so that it slowly goes up like its being shrinked. Any leads on how to do this?

I tries 'animateLayoutChanges' in XML and Tranlate animation but none of them is a smooth experience.

Thanks for helping!

Vipul J
  • 6,641
  • 9
  • 46
  • 61

3 Answers3

1

You can animate your linearlayouts by adding this paramter:

android:animateLayoutChanges="true"

This only works from API 11/Android 3.0. As far is i know, this is the only easy way to animate the change of visibility. If you want something more custom, i don't think there is a shorter way around.

Chris
  • 3,329
  • 1
  • 30
  • 44
  • Tried it already as I mentioned. But its not smooth, still flashes. – Vipul J Aug 12 '14 at 10:29
  • Have you tried it on a different device? Or with different timings? If you want more control over it, i would suggest to override setVisibility and give it a custom animation there. Check this answer out: http://stackoverflow.com/a/12388385/1759274 – Chris Aug 12 '14 at 10:31
0

For a "collapse" animation you could do the following animation in on click of your textview

TranslateAnimation collapseAnim = new TranslateAnimation(0.0f, 0.0f, 0.0f, -view.getHeight());
        collapseAnim.setAnimationListener(new AnimationListener() {
            //.....

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.GONE);
            }
        });

        collapseAnim.setDuration(300);
        collapseAnim.setInterpolator(new AccelerateInterpolator(0.5f));
        //Starts Animation
        view.startAnimation(collapseAnim);
santoshavss
  • 221
  • 1
  • 6
0

use this

final ViewGroup.LayoutParams lp = dismissView.getLayoutParams();
    final int originalHeight = dismissView.getHeight();

    ValueAnimator animator = ValueAnimator.ofInt(originalHeight, 1).setDuration(mAnimationTime);

    animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            //gone dismissView
        }
    });

    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            lp.height = (Integer) valueAnimator.getAnimatedValue();
            dismissView.setLayoutParams(lp);
        }
    });

    animator.start();
MHP
  • 2,613
  • 1
  • 16
  • 26