Android: show/hide a view using an animation

后端 未结 9 2065
青春惊慌失措
青春惊慌失措 2021-01-30 00:53

I\'ve been looking through many google results / questions on here to determine how to show/hide a view via a vertical animation, but I can\'t seem to find one that\'s exactly r

9条回答
  •  天涯浪人
    2021-01-30 01:07

    I have used this two function to hide and show view with transition animation smoothly.

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public void expand(final View v, int duration, int targetHeight, final int position) {
    
            int prevHeight = v.getHeight();
    
            v.setVisibility(View.VISIBLE);
            ValueAnimator valueAnimator = ValueAnimator.ofInt(0, targetHeight);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    v.getLayoutParams().height = (int) animation.getAnimatedValue();
                    v.requestLayout();
                }
            });
            valueAnimator.setInterpolator(new DecelerateInterpolator());
            valueAnimator.setDuration(duration);
            valueAnimator.start();
            valueAnimator.addListener(new AnimatorListenerAdapter() {
    
                @Override
                public void onAnimationEnd(Animator animation) {
                    v.clearAnimation();
                }
            });
    
        }
    
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public void collapse(final View v, int duration, int targetHeight, final int position) {
            if (position == (data.size() - 1)) {
                return;
            }
            int prevHeight = v.getHeight();
            ValueAnimator valueAnimator = ValueAnimator.ofInt(prevHeight, targetHeight);
            valueAnimator.setInterpolator(new DecelerateInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    v.getLayoutParams().height = (int) animation.getAnimatedValue();
                    v.requestLayout();
                }
            });
            valueAnimator.setInterpolator(new DecelerateInterpolator());
            valueAnimator.setDuration(duration);
            valueAnimator.start();
            valueAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    animBoolArray.put(position, false);
                    v.clearAnimation();
    
                }
            });
        }
    

提交回复
热议问题