Android: Expand/collapse animation

后端 未结 30 2650
说谎
说谎 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:24

    Based on solutions by @Tom Esterez and @Seth Nelson (top 2) I simlified them. As well as original solutions it doesn't depend on Developer options (animation settings).

    private void resizeWithAnimation(final View view, int duration, final int targetHeight) {
        final int initialHeight = view.getMeasuredHeight();
        final int distance = targetHeight - initialHeight;
    
        view.setVisibility(View.VISIBLE);
    
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1 && targetHeight == 0) {
                    view.setVisibility(View.GONE);
                }
                view.getLayoutParams().height = (int) (initialHeight + distance * interpolatedTime);
                view.requestLayout();
            }
    
            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
    
        a.setDuration(duration);
        view.startAnimation(a);
    }
    

提交回复
热议问题