Expandable list with animated effect

Posted by Naveen Chauhan on Stack Overflow See other posts from Stack Overflow or by Naveen Chauhan
Published on 2012-06-14T12:50:07Z Indexed on 2012/10/05 15:38 UTC
Read the original article Hit count: 256

Filed under:

I am using this animation class to create the animation when i shrink and expand the list on some click event

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout.LayoutParams; 
public class ExpandAnimation extends Animation{
    private View mAnimatedView;
    private LayoutParams mViewLayoutParams;
    private int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;

    public ExpandAnimation(View view, int duration){
        setDuration(duration);
        mAnimatedView = view;
        System.out.println(view.getVisibility());
        mViewLayoutParams = (LayoutParams)view.getLayoutParams();
        mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);
        System.out.println("mIsVisibleAfter:- "+ mIsVisibleAfter);
        mMarginStart = mViewLayoutParams.bottomMargin;
        System.out.println("mMarginStart:- "+ mMarginStart);
        mMarginEnd = (mMarginStart == 0 ?(0 - view.getHeight()):0);
        System.out.println("mMarginEnd:- "+mMarginEnd);
        view.setVisibility(View.VISIBLE);   
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t){
        super.applyTransformation(interpolatedTime, t);
        System.out.println("mMarginEnd:- "+interpolatedTime);
        if(interpolatedTime<1.0f){
            System.out.println("Inside if true");
            mViewLayoutParams.bottomMargin = mMarginStart + (int) ((mMarginEnd - mMarginStart)*interpolatedTime);
            System.out.println("mViewLayoutParams.bottomMargin:- "+mViewLayoutParams.bottomMargin);
            mAnimatedView.requestLayout();
        }else if(!mWasEndedAlready){
            mViewLayoutParams.bottomMargin = mMarginEnd;


            mAnimatedView.requestLayout();
            System.out.println("mIsVisibleAfter:- "+mIsVisibleAfter);
            if(mIsVisibleAfter){
                mAnimatedView.setVisibility(View.GONE);
            }
            mWasEndedAlready = true;
        }
    }
}

i am using following lines on some click event in my activity class to create the object of my animation class

            View toolbar = (View) findViewById(R.id.toolbar1);
        ExpandAnimation expandani = new ExpandAnimation(toolbar,500);
        toolbar.startAnimation(expandani);

My probem is that when click event occurs, my list expand and then shrink but it must stop when it grows completely and shrink when i click on up image.

please let me know that how my animation class is working. i have also tried myself by using SOP statements which you can see in my animation class.

© Stack Overflow or respective owner

Related posts about android