以编程方式调整布局大小(作为动画)

时间:2011-11-15 17:32:04

标签: android layout animation android-animation

我想在我的Activity中调整一些布局。

以下是主要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,中间 布局涵盖了所有的地方。

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

我希望它看起来像一个动画。

我该怎么做?

由于

3 个答案:

答案 0 :(得分:95)

我为了类似的目的写了一个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();
    }
}

答案 1 :(得分:4)

在Honeycomb(Android 3.0)上,有AnimatorObjectAnimator类可以使动画更流畅。

阅读here

如何使用反弹插补器为视图组(LinearLayout)的移动设置动画示例。

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

这将触发具有反弹效果的平滑动画,并且实际上移动视图不像Honeycomb之前的动画。来自文档:

  

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

答案 2 :(得分:1)

您还可以使用Google的新Spring动画调整大小。

SpringAnimation创建方法:

fun getSpringAnimation(view: View, springAnimationType: FloatPropertyCompat<View>, finalPosition: Float): SpringAnimation {
    val animation = SpringAnimation(view, springAnimationType )
    // create a spring with desired parameters
    val spring = SpringForce()
    spring.finalPosition = finalPosition
    spring.stiffness = SpringForce.STIFFNESS_VERY_LOW // optional
    spring.dampingRatio = SpringForce.DAMPING_RATIO_NO_BOUNCY // optional
    // set your animation's spring
    animation.spring = spring
    return animation
}

用法(调整为原始视图大小的80%。)

 getSpringAnimation(view, SpringAnimation.SCALE_X, 0.8f).start()
 getSpringAnimation(view, SpringAnimation.SCALE_Y, 0.8f).start()
相关问题