Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android views expand animation

I'm trying to edit these source to create a simple function that will work on all of my views.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

RelativeLayout mLayout1 = (RelativeLayout) findViewById(R.id.layout1);
RelativeLayout mLayoutToBeExpanded1 = (RelativeLayout) findViewById(R.id.layout_exp_1);
addAction(mLayout, mLayoutToBeExpanded);

RelativeLayout mLayout2 = (RelativeLayout) findViewById(R.id.layout2);
RelativeLayout mLayoutToBeExpanded2 = (RelativeLayout) findViewById(R.id.layout_exp_2);
addAction(mLayout2, mLayoutToBeExpanded2);

RelativeLayout mLayout3 = (RelativeLayout) findViewById(R.id.layout3);
RelativeLayout mLayoutToBeExpanded3 = (RelativeLayout) findViewById(R.id.layout_exp_3);
addAction(mLayout3, mLayoutToBeExpanded3);

}

    public void addAction(final View layout, final View summary) {

    summary.getViewTreeObserver().addOnPreDrawListener(
            new ViewTreeObserver.OnPreDrawListener() {

                @Override
                public boolean onPreDraw() {
                    summary.getViewTreeObserver().removeOnPreDrawListener(this);
                    summary.setVisibility(View.GONE);

                    final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    summary.measure(widthSpec, heightSpec);

                    mAnimator = slideAnimator(0, summary.getMeasuredHeight(), summary);
                    return true;
                }
            });

    layout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (summary.getVisibility() == View.GONE) {
                expand(summary);
            } else {
                collapse(summary);
            }
        }
    });
}

private void expand(View summary) {
    //set Visible
    summary.setVisibility(View.VISIBLE);

            /* Remove and used in preDrawListener
            final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            mLinearLayout.measure(widthSpec, heightSpec);

            mAnimator = slideAnimator(0, mLinearLayout.getMeasuredHeight());
            */

    mAnimator.start();
}

private void collapse(final View summary) {
    int finalHeight = summary.getHeight();

    ValueAnimator mAnimator = slideAnimator(finalHeight, 0, summary);

    mAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationEnd(Animator animator) {
            //Height=0, but it set visibility to GONE
            summary.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationStart(Animator animator) {
        }

        @Override
        public void onAnimationCancel(Animator animator) {
        }

        @Override
        public void onAnimationRepeat(Animator animator) {
        }
    });
    mAnimator.start();
}


private ValueAnimator slideAnimator(int start, int end, final View summary) {

    ValueAnimator animator = ValueAnimator.ofInt(start, end);


    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            //Update Height
            int value = (Integer) valueAnimator.getAnimatedValue();

            ViewGroup.LayoutParams layoutParams = summary.getLayoutParams();
            layoutParams.height = value;
            summary.setLayoutParams(layoutParams);
        }
    });
    return animator;
}

The issue: the first 2 layouts are working only one time. After the first expand -> collapse the onClickFunction is not working. the 3rd layout is working fine, i can expand/collapse it multiple times. Any solution?

like image 880
iGio90 Avatar asked Nov 08 '13 16:11

iGio90


People also ask

How do I animate a view in Android?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.

What is crossfade animation?

Crossfade animations (also know as dissolve) gradually fade out one UI component while simultaneously fading in another. This animation is useful for situations where you want to switch content or views in your app. Crossfades are very subtle and short but offer a fluid transition from one screen to the next.

Can be used to animate between two or more views in Android?

In Android, ViewAnimator is a sub class of FrameLayout container that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views ( like TextView, ImageView or any layout). It is mainly useful to animate the views on screen.


1 Answers

I solved by myself so, here is the working solution for an expandable layout.

Use:

addAction(YourLayout, LayoutToBeExpanded)

Function:

    public void addAction(final View layout, final View summary) {



    layout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (summary.getVisibility() == View.GONE) {
                expand(summary);
            } else {
                collapse(summary);
            }
        }
    });
}

private void expand(View summary) {
    //set Visible
    summary.setVisibility(View.VISIBLE);

            final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            summary.measure(widthSpec, 300);

            mAnimator = slideAnimator(0, 300, summary);

    mAnimator.start();
}

private void collapse(final View summary) {
    int finalHeight = summary.getHeight();

    ValueAnimator mAnimator = slideAnimator(finalHeight, 0, summary);

    mAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationEnd(Animator animator) {
            //Height=0, but it set visibility to GONE
            summary.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationStart(Animator animator) {
        }

        @Override
        public void onAnimationCancel(Animator animator) {
        }

        @Override
        public void onAnimationRepeat(Animator animator) {
        }
    });
    mAnimator.start();
}


private ValueAnimator slideAnimator(int start, int end, final View summary) {

    ValueAnimator animator = ValueAnimator.ofInt(start, end);


    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            //Update Height
            int value = (Integer) valueAnimator.getAnimatedValue();

            ViewGroup.LayoutParams layoutParams = summary.getLayoutParams();
            layoutParams.height = value;
            summary.setLayoutParams(layoutParams);
        }
    });
    return animator;
}

If you would like to also collapse the "mLayoutToBeExpanded" on self click use this:

    public void addAction(final View layout, final View summary) {
    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (summary.getVisibility() == View.GONE) {
                expand(summary);
            } else {
                collapse(summary);
            }
        }
    });

    summary.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (summary.getVisibility() == View.VISIBLE) {
                collapse(summary);
            } else {
                //nothing
            }
        }
    });
}
like image 88
iGio90 Avatar answered Oct 06 '22 07:10

iGio90