Android 属性 动画:如何增加视图高度?

Android property animation: how to increase view height?

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

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

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

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(); 

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

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

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

如下所示,它非常简单。

<LinearLayout android:id="@+id/container"
android:animateLayoutChanges="true"
.../>

更多信息如下:

https://developer.android.com/training/animation/layout

这不是对这个问题的直接回答。但是,它可能会对某人有所帮助。

有时,我们想要 increase/decrease 视图的高度,因为它的某些子项正在 added/removed(或刚成为 visible/gone)。

在这些情况下,您确实可以使用 Android 默认动画。正如其他答案中提到的,您可以通过以下方式设置:

<LinearLayout
    ...
    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 上进行了测试。当您的视图可见时,这将自动添加淡入淡出 in/out。它还会在相同更改时为视图的 height/width 设置动画(例如当您更改其中一个子视图的可见性时等)。

所以,我建议至少试一试。

在kotlin中使用这个方法:

private fun increaseViewSize(view: View) {
    val valueAnimator = ValueAnimator.ofInt(view.measuredHeight, view.measuredHeight+20)
    valueAnimator.duration = 500L
    valueAnimator.addUpdateListener {
        val animatedValue = valueAnimator.animatedValue as Int
        val layoutParams = view.layoutParams
        layoutParams.height = animatedValue
        view.layoutParams = layoutParams
    }
    valueAnimator.start()
}

对于kotlin你可以使用这个方法

private fun increaseViewSize(view: View, increaseValue: Int) {
    val valueAnimator =
        ValueAnimator.ofInt(view.measuredHeight, view.measuredHeight + increaseValue)
    valueAnimator.duration = 500L
    valueAnimator.addUpdateListener {
        val animatedValue = valueAnimator.animatedValue as Int
        val layoutParams = view.layoutParams
        layoutParams.height = animatedValue
        view.layoutParams = layoutParams
    }
    valueAnimator.start()
}

它将增加参数中定义的视图大小

基于@Minion 的回答

这是我的代码,用于在 recycleView

内将项目滚动到屏幕中心的动画
// Scrolling with animation
val valueAnimator = ValueAnimator.ofInt(getWidth() / 2)
valueAnimator.duration = 250L
valueAnimator.addUpdateListener {
    val animatedValue = valueAnimator.animatedValue as Int
    (layoutManager as LinearLayoutManager).scrollToPositionWithOffset(itemToScroll, animatedValue)
}
valueAnimator.start()
// End of scrolling with animation

注意它有点类似于Swift中的UIView.animate,但更复杂

fun View.animateHeightFromTo(initialHeight: Int, finalHeight: Int) {
    val animator = ValueAnimator.ofInt(initialHeight, finalHeight)
    animator.duration = 250
    animator.addUpdateListener {
        val value = it.animatedValue as Int
        val lp = this.layoutParams
        lp.height = value
        this.layoutParams = lp
        isVisible = value != 0
    }
    animator.start()
}

可见性设置对我来说很方便,但你可能不想要那个