如何以编程方式动画移动片段?

时间:2016-04-19 18:10:48

标签: android xml android-fragments android-animation

我想点击一个按钮,让某些View容器平滑/过渡地改变大小。这可以在Android中完成吗?

1 个答案:

答案 0 :(得分:0)

如果通过视图容器表示ViewGroup,您可以使用ValueAnimator。这是一个简单的计时引擎,可用于制作动画。

例如,如果要在400ms内从startHeight为RelativeLayout rl设置高度更改为finalHeight,您可以执行以下操作:

ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
va.setDuration(400);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                Float value = (Float) animation.getAnimatedValue();
                rl.height = (int) (startHeight + (finalHeight - startHeight) * value);
                rl.requestLayout();
            }
        });

您可以使用不同的插值器并自定义ValueAnimator来做一些非常好的效果。 http://developer.android.com/reference/android/animation/ValueAnimator.html