Android: Expand/collapse animation

后端 未结 30 2524
说谎
说谎 2020-11-22 05:01

Let\'s say I have a vertical linearLayout with :

[v1]
[v2]

By default v1 has visibily = GONE. I would like to show v1 with an expand animat

30条回答
  •  天涯浪人
    2020-11-22 05:35

    If you don't want to expand or collapse all the way - here is a simple HeightAnimation -

    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.Transformation;
    
    public class HeightAnimation extends Animation {
        protected final int originalHeight;
        protected final View view;
        protected float perValue;
    
        public HeightAnimation(View view, int fromHeight, int toHeight) {
            this.view = view;
            this.originalHeight = fromHeight;
            this.perValue = (toHeight - fromHeight);
        }
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            view.getLayoutParams().height = (int) (originalHeight + perValue * interpolatedTime);
            view.requestLayout();
        }
    
        @Override
        public boolean willChangeBounds() {
            return true;
        }
    }
    

    Usage:

    HeightAnimation heightAnim = new HeightAnimation(view, view.getHeight(), viewPager.getHeight() - otherView.getHeight());
    heightAnim.setDuration(1000);
    view.startAnimation(heightAnim);
    

提交回复
热议问题