Monday 4 August 2014

Custom Slide Animation for a Layout onButtonClick

If you came across a scenario where you need to slide out a view(Linear layout/Relative Layout) i.e. Slide animation while changing the visibility GONE to VISIBLE and vice versa, you may use this code snippet



import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout;

public class CustomSlideAnimation extends Animation {

    public final static int COLLAPSE = 1;
    public final static int EXPAND = 0;

    private View mView;
    private int mEndHeight;
    private int mType;
    private LinearLayout.LayoutParams mLayoutParams;

    public CustomSlideAnimation(View view, int duration, int type) {

        setDuration(duration);
        mView = view;
        mEndHeight = mView.getHeight();
        mLayoutParams = ((LinearLayout.LayoutParams) view.getLayoutParams());
        mType = type;
        if(mType == EXPAND) {
            mLayoutParams.height = 0;
        } else {
            mLayoutParams.height = LayoutParams.WRAP_CONTENT;
        }
        view.setVisibility(View.VISIBLE);
    }

    public int getHeight(){
        return mView.getHeight();
    }

    public void setHeight(int height){
        mEndHeight = height;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
            if(mType == EXPAND) {
                mLayoutParams.height =  (int)(mEndHeight * interpolatedTime);
            } else {
                mLayoutParams.height = (int) (mEndHeight * (1 - interpolatedTime));
            }
            mView.requestLayout();
        } else {
            if(mType == EXPAND) {
                mLayoutParams.height = LayoutParams.WRAP_CONTENT;
                mView.requestLayout();
            }else{
                mView.setVisibility(View.GONE);
            }
        }
    }
}



Provide animation to the layout in the button's Click Listener to provide the Custom Slide Animation as shown below.

button.setOnClickListener(new OnClickListener() {
            int height;
            @Override
            public void onClick(View arg0) {
                if (layout.getVisibility() == View.VISIBLE && status == 0) {
                    

                    CustomSlideAnimation a = new CustomSlideAnimation(replyLayout,
                            1000, CustomSlideAnimation.COLLAPSE);
                    height = a.getHeight();
                   
layout.startAnimation(a);
                    status = 1;
                } else {
                   
                    CustomSlideAnimation a = new CustomSlideAnimation(replyLayout,
                            1000, CustomSlideAnimation.EXPAND);
                    a.setHeight(height);
                   
layout.startAnimation(a);
                    status = 0;
                }
            }
        });