简介:
由于界面View.VISIBLE和View.GONE的动画太生硬,所以写了ExpandLayout类来平滑过渡。 基本思路,动态的设置布局的高度。
先上效果图:
expand.gif
核心动画效果代码
/**
* 切换动画实现
*/
private void animateToggle(long animationDuration) {
ValueAnimator heightAnimation = isExpand ?
ValueAnimator.ofFloat(0f, viewHeight) : ValueAnimator.ofFloat(viewHeight, 0f);
heightAnimation.setDuration(animationDuration / 2);
heightAnimation.setStartDelay(animationDuration / 2);
heightAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float val = (float) animation.getAnimatedValue();
setViewHeight(layoutView, (int) val);
}
});
heightAnimation.start();
}
使用:
布局文件中 (引入自定义View)
android:id="@+id/expandLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFF00"
android:clickable="true">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:text="大辉儿: 这里是可收缩布局内部" />
java代码中 初始状态是否显示,toggleExpand切换折叠/展开状态<