Search code examples
androidanimationviewexpandable

Expand Animation to work on both sides? (on negative axis)


I implemented an ExpandAnimation on view like this:

        @Override
    protected void applyTransformation(float interpolatedTime,
                                       Transformation t) {
        LayoutParams lp =
                (LayoutParams) view.getLayoutParams();
        if(rightAnimation){
            lp.width = (int) (mStartWidth + mDeltaWidth *
                    interpolatedTime);
            view.setLayoutParams(lp);
        }else{

        }

    }

With this animation my view expands and shrinks.

What i want to do is to implement this animation to the other side (to -x side) as well. But i get confused a little bit, since shrinking the width will not work. (Since minus widths or heights are not allowed)

Does anyone know a better way to implement expand animation to the left (-x) or up(-y) side as well?

Or maybe mirroring the view?


Solution

  • Finally I achieved to make a left hand side expand animation. Here is the code:

       private class ExpandAnimation extends Animation implements Animation.AnimationListener {
        private final int mStartWidth;
        private final int mDeltaWidth;
        int delta;
    
        public ExpandAnimation(int startWidth, int endWidth) {
            mStartWidth = startWidth;
            mDeltaWidth = endWidth - startWidth;
            RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) getLayoutParams();
            delta = param.width;
            setAnimationListener(this);
        }
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) getLayoutParams();
            delta = param.width;
            if(param.width <= 0){
                delta = 0;
            }
            param.width = (int) (mStartWidth + mDeltaWidth *
                    interpolatedTime);
            if(delta != 0){
                delta = (int) (mStartWidth + mDeltaWidth *
                        interpolatedTime) - delta;
            }
            param.leftMargin -=delta;
            setLayoutParams(param);
        }
    
        @Override
        public boolean willChangeBounds() {
            return true;
        }
    
        @Override
        public void onAnimationStart(Animation animation) {
        }
    
        @Override
        public void onAnimationEnd(Animation animation) {
            setText(text);
            requestLayout();
            mExpanded = !mExpanded;
        }
    
        @Override
        public void onAnimationRepeat(Animation animation) {
    
        }
    }
    

    For a reason that I don't know, width comes as -2 in the start. So i filter out that by an if. Apply Transformation method does the job.