安卓(Android) 怎么实现持续折叠垂直视图(View)

2019年10月15日20:26:56安卓(Android) 怎么实现持续折叠垂直视图(View)已关闭评论 433 903字阅读3分0秒

1.简介

安卓(Android) 怎么实现持续折叠垂直视图(View)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import android.view.View;

import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class Main {
    public static void collapseVertical(final View v, int duration,
            final boolean hide, Animation.AnimationListener listener) {
        final int initialHeight = v.getMeasuredHeight();

        Animation a = new AlphaAnimation((float) 1.0, (float) 0.0) {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                super.applyTransformation(interpolatedTime, t);
                if (interpolatedTime == 1 && hide) {
                    v.setVisibility(View.INVISIBLE);
                } else {
                    v.getLayoutParams().height = initialHeight
                            - (int) (initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        if (listener != null) {
            a.setAnimationListener(listener);
        }
        a.setDuration(duration);
        v.startAnimation(a);
    }
}

 
继续阅读
编程十万个怎么办