轻柔地创建Android动画

时间:2013-06-25 21:28:34

标签: android android-layout

我有一个LinearLayout(称为布局),其高度为fill_parent,而RelativeLayout内的初始上边距为200dp。 当我单击一个按钮时,我希望topMargin变为0,展开LinearLayout。 我正在使用下一个代码:

     final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.FILL_PARENT,
              RelativeLayout.LayoutParams.FILL_PARENT);

     Animation animation = new Animation() {
              @Override
              protected void applyTransformation(float interpolatedTime,
                       Transformation t) {
                       int topMargin = (int)((1-interpolatedTime)*200);
                       layoutParams.setMargins(0, topMargin, 0, 0);
                       layout.setLayoutParams(layoutParams);
                       LogThis.debug("topMargin: "+topMargin);
              }
     };
     animation.setDuration(1000);
     layout.startAnimation(animation);

问题在于虽然它制作动画,但它并不柔和。它通过4个步骤进行转换,因此效果不佳。

你知道如何轻柔地进行这种转变吗?

提前谢谢。

修改

我在第一时间就有这个:

enter image description here

在动画之后我想要这个:

enter image description here

使用TranslationAnimation,我得到了这个:

enter image description here

1 个答案:

答案 0 :(得分:1)

发生了不稳定的动画,因为这不是动画应该如何使用的。那些是使用传递给方法的Transformation对象。

但是因为你正在使用一个简单的翻译,你可以使用TranslateAnimation轻松实现这个答案:

translate animation

相关问题