Java-Android属性动画:如何增加视图高度?
如何使用Android中的Property Animations增加视图高度?
ObjectAnimator a = ObjectAnimator.ofFloat(viewToIncreaseHeight, "translationY", -100);
a.setInterpolator(new AccelerateDecelerateInterpolator());
a.setDuration(1000);
a.start();
实际上,translationY移动视图不会增加高度。 如何增加视图的高度?
4个解决方案
113 votes
ValueAnimator anim = ValueAnimator.ofInt(viewToIncreaseHeight.getMeasuredHeight(), -100);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = viewToIncreaseHeight.getLayoutParams();
layoutParams.height = val;
viewToIncreaseHeight.setLayoutParams(layoutParams);
}
});
anim.setDuration(DURATION);
anim.start();
rajan ks answered 2020-01-25T23:33:22Z
18 votes
您可以使用ViewPropertyAnimator,它可以为您节省一些代码行:
yourView.animate().scaleY(-100f).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(1000);
那应该是您所需要的,请务必检查文档和ViewPropertyAnimator的所有可用方法。
A Honey Bustard answered 2020-01-25T23:33:47Z
2 votes
这不是这个问题的直接答案。 但是,它可能会帮助某人。
有时,我们想增加/减少视图的高度,因为正在添加/删除其某些子视图(或只是变得可见/消失)。
在这种情况下,您确实可以使用Android默认动画。 如其他答案所述,您可以通过以下方式进行设置:
...
android:animateLayoutChanges="true"
.../>
或在Java中:
linearLayout.setLayoutTransition(new LayoutTransition());
如果创建了自定义视图:
public StickerPickerView(final Context context, final AttributeSet attrs,
final int defStyleAttr) {
super(context, attrs, defStyleAttr);
setLayoutTransition(new LayoutTransition());
}
对我来说,这非常令人满意,但是我只是在最新的API上进行了测试。 当您的视图变为可见时,它将自动添加淡入/淡出。 当相同的更改(例如,更改子视图之一的可见性等时)时,还将为视图的高度/宽度设置动画。
因此,我建议至少尝试一下。
W0rmH0le answered 2020-01-25T23:34:34Z
-1 votes
如下所示,这非常简单。
android:animateLayoutChanges="true"
.../>
以下是更多信息:
[https://developer.android.com/training/animation/layout]
Amit Garg answered 2020-01-25T23:35:03Z