How to achieve smooth expand/collapse animation

前端 未结 4 530
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 10:16

I\'m referring the expand / collapse animation code found here.

Android: Expand/collapse animation

Although it works, it doesn\'t do the job well. The animat

4条回答
  •  时光说笑
    2020-12-23 10:44

    Its very easy to get smooth animation (Expand/Collapse) on View

    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);
    }
    

    And use this method in your code. I have used in TextView and it is tested .You may pass your view in this method as a parameter.

            TextView tv=(TextView)findViewById(R.id.textview);
              //TO Expand 
             expandOrCollapse(tv,"expand");
    
            //TO Collapse
            expandOrCollapse(tv,"collapse");
    

    ANd Enjoy smooth Collapse and Expand Animation................

提交回复
热议问题