Android: Expand/collapse animation

后端 未结 30 2512
说谎
说谎 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:31

    For Smooth animation please use Handler with run method.....And Enjoy Expand /Collapse animation

        class AnimUtils{
    
                     public void expand(final View v) {
                      int ANIMATION_DURATION=500;//in milisecond
            v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            final int targtetHeight = v.getMeasuredHeight();
    
            v.getLayoutParams().height = 0;
            v.setVisibility(View.VISIBLE);
            Animation a = new Animation()
            {
                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    v.getLayoutParams().height = interpolatedTime == 1
                            ? LayoutParams.WRAP_CONTENT
                            : (int)(targtetHeight * interpolatedTime);
                    v.requestLayout();
                }
    
                @Override
                public boolean willChangeBounds() {
                    return true;
                }
            };
    
            // 1dp/ms
            a.setDuration(ANIMATION_DURATION);
    
          // a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
            v.startAnimation(a);
        }
    
    
    
        public void collapse(final View v) {
            final int initialHeight = v.getMeasuredHeight();
    
            Animation a = new Animation()
            {
                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    if(interpolatedTime == 1){
                        v.setVisibility(View.GONE);
                    }else{
                        v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                        v.requestLayout();
                    }
                }
    
                @Override
                public boolean willChangeBounds() {
                    return true;
                }
            };
    
            // 1dp/ms
            a.setDuration(ANIMATION_DURATION);
           // a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
            v.startAnimation(a);
        }
    }
    

    And Call using this code:

           private void setAnimationOnView(final View inactive ) {
        //I am applying expand and collapse on this TextView ...You can use your view 
    
        //for expand animation
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
    
                new AnimationUtililty().expand(inactive);
    
            }
        }, 1000);
    
    
        //For collapse
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
    
                new AnimationUtililty().collapse(inactive);
                //inactive.setVisibility(View.GONE);
    
            }
        }, 8000);
    
    }
    

    Other solution is:

                   public void expandOrCollapse(final View v,String exp_or_colpse) {
        TranslateAnimation anim = null;
        if(exp_or_colpse.equals("expand"))
        {
            anim = new TranslateAnimation(0.0f, 0.0f, -v.getHeight(), 0.0f);
            v.setVisibility(View.VISIBLE);  
        }
        else{
            anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, -v.getHeight());
            AnimationListener collapselistener= new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                v.setVisibility(View.GONE);
                }
            };
    
            anim.setAnimationListener(collapselistener);
        }
    
         // To Collapse
            //
    
        anim.setDuration(300);
        anim.setInterpolator(new AccelerateInterpolator(0.5f));
        v.startAnimation(anim);
    }
    

提交回复
热议问题