Android: Expand/collapse animation

后端 未结 30 2205
说谎
说谎 2020-11-22 05:01

Let\'s say I have a vertical linearLayout with :

[v1]
[v2]

By default v1 has visibily = GONE. I would like to show v1 with an expand animat

30条回答
  •  死守一世寂寞
    2020-11-22 05:44

    /**
     * Animation that either expands or collapses a view by sliding it down to make
     * it visible. Or by sliding it up so it will hide. It will look like it slides
     * behind the view above.
     * 
     */
    public class FinalExpandCollapseAnimation extends Animation
    {
        private View mAnimatedView;
        private int mEndHeight;
        private int mType;
        public final static int COLLAPSE = 1;
        public final static int EXPAND = 0;
        private LinearLayout.LayoutParams mLayoutParams;
        private RelativeLayout.LayoutParams mLayoutParamsRel;
        private String layout;
        private Context context;
    
        /**
         * Initializes expand collapse animation, has two types, collapse (1) and
         * expand (0).
         * 
         * @param view
         *            The view to animate
         * @param type
         *            The type of animation: 0 will expand from gone and 0 size to
         *            visible and layout size defined in xml. 1 will collapse view
         *            and set to gone
         */
        public FinalExpandCollapseAnimation(View view, int type, int height, String layout, Context context)
        {
            this.layout = layout;
            this.context = context;
            mAnimatedView = view;
            mEndHeight = mAnimatedView.getMeasuredHeight();
            if (layout.equalsIgnoreCase("linear"))
                mLayoutParams = ((LinearLayout.LayoutParams) view.getLayoutParams());
            else
                mLayoutParamsRel = ((RelativeLayout.LayoutParams) view.getLayoutParams());
            mType = type;
            if (mType == EXPAND)
            {
                AppConstant.ANIMATED_VIEW_HEIGHT = height;
            }
            else
            {
                if (layout.equalsIgnoreCase("linear"))
                    mLayoutParams.topMargin = 0;
                else
                    mLayoutParamsRel.topMargin = convertPixelsIntoDensityPixels(36);
            }
            setDuration(600);
        }
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t)
        {
            super.applyTransformation(interpolatedTime, t);
            if (interpolatedTime < 1.0f)
            {
                if (mType == EXPAND)
                {
                    if (layout.equalsIgnoreCase("linear"))
                    {
                        mLayoutParams.height = AppConstant.ANIMATED_VIEW_HEIGHT
                                + (-AppConstant.ANIMATED_VIEW_HEIGHT + (int) (AppConstant.ANIMATED_VIEW_HEIGHT * interpolatedTime));
                    }
                    else
                    {
                        mLayoutParamsRel.height = AppConstant.ANIMATED_VIEW_HEIGHT
                                + (-AppConstant.ANIMATED_VIEW_HEIGHT + (int) (AppConstant.ANIMATED_VIEW_HEIGHT * interpolatedTime));
                    }
                    mAnimatedView.setVisibility(View.VISIBLE);
                }
                else
                {
                    if (layout.equalsIgnoreCase("linear"))
                        mLayoutParams.height = mEndHeight - (int) (mEndHeight * interpolatedTime);
                    else
                        mLayoutParamsRel.height = mEndHeight - (int) (mEndHeight * interpolatedTime);
                }
                mAnimatedView.requestLayout();
            }
            else
            {
                if (mType == EXPAND)
                {
                    if (layout.equalsIgnoreCase("linear"))
                    {
                        mLayoutParams.height = AppConstant.ANIMATED_VIEW_HEIGHT;
                        mLayoutParams.topMargin = 0;
                    }
                    else
                    {
                        mLayoutParamsRel.height = AppConstant.ANIMATED_VIEW_HEIGHT;
                        mLayoutParamsRel.topMargin = convertPixelsIntoDensityPixels(36);
                    }
                    mAnimatedView.setVisibility(View.VISIBLE);
                    mAnimatedView.requestLayout();
                }
                else
                {
                    if (layout.equalsIgnoreCase("linear"))
                        mLayoutParams.height = 0;
                    else
                        mLayoutParamsRel.height = 0;
                    mAnimatedView.setVisibility(View.GONE);
                    mAnimatedView.requestLayout();
                }
            }
        }
    
        private int convertPixelsIntoDensityPixels(int pixels)
        {
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            return (int) metrics.density * pixels;
        }
    }
    

    The class can be called in following way

       if (findViewById(R.id.ll_specailoffer_show_hide).getVisibility() == View.VISIBLE) {
                            ((ImageView) findViewById(R.id.iv_specialhour_seemore)).setImageResource(R.drawable.white_dropdown_up);
    
                            FinalExpandCollapseAnimation finalExpandCollapseAnimation = new FinalExpandCollapseAnimation(
                                    findViewById(R.id.ll_specailoffer_show_hide),
                                    FinalExpandCollapseAnimation.COLLAPSE,
                                    SpecialOfferHeight, "linear", this);
                            findViewById(R.id.ll_specailoffer_show_hide)
                                    .startAnimation(finalExpandCollapseAnimation);
                            ((View) findViewById(R.id.ll_specailoffer_show_hide).getParent()).invalidate();
                        } else {
                            ((ImageView) findViewById(R.id.iv_specialhour_seemore)).setImageResource(R.drawable.white_dropdown);
    
                            FinalExpandCollapseAnimation finalExpandCollapseAnimation = new FinalExpandCollapseAnimation(
                                    findViewById(R.id.ll_specailoffer_show_hide),
                                    FinalExpandCollapseAnimation.EXPAND,
                                    SpecialOfferHeight, "linear", this);
                            findViewById(R.id.ll_specailoffer_show_hide)
                                    .startAnimation(finalExpandCollapseAnimation);
                            ((View) findViewById(R.id.ll_specailoffer_show_hide).getParent()).invalidate();
                        }
    

提交回复
热议问题