Android TabLayout选项卡Tab布局伸缩动画

需求:当ScrollView上滑时,动画收缩隐藏Tab;当ScrollView下拉时,动画扩展显示Tab

方案:ValueAnimator属性动画改变TabLayout的高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
private ValueAnimator mGoodsTabIndentAnimator;
private ValueAnimator mGoodsTabExpandAnimator;
private int mGoodsTabLayoutHeight;

public void startGoodsTabIndentAnimation() {
if (mGoodsTabIndentAnimator == null) {
mGoodsTabLayoutHeight = mGoodsTabLayout.getHeight();
NLog.i(TAG, "mGoodsTabLayout Height: " + mGoodsTabLayoutHeight);
mGoodsTabIndentAnimator = ValueAnimator.ofInt(mGoodsTabLayout.getHeight(), 0);
mGoodsTabIndentAnimator.setDuration(200);
mGoodsTabIndentAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
Integer value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams params = mGoodsTabLayout.getLayoutParams();
params.height = value;
mGoodsTabLayout.setLayoutParams(params);
}
});
}
mGoodsTabIndentAnimator.start();
}

public void startGoodsTabExpandAnimation() {
if (mGoodsTabExpandAnimator == null) {
mGoodsTabExpandAnimator = ValueAnimator.ofInt(0, mGoodsTabLayoutHeight);
mGoodsTabExpandAnimator.setDuration(200);
mGoodsTabExpandAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
Integer value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams params = mGoodsTabLayout.getLayoutParams();
params.height = value;
mGoodsTabLayout.setLayoutParams(params);
}
});
}
mGoodsTabExpandAnimator.start();
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :