Android: How to change my ExpandAnimation class to handle animation correctly?

Posted by IamStalker on Stack Overflow See other posts from Stack Overflow or by IamStalker
Published on 2012-07-05T09:12:40Z Indexed on 2012/07/05 9:15 UTC
Read the original article Hit count: 234

Filed under:
|

here is my animation class that i have taken from Udnic - thanks m8, but i'm using it "not from closed position but from opened" and it's jumps and not behaving correctly.

what should i do?

    public class ExpandAnimation extends Animation {
    private View mAnimatedView;
    private LinearLayout.LayoutParams mViewLayoutParams;
    public int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;
    private ImageView mImageView;
    /**
     * Initialize the animation
     * @param view The layout we want to animate
     * @param duration The duration of the animation, in ms
     */
    public ExpandAnimation(View view, int duration, ImageView imageView) {
        setDuration(duration);
        mImageView = imageView;
        mAnimatedView = view;
        mViewLayoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();

        mMarginStart = mMarginEnd = 0;
        // decide to show or hide the view
        mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);
        //mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0);

        mMarginStart = mViewLayoutParams.bottomMargin;
        if(mMarginStart != 0)
            mMarginStart = 0 - view.getHeight();
        mMarginEnd = (mMarginStart == 0 ? (0 - view.getHeight()) : 0);

        view.setVisibility(View.VISIBLE);
        mImageView.setImageResource(R.drawable.expand_open);
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }


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

        if (interpolatedTime < 1.0f) {

            // Calculating the new bottom margin, and setting it
            mViewLayoutParams.bottomMargin = mMarginStart
                    + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);

            // Invalidating the layout, making us seeing the changes we made
            mAnimatedView.requestLayout();

            // Making sure we didn't run the ending before (it happens!)
        } else if (!mWasEndedAlready) {
            mViewLayoutParams.bottomMargin = mMarginEnd;
            mAnimatedView.requestLayout();

//          if (mIsVisibleAfter) {
            if(mMarginEnd != 0) {
                mAnimatedView.setVisibility(View.GONE);
                mImageView.setImageResource(R.drawable.expand_close);
            }
            mWasEndedAlready = true;
        }
    }
}

© Stack Overflow or respective owner

Related posts about android

Related posts about android-animation