关于Android中View的折叠和伸展均伴随动画
之前讲了Textview的折叠和伸展,是通过 ObjectAnimator.ofInt(tv, "maxLines", numLines); 来实现的,其中主要是第二个参数 maxlines 这个属于TextView的属性。 如果换成其他View,没了这个属性的话就无法实现了,那么这个时候我们就需要去通过设置高度来实现,接着我们把maxLines设置成height或者layout_height发现没有效果。那么应该怎么实现呢?使用ValueAnimator
ValueAnimator valueAnimator = ObjectAnimator.ofInt(startH,endH).setDuration(500);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
rv.getLayoutParams().height = animatedValue;
rv.requestLayout();
}
});
valueAnimator.start();
现在最主要的问题是 startH(动画前的高度) 和 endH(动画后的高度) 如何实现
- 折叠
startH = view.getHeight();
endH = 1;//不能设置成0,不然你会发现它又回到了原始高度
- 伸展
startH = 1;
view.measure(View.MeasureSpec.UNSPECIFIED, ViewGroup.LayoutParams.WRAP_CONTENT);
int measuredHeight = view.getMeasuredHeight();
endH = measuredHeight;//利用measure方法测量

浙公网安备 33010602011771号