Android: Expand/collapse animation

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

    Here is my solution. I think it is simpler. It only expands the view but can easy be extended.

    public class WidthExpandAnimation extends Animation
    {
        int _targetWidth;
        View _view;
    
        public WidthExpandAnimation(View view)
        {
            _view = view;
        }
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t)
        {
            if (interpolatedTime < 1.f)
            {
                int newWidth = (int) (_targetWidth * interpolatedTime);
    
                _view.layout(_view.getLeft(), _view.getTop(),
                        _view.getLeft() + newWidth, _view.getBottom());
            }
            else
                _view.requestLayout();
        }
    
        @Override
        public void initialize(int width, int height, int parentWidth, int parentHeight)
        {
            super.initialize(width, height, parentWidth, parentHeight);
    
            _targetWidth = width;
        }
    
        @Override
        public boolean willChangeBounds() {
            return true;
        }
    }
    

提交回复
热议问题