安卓属性动画:如何增加观看高度?

如何在Android中使用属性动画增加视图高度?

ObjectAnimator a = ObjectAnimator.ofFloat(viewToIncreaseHeight, "translationY", -100);
a.setInterpolator(new AccelerateDecelerateInterpolator());
a.setDuration(1000);
a.start();

平移Y实际上移动视图而不是增加高度。如何增加视图的高度?


答案 1
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(); 

答案 2

您可以使用ViewPropertyAnimator,它可以为您节省一些代码行:

yourView.animate()
   .scaleY(-100f)
   .setInterpolator(new AccelerateDecelerateInterpolator())
   .setDuration(1000);

这应该是您所需要的,请务必查看ViewPropertyAnimator的文档和所有可用方法。


推荐