以编程方式调整布局的大小(如animation)

我想调整我的活动中的一些布局。

这是主XML的代码:

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <LinearLayout android:id="@+id/top" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:background="#3ee3e3" > </LinearLayout> <LinearLayout android:id="@+id/middle" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> </LinearLayout> <LinearLayout android:id="@+id/bottom" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:background="#fe51e6" > </LinearLayout> </LinearLayout> 

正如你所看到的,顶部和底部的布局高度是0,中间的布局覆盖了所有的地方。

我想以编程方式减小中间布局大小,同时增加顶部和底部布局大小,直到所有布局具有相同的高度。

我希望它看起来像一个animation。

我该怎么做?

谢谢

我为类似的目的写了一个ResizeAnimation。 这很简单,但成本高昂。

 /** * an animation for resizing the view. */ public class ResizeAnimation extends Animation { private View mView; private float mToHeight; private float mFromHeight; private float mToWidth; private float mFromWidth; public ResizeAnimation(View v, float fromWidth, float fromHeight, float toWidth, float toHeight) { mToHeight = toHeight; mToWidth = toWidth; mFromHeight = fromHeight; mFromWidth = fromWidth; mView = v; setDuration(300); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { float height = (mToHeight - mFromHeight) * interpolatedTime + mFromHeight; float width = (mToWidth - mFromWidth) * interpolatedTime + mFromWidth; LayoutParams p = mView.getLayoutParams(); p.height = (int) height; p.width = (int) width; mView.requestLayout(); } } 

在Honeycomb(Android 3.0)上,有AnimatorObjectAnimator类来实现更stream畅的animation。

在这里阅读

有关如何使用弹跳插值器对视图组(LinearLayout)的移动进行animation处理的示例。

 BounceInterpolator bounceInterpolator = new BounceInterpolator(); ObjectAnimator anim = ObjectAnimator.ofFloat(myViewGroup, "translationY", 0f, -200 ); anim.setInterpolator(bounceInterpolator); anim.setDuration(1100).start(); 

这将触发一个具有反弹效果的平滑animation,并且实际上将animation视为不像Honeycomb之前的animation。 从文档:

之前的animation改变了目标对象的视觉外观……但是它们实际上并没有改变对象本身。