How to achieve smooth expand/collapse animation

前端 未结 4 531
爱一瞬间的悲伤
爱一瞬间的悲伤 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:39

    These is a very good example on SlideExpandibleList in Github.

    https://github.com/tjerkw/Android-SlideExpandableListView

    Hope this will help you to achieve smooth animation and collapse.

    In this example , it saved the state of expand list item. So even if you will scroll down the list it wont let the expanded list item to close.

    In this example expand or collapse event is given on Button, so you need to change it List item parent layout.

    I attached the screen shots.

    Hope this will help you.

    enter image description here

    0 讨论(0)
  • 2020-12-23 10:39

    You can do Smooth Animation of View this way:

    public class ViewAnimationUtils {
    
        public static void expand(final View v) {
            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;
                }
            };
    
            a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
            v.startAnimation(a);
        }
    
        public static 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;
                }
            };
    
            a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
            v.startAnimation(a);
        }
    }
    

    Hope it will help you.

    0 讨论(0)
  • 2020-12-23 10:44

    The easiest way to do animations is by adding the following to your xml for your linear layout:

    android:animateLayoutChanges="true"
    

    It works great and uses default animations directly from Android

    0 讨论(0)
  • 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................

    0 讨论(0)
提交回复
热议问题