70

如何在 Android 中使用 Property Animations 增加视图高度?

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

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

4

8 回答 8

148
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(); 
于 2015-09-29T03:54:29.623 回答
17

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

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

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

于 2015-09-29T07:17:57.273 回答
12

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

有时,我们想要增加/减少视图的高度,因为它的一些子项正在被添加/删除(或者只是变得可见/消失)。

在这些情况下,您确实可以使用 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 上进行了测试。当您的视图变得可见时,这将自动添加淡入/淡出。当相同的更改(例如当您更改其中一个子视图的可见性等时)时,它还会为您的视图的高度/宽度设置动画。

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

于 2019-07-22T14:10:07.553 回答
6

在 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()
}
于 2020-02-17T10:40:33.707 回答
1

因为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的回答

于 2020-05-07T10:06:25.643 回答
0

这是我的代码,用于将项目滚动到 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,但更复杂

于 2020-12-01T12:58:20.503 回答
0
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()
}

可见性设置对我来说很方便,但您可能不希望这样

于 2021-06-17T13:28:56.353 回答
-2

正如您在下面看到的那样,它非常简单。

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

更多信息如下:

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

于 2019-01-18T23:38:19.980 回答